java中的IO

it2022-05-05  129

IO的分类 可根据数据的流向分为: 输入流 :把数据从 其他设备 上读取到 内存 中的流。 输出流 :把数据从 内存 中写出到 其他设备 上的流。 数据的格局分为: 字节流 :以字节为单位,读写数据的流。 字符流 :以字符为单位,读写数据的流。

字节流

一切文件数据(文本、图片、视频等)在存储时,都是以二进制数字的形式保存,都一个一个的字节,那么传输时一 样如此。所以,字节流可以传输任意文件数据。在操作流的时候,我们要时刻明确,无论使用什么样的流对象,底 层传输的始终为二进制数据。

字节输出流【OutputStream】(java.io.OutputStream )抽象类是表示字节输出流的所有类的超类。 public void close() :关闭此输出流并释放与此流相关联的任何系统资源。(当完成流的操作时,必须调用此方法,释放系统资源) public void flush() :刷新此输出流并强制任何缓冲的输出字节被写出。 public void write(byte[] b) :将 b.length字节从指定的字节数组写入此输出流。 public void write(byte[] b, int off, int len) :从指定的字节数组写入 len字节,从偏移量 off开始输 出到此输出流。 public abstract void write(int b) :将指定的字节输出流。

FileOutputStream(OutputStream子类)

