字节输入流
InputStream 类及其子类的对象表示字节输入流,InputStream 类的常用子类如下。
ByteArrayInputStream 类:将字节数组转换为字节输入流,从中读取字节。
FileInputStream 类:从文件中读取数据。
PipedInputStream 类:连接到一个 PipedOutputStream(管道输出流)。
SequenceInputStream 类:将多个字节输入流串联成一个字节输入流。
ObjectInputStream 类:将对象反序列化。
字节输出流
OutputStream 类及其子类的对象表示一个字节输出流。OutputStream 类的常用子类如下。
ByteArrayOutputStream 类:向内存缓冲区的字节数组中写数据。
FileOutputStream 类:向文件中写数据。
PipedOutputStream 类:连接到一个 PipedlntputStream(管道输入流)。
ObjectOutputStream 类:将对象序列化。
public class test08 {
public static void main(String[] args) {
byte[] b = new byte[] { 1, -1, 25, -22, -5, 23 }; // 创建数组
ByteArrayInputStream bais = new ByteArrayInputStream(b, 0, 6); // 创建字节数组输入流
int i = bais.read(); // 从输入流中读取下一个字节,并转换成int型数据
while (i != -1) { // 如果不返回-1,则表示没有到输入流的末尾
System.out.println("原值=" + (byte) i + "\t\t\t转换为int类型=" + i);
i = bais.read(); // 读取下一个
}
}
}
/**
原值=1 转换为int类型=1
原值=-1 转换为int类型=255
原值=25 转换为int类型=25
原值=-22 转换为int类型=234
原值=-5 转换为int类型=251
原值=23 转换为int类型=23
*/
ByteArrayOutputStream 类
public class Test09 {
public static void main(String[] args) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[] { 1, -1, 25, -22, -5, 23 }; // 创建数组
baos.write(b, 0, 6); // 将字节数组b中的前4个字节元素写到输出流中
System.out.println("数组中一共包含:" + baos.size() + "字节"); // 输出缓冲区中的字节数
byte[] newByteArray = baos.toByteArray(); // 将输出流中的当前内容转换成字节数组
System.out.println(Arrays.toString(newByteArray)); // 输出数组中的内容
}
}
//文件输入流
public class Test10 {
public static void main(String[] args) {
File f = new File("D:/myJava/HelloJava.java");
FileInputStream fis = null;
try {
// 因为File没有读写的能力,所以需要有个InputStream
fis = new FileInputStream(f);
// 定义一个字节数组
byte[] bytes = new byte[1024];
int n = 0; // 得到实际读取到的字节数
System.out.println("D:\\myJava\\HelloJava.java文件内容如下:");
// 循环读取
while ((n = fis.read(bytes)) != -1) {
String s = new String(bytes, 0, n); // 将数组中从下标0到n的内容给s
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileInputStream 常用的构造方法主要有如下两种重载形式。
-
FileInputStream(File file):通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。
-
FileInputStream(String name):通过打开一个到实际文件的链接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。
//文件输出流
public class Test11 {
public static void main(String[] args) {
FileInputStream fis = null; // 声明FileInputStream对象fis
FileOutputStream fos = null; // 声明FileOutputStream对象fos
try {
File srcFile = new File("D:/myJava/HelloJava.java");
fis = new FileInputStream(srcFile); // 实例化FileInputStream对象
File targetFile = new File("D:/myJava/HelloJava.txt"); // 创建目标文件对象,该文件不存在
fos = new FileOutputStream(targetFile); // 实例化FileOutputStream对象
byte[] bytes = new byte[1024]; // 每次读取1024字节
int i = fis.read(bytes);
while (i != -1) {
fos.write(bytes, 0, i); // 向D:\HelloJava.txt文件中写入内容
i = fis.read(bytes);
}
System.out.println("写入结束!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close(); // 关闭FileInputStream对象
fos.close(); // 关闭FileOutputStream对象
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileOutputStream 类的构造方法主要有如下 4 种重载形式。
-
FileOutputStream(File file):创建一个文件输出流,参数 file 指定目标文件。
-
FileOutputStream(File file,boolean append):创建一个文件输出流,参数 file 指定目标文件,append 指定是否将数据添加到目标文件的内容末尾,如果为 true,则在末尾添加;如果为 false,则覆盖原有内容;其默认值为 false。
-
FileOutputStream(String name):创建一个文件输出流,参数 name 指定目标文件的文件路径信息。
-
FileOutputStream(String name,boolean append):创建一个文件输出流,参数 name 和 append 的含义同上。
字符流
Reader 类是所有字符流输入类的父类
Reader 类的常用子类如下。
-
CharArrayReader 类:将字符数组转换为字符输入流,从中读取字符。
-
StringReader 类:将字符串转换为字符输入流,从中读取字符。
-
BufferedReader 类:为其他字符输入流提供读缓冲区。
-
PipedReader 类:连接到一个 PipedWriter。
-
InputStreamReader 类:将字节输入流转换为字符输入流,可以指定字符编码。
Writer 类是所有字符输出流的父类
Writer 类的常用子类如下。
-
CharArrayWriter 类:向内存缓冲区的字符数组写数据。
-
StringWriter 类:向内存缓冲区的字符串(StringBuffer)写数据。
-
BufferedWriter 类:为其他字符输出流提供写缓冲区。
-
PipedWriter 类:连接到一个 PipedReader。
-
OutputStreamReader 类:将字节输出流转换为字符输出流,可以指定字符编码。
字符文件输入流
FileReader
public class Test12 {
public static void main(String[] args) {
FileReader fr = null;
try {
fr = new FileReader("D:/myJava/HelloJava.java"); // 创建FileReader对象
int i = 0;
System.out.println("D:\\myJava\\HelloJava.java文件内容如下:");
while ((i = fr.read()) != -1) { // 循环读取
System.out.print((char) i); // 将读取的内容强制转换为char类型
}
} catch (Exception e) {
System.out.print(e);
} finally {
try {
fr.close(); // 关闭对象
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字符文件输出流
FileWriter
FileWriter 类的创建不依赖于文件存在与否,如果关联文件不存在,则会自动生成一个新的文件。在创建文件之前,FileWriter 将在创建对象时打开它作为输出。如果试图打开一个只读文件,将引发一个 IOException 异常。
public class Test13 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
FileWriter fw = null;
try {
fw = new FileWriter("D:\\myJava\\book.txt"); // 创建FileWriter对象
for (int i = 0; i < 4; i++) {
System.out.println("请输入第" + (i + 1) + "个字符串:");
String name = input.next(); // 读取输入的名称
fw.write(name + "\r\n"); // 循环写入文件
}
System.out.println("录入完成!");
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
fw.close(); // 关闭对象
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字符缓冲区输入流
BufferedReader 类主要用于辅助其他字符输入流,它带有缓冲区,可以先将一批数据读到内存缓冲区
public class Test13 {
public static void main(String[] args) {
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader("D:\\myJava\\book.txt"); // 创建 FileReader 对象
br = new BufferedReader(fr); // 创建 BufferedReader 对象
System.out.println("D:\\myJava\\book.txt 文件中的内容如下:");
String strLine = "";
while ((strLine = br.readLine()) != null) { // 循环读取每行数据
System.out.println(strLine);
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fr.close(); // 关闭 FileReader 对象
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字符缓冲区输出流
BufferedWriter 类主要用于辅助其他字符输出流
该类除了可以给字符输出流提供缓冲区之外,还提供了一个新的方法 newLine(),该方法用于写入一个行分隔符。行分隔符字符串由系统属性 line.separator 定义,并且不一定是单个新行(\n)符。