目录
字节FileInputStream输入与FileOutputStream输出流
字节缓冲BufferedInputStream输入与BufferedOutputStream输出流
字符缓冲BufferedReader输入与BufferedWriter输出流
字节PrintStream打印流与字符PrintWriter打印流
简介
流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流。
根据处理数据类型的不同分为:字节流和字符流
根据数据流向的不同分为:输入流和输出流
字节FileInputStream输入与FileOutputStream输出流
字节输入与输出流适用于操作某个文件对象本身。
字节是数据传输的基本单位,文件内容也是以字节为单位存储的,从文件中把数据读到程序中使用输入流,从程序把数据写到文件中使用输出流。
字节FileInputStream输入流
import java.io.*;
public class TestInputStream {
public static void main(String[] args) {
//指定输入目标文件路径
File file = new File("D:\\test01.txt");
InputStream inputStream = null;
try {
//实例化字节文件输入流对象
inputStream = new FileInputStream(file);
//定义一次性读取的字节空间大小
byte[] bytes = new byte[1024];
//定义接收实际读取的字节空间大小的长度
int len = -1;
//将数据读取到数组中,并返回读取的字节数长度,当等于-1时文件读取结束
while ((len = inputStream.read(bytes)) != -1){
//打印从数组位置0开始,打印长度为len
System.out.println(new String(bytes,0,len));
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
//最后判断该流不为空,必须关闭流,释放资源
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
字节FileOutputStream输出流
import java.io.*;
public class TestInputStream {
public static void main(String[] args) {
//指定输出目标文件路径
File file = new File("D:\\test01.txt");
OutputStream outputStream = null;
try {
//实例化字节文件输出流对象
outputStream = new FileOutputStream(file,false);//是否追加输出内容
//将指定字符串输出到该流中
outputStream.write("输出内容".getBytes());
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
//最后判断该流不为空,必须关闭流,释放资源
if (outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
字符FileReader输入与FileWriter输出流
字符输入与输出流适用于操作某个文件中的内容,防止字符乱码。
字符流处理的单元为2个字节的Unicode字符,分别操作字符、字符数组或字符串,而字节流处理单元为1个字节,操作字节和字节数组。
字符FileReader输入流
import java.io.*;
public class TestFileReader {
public static void main(String[] args) {
File file = new File("D:\\test01.txt");
Reader reader = null;
try {
reader = new FileReader(file);
//字符流需要定义字符数组存储
char[] chars = new char[3];
int len = -1;
//创建字符串缓冲对象存储数据,用于输出
StringBuffer stringBuffer = new StringBuffer();
while ((len = reader.read(chars)) != -1){
//追加字符串到字符串缓冲流中
stringBuffer.append(new String(chars,0,len));
}
System.out.println(stringBuffer);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (reader != null){
try {
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
字符FileWriter输出流
import java.io.*;
public class TestFileWriter {
public static void main(String[] args) {
File file = new File("D:\\test01.txt");
Writer writer = null;
try {
writer = new FileWriter(file,true);//追加方式输出
writer.write("输出内容");
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (writer != null){
try {
writer.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
字节缓冲BufferedInputStream输入与BufferedOutputStream输出流
使用缓冲流能够更高效的读写信息,原理是将数据先缓冲起来,然后一起写入或读取出来。
BufferedInputStream为了另一个输入流添加一些功能,在创建此对象时,会创建一个内部缓冲区数组,用于缓冲数据。
BufferedOutputStream通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中,而不必针对每次字节写入调用底层系统。
字节缓冲BufferedInputStream输入流
import java.io.*;
public class TestBufferedInputStream {
public static void main(String[] args) {
File file = new File("D:\\test01.txt");
InputStream inputStream = null;
BufferedInputStream bufferedInputStream = null;
try {
inputStream = new FileInputStream(file);
//将字节输入流对象放入字节缓冲输入流对象中
bufferedInputStream = new BufferedInputStream(inputStream);
byte[] bytes = new byte[1024];
int len = -1;
while ((len = bufferedInputStream.read(bytes)) != -1){
System.out.println(new String(bytes,0,len));
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (bufferedInputStream != null){
try {
bufferedInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
字节缓冲BufferedOutputStream输出流
import java.io.*;
public class TestBufferedOutputStream {
public static void main(String[] args) {
File file = new File("D:\\test01.txt");
OutputStream outputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
outputStream = new FileOutputStream(file,false);
//将字节输出流对象放入字节缓冲输出流对象中
bufferedOutputStream = new BufferedOutputStream(outputStream);
bufferedOutputStream.write("输出内容".getBytes());
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (bufferedOutputStream != null){
try {
bufferedOutputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
字符缓冲BufferedReader输入与BufferedWriter输出流
BufferedReader从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
BufferedWriter将文本写入字符输出流,缓冲各个字符,从而实现字符、数组和行的高效写入。
字符缓冲BufferedReader输入流
import java.io.*;
public class TestBufferedReader {
public static void main(String[] args) {
File file = new File("D:\\test01.txt");
Reader reader = null;
BufferedReader bufferedReader = null;
try {
reader = new FileReader(file);
bufferedReader = new BufferedReader(reader);
//用字符数组存储
char[] chars = new char[1024];
int len = -1;
while ((len = bufferedReader.read(chars)) != -1){
System.out.println(new String(chars,0,len));
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (bufferedReader != null){
try {
bufferedReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (reader != null){
try {
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
字符缓冲BufferedWrite输出流
import java.io.*;
public class TestBufferedWriter {
public static void main(String[] args) {
File file = new File("D:\\test01.txt");
Writer writer = null;
BufferedWriter bufferedWriter = null;
try {
writer = new FileWriter(file);
bufferedWriter = new BufferedWriter(writer);
bufferedWriter.write("输出内容");
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (bufferedWriter != null){
try {
bufferedWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (writer != null){
try {
writer.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
字节PrintStream打印流与字符PrintWriter打印流
字节或字符打印流可以用来增强字节或字符输出流的打印功能,有效提供了更多丰富的输出方法。
字节PrintStream打印流
import java.io.*;
public class TestPrintStream {
public static void main(String[] args) {
File file = new File("D:\\test01.txt");
FileOutputStream output = null;
BufferedOutputStream bufferedOutput = null;
PrintStream printStream = null;
try {
output = new FileOutputStream(file,true);
bufferedOutput = new BufferedOutputStream(output);
printStream = new PrintStream(bufferedOutput);
printStream.print("字节打印输出内容");
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} finally {
try {
if (printStream != null){
printStream.close();
}
if (bufferedOutput != null){
bufferedOutput.close();
}
if (output != null){
output.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
字符PrintWriter打印流
import java.io.*;
public class TestPrintWriter {
public static void main(String[] args) {
File file = new File("D:\\test01.txt");
Writer writer = null;
BufferedWriter bufferedWriter = null;
PrintWriter printWriter = null;
try {
writer = new FileWriter(file,false);
bufferedWriter = new BufferedWriter(writer);
printWriter = new PrintWriter(bufferedWriter);
printWriter.write("字符打印输出流");
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (printWriter != null){
printWriter.close();
}
if (bufferedWriter != null){
bufferedWriter.close();
}
if (writer != null){
writer.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}