public class FileOutputStreamConstructor throws IOException { public static void main(String[] args) { // 使用File对象创建流对象 File file = new File("a.txt"); FileOutputStream fos = new FileOutputStream(file); // 使用文件名称创建流对象 FileOutputStream fos = new FileOutputStream("b.txt"); } }

写出字节: write(int b) 方法,每次可以写出一个字节数据

public static void main(String[] args) throws IOException { // 使用文件名称创建流对象 FileOutputStream fos = new FileOutputStream("fos.txt"); // 写出数据 fos.write(97); // 写出第1个字节 fos.write(98); // 写出第2个字节 fos.write(99); // 写出第3个字节 // 关闭资源 fos.close(); 输出结果: abc }

写出字节数组: write(byte[] b) ,每次可以写出数组中的数据

public static void main(String[] args) throws IOException { // 使用文件名称创建流对象 FileOutputStream fos = new FileOutputStream("fos.txt"); // 字符串转换为字节数组 byte[] b = "黑马程序员".getBytes(); // 写出字节数组数据 fos.write(b); // 关闭资源 fos.close(); 输出结果: 黑马程序员 }

写出指定长度字节数组: write(byte[] b, int off, int len) ,每次写出从off索引开始,len个字节

public static void main(String[] args) throws IOException { // 使用文件名称创建流对象 FileOutputStream fos = new FileOutputStream("fos.txt"); // 字符串转换为字节数组 byte[] b = "abcde".getBytes(); // 写出从索引2开始,2个字节。索引2是c,两个字节,也就是cd。 fos.write(b,2,2); // 关闭资源 fos.close(); 输出结果: cd }

数据追加续写 经过以上的演示,每次程序运行,创建输出流对象,都会清空目标文件中的数据。 public FileOutputStream(File file, boolean append) : 创建文件输出流以写入由指定的 File对象表示的 文件。 public FileOutputStream(String name, boolean append) : 创建文件输出流以指定的名称写入文件。 这两个构造方法,参数中都需要传入一个boolean类型的值, true 表示追加数据, false 表示清空原有数据。 这样创建的输出流对象,就可以指定是否追加续写了。

写出换行 回车符 \r 和换行符 \n : 回车符:回到一行的开头(return)。 换行符:下一行(newline)。 系统中的换行: Windows系统里,每行结尾是 回车+换行 ,即 \r\n ; Unix系统里,每行结尾只有 换行 ,即 \n ; Mac系统里,每行结尾是 回车 ,即 \r 。从 Mac OS X开始与Linux统一。

字节输入流【InputStream】(InputStream 抽象类是表示字节输入流的所有类的超类) public void close() :关闭此输入流并释放与此流相关联的任何系统资源。(当完成流的操作时,必须调用此方法,释放系统资源) public abstract int read() : 从输入流读取数据的下一个字节。 public int read(byte[] b) : 从输入流中读取一些字节数,并将它们存储到字节数组 b中 。

FileInputStream(InputStream子类)

public class FileInputStreamConstructor throws IOException{ public static void main(String[] args) { // 使用File对象创建流对象 File file = new File("a.txt"); FileInputStream fos = new FileInputStream(file); // 使用文件名称创建流对象 FileInputStream fos = new FileInputStream("b.txt"); } }

读取字节 read 方法,每次可以读取一个字节的数据,提升为int类型,读取到文件末尾,返回 -1

public class FISRead { public static void main(String[] args) throws IOException{ // 使用文件名称创建流对象 FileInputStream fis = new FileInputStream("read.txt"); // 读取数据,返回一个字节 int read = fis.read(); System.out.println((char) read); read = fis.read(); System.out.println((char) read); read = fis.read(); System.out.println((char) read); read = fis.read(); System.out.println((char) read); read = fis.read(); System.out.println((char) read); // 读取到末尾,返回‐1 read = fis.read(); System.out.println( read); // 关闭资源 fis.close(); } } 输出结果: a b c d e ‐1

使用字节数组读取: read(byte[] b) ,每次读取b的长度个字节到数组中,返回读取到的有效字节个数,读 取到末尾时,返回 -1

public class FISRead { public static void main(String[] args) throws IOException{ // 使用文件名称创建流对象. FileInputStream fis = new FileInputStream("read.txt"); // 文件中为abcde // 定义变量,作为有效个数 int len ; // 定义字节数组,作为装字节数据的容器 byte[] b = new byte[2]; // 循环读取 while (( len= fis.read(b))!=‐1) { // 每次读取后,把数组变成字符串打印 System.out.println(new String(b)); } // 关闭资源 fis.close(); } } 输出结果: ab cd ed **错误数据 d ,是由于后一次读取时,只读取一个字节 e ,数组中,上次读取的数据没有被完全替换,所以要通 过 len** public class FISRead { public static void main(String[] args) throws IOException{ // 使用文件名称创建流对象. FileInputStream fis = new FileInputStream("read.txt"); // 文件中为abcde // 定义变量,作为有效个数 int len ; // 定义字节数组,作为装字节数据的容器 byte[] b = new byte[2]; // 循环读取 while (( len= fis.read(b))!=‐1) { // 每次读取后,把数组的有效字节部分,变成字符串打印 System.out.println(new String(b,0,len)); // len 每次读取的有效字节个数 } // 关闭资源 fis.close(); } } 输出结果: ab cd e

当使用字节流读取文本文件时,可能会有一个小问题。就是遇到中文字符时,可能不会显示完整的字符,那是因为 一个中文字符可能占用多个字节存储。所以Java提供一些字符流类,以字符为单位读写数据,专门用于处理文本文 件。

字符流

字符输入流Reader(抽象类是表示用于读取字符流的所有类的超类) public void close() :关闭此流并释放与此流相关联的任何系统资源。 public int read() : 从输入流读取一个字符。 public int read(char[] cbuf) : 从输入流中读取一些字符,并将它们存储到字符数组 cbuf中

FileReder类是读取字符文件的便利类。构造时使用系统默认的字符编码和默认字节缓冲区。 (1. 字符编码:字节与字符的对应规则。Windows系统的中文编码默认是GBK编码表。 idea中UTF-8 2. 字节缓冲区:一个字节数组,用来临时存储字节数据。)

public class FileReaderConstructor throws IOException{ public static void main(String[] args) { // 使用File对象创建流对象 File file = new File("a.txt"); FileReader fr = new FileReader(file); // 使用文件名称创建流对象 FileReader fr = new FileReader("b.txt"); } }

读取字符: read 方法,每次可以读取一个字符的数据,提升为int类型,读取到文件末尾,返回 -1

public class FRRead { public static void main(String[] args) throws IOException { // 使用文件名称创建流对象 FileReader fr = new FileReader("read.txt"); // 定义变量,保存数据 int b ; // 循环读取 while ((b = fr.read())!=‐1) { System.out.println((char)b); } // 关闭资源 fr.close(); } } 输出结果: 黑 马 程 序 员

使用字符数组读取: read(char[] cbuf) ,每次读取b的长度个字符到数组中,返回读取到的有效字符个数, 读取到末尾时,返回 -1

public class FRRead { public static void main(String[] args) throws IOException { // 使用文件名称创建流对象 FileReader fr = new FileReader("read.txt"); // 定义变量,保存有效字符个数 int len ; // 定义字符数组,作为装字符数据的容器 char[] cbuf = new char[2]; // 循环读取 while ((len = fr.read(cbuf))!=‐1) { System.out.println(new String(cbuf,0,len)); } // 关闭资源 fr.close(); } } 输出结果: 黑马 程序 员

字符输出流Writer,抽象类是表示用于写出字符流的所有类的超类,将指定的字符信息写出到目的地。 void write(int c) 写入单个字符。 void write(char[] cbuf) 写入字符数组。 abstract void write(char[] cbuf, int off, int len) 写入字符数组的某一部分,off数组的开始索引,len 写的字符个数。 void write(String str) 写入字符串。 void write(String str, int off, int len) 写入字符串的某一部分,off字符串的开始索引,len写的字符个 数。 void flush() 刷新该流的缓冲。 void close() 关闭此流,但要先刷新它。

FileWriter类是写出字符到文件的便利类。构造时使用系统默认的字符编码和默认字节缓冲区。

public class FileWriterConstructor { public static void main(String[] args) throws IOException { // 使用File对象创建流对象 File file = new File("a.txt"); FileWriter fw = new FileWriter(file); // 使用文件名称创建流对象 FileWriter fw = new FileWriter("b.txt"); } }

write(int b) 方法,每次可以写出一个字符数据

public class FileWriterConstructor { public static void main(String[] args) throws IOException { // 使用File对象创建流对象 File file = new File("a.txt"); FileWriter fw = new FileWriter(file); // 使用文件名称创建流对象 FileWriter fw = new FileWriter("b.txt"); } } public class FWWrite { public static void main(String[] args) throws IOException { // 使用文件名称创建流对象 FileWriter fw = new FileWriter("fw.txt"); // 写出数据 fw.write(97); // 写出第1个字符 fw.write('b'); // 写出第2个字符 fw.write('C'); // 写出第3个字符 fw.write(30000); // 写出第4个字符,中文编码表中30000对应一个汉字。 /* 【注意】关闭资源时,与FileOutputStream不同。 如果不关闭,数据只是保存到缓冲区,并未保存到文件。 */ // fw.close(); } } 输出结果: abC田 未调用close方法,数据只是保存到了缓冲区,并未写出到文件中

关闭和刷新 因为内置缓冲区的原因,如果不关闭输出流,无法写出字符到文件中。但是关闭的流对象,是无法继续写出数据 的。如果我们既想写出数据,又想继续使用流,就需要 flush 方法了。 flush :刷新缓冲区,流对象可以继续使用。 close :先刷新缓冲区,然后通知系统释放资源。流对象不可以再被使用了。

写出字符数组 : write(char[] cbuf) 和 write(char[] cbuf, int off, int len) ,每次可以写出字符数 组中的数据,用法类似FileOutputStream

public class FWWrite { public static void main(String[] args) throws IOException { // 使用文件名称创建流对象 FileWriter fw = new FileWriter("fw.txt"); // 字符串转换为字节数组 char[] chars = "黑马程序员".toCharArray(); // 写出字符数组 fw.write(chars); // 黑马程序员 // 写出从索引2开始,2个字节。索引2是'程',两个字节,也就是'程序'。 fw.write(b,2,2); // 程序 // 关闭资源 fos.close(); } }

写出字符串: write(String str) 和 write(String str, int off, int len) ,每次可以写出字符串中的 数据

public class FWWrite { public static void main(String[] args) throws IOException { // 使用文件名称创建流对象 FileWriter fw = new FileWriter("fw.txt"); // 字符串 String msg = "黑马程序员"; // 写出字符数组 fw.write(msg); //黑马程序员 // 写出从索引2开始,2个字节。索引2是'程',两个字节,也就是'程序'。 fw.write(msg,2,2); // 程序 // 关闭资源 fos.close(); } }

字符流,只能操作文本文件,不能操作图片,视频等非文本文件。当我们单纯读或者写文本文件时 使用字符流 其他情况使用字节流


最新回复(0)