Java IO操作


分类

  1. 输入流:将存储设备中的数据写入内存中。

  2. 输出流:将内存中的数据写入存储设备中。

  3. 字节流:以字节为单位,可以读写所有文件

  4. 字符流:以字符为单位,只能读写文本文件。

字节流

  1. InputStream:所有输入字节流的超类
    主要方法

    1. close();
    2. mark();在此输入流中标记当前位置
    3. markSupported();测试此输入流是否支持mark
    4. read();从输入流中读取下一个字节,该方法还有其他参数
    5. reset();将此流重新定位到上一次调用mark方法的地方
    6. skip(long n)跳过或者丢弃此流中的N个字节
  2. OutputStream:所有输出字节流的超类
    主要方法:

    1. close();
    2. flush();刷新此输入流并写出缓存中的数据
    3. write();写入操作

字符流

  1. Reader:所有输入字符流的基类
    主要方法:

    1. read(),读取一个字符,返回值为读取的字符
    2. read(char[] a)throws IOException,读取一系列的字符到a中,返回值为读取的字符的数量。
    3. abstract read(char[] a,int off,int len),读取len个字符,从a的off下标处开始写入
  2. Writer:所有输出字符流的基类

    主要方法:

    1. write(),写单个字符

Java的IO流根据是否直接处理数据,又分为节点流和处理流。节点流直接处理数据,处理流是对一个已存在的节点流的连接和封装。

节点流

  1. 文件流:FieInputStream,FileOutputStream,FileWriter,FileReader. 他们都会直接操作文件,和OS底层互动,在使用完成之后,需要关闭流对象,因为Java垃圾回收机制不会主动回收。
  2. 数组流:ByteArrayInputStream,ByteArrayOutputStream,CharArrayReader,CharArrayWriter,对数组进行处理的节点流。
  3. 字符串流:StringWriter,StringReader,其中StringReader能够从String字符串中读取字符然后保存在char数组中。
  4. 管道流:PipedInputStream,PipedOutputStream,PipedReader,PipedWriter,对管道进行处理的管道流。

处理流

处理流的构造方法中总是要带一个节点流的对象作为参数

  1. 缓冲流:BufferedImputStrean,BufferedOutputStream,BufferedReader ,BufferedWriter,需要父类作为构造参数,增加缓冲功能,避免频繁读写硬盘。可以初始化缓冲数据的大小。另外,BufferedReader提供了一次读一行的方法radeLine(),而FileInputStream和FileReader只能读取一个字符或者一个字。

  2. 转换流:InputStreamReader,OutputStreamWriter,要inputStream或者OutputStream作为参数,实现从字节流到字符流的转换

  3. 数据流:DataInputStream,DataOutputStream,提供将基础数据类型写入到文件中,或者读取出来。

常见用法

  1. 读取键盘输入,打印到控制台
PrintWriter out = null;
 BufferedReader br = null;
 try{
 System.out.println("请输入:");
 out = new PrintWriter(System.out, true);
 br = new BufferedReader(new InputStreamReader(System.in));
 String line = null;
 while ((line = br.readLine()) != null) {
 if (line.equals("exit")) {
 System.exit(1);
 }
 out.println(line);
 }
 } catch (IOException e) {
 e.printStackTrace();
 }finally{
 out.close();
 br.close();
 }
  1. 用字节流读写文件

因为是用字节流来读媒介,所以对应的流是 InputStream 和 OutputStream,并且媒介对象是文件,所以用到子类是 FileInputStream 和 FileOutputStream,这里还可以通过 BufferedInputStream 用缓冲流来读取文件。

InputStream is =null;
 OutputStream os = null;
 try {
 // 在try()中打开文件会在结尾自动关闭
 is = new FileInputStream("D:/FileInputStreamTest.txt");
 os = new FileOutputStream("D:/FileOutputStreamTest.txt");
 byte[] buf = new byte[4];
 int hasRead = 0;
 while ((hasRead = is.read(buf)) > 0) {
 os.write(buf, 0, hasRead);
 }
 System.out.println("write success");
 } catch (Exception e) {
 e.printStackTrace();
 }finally{
 os.close();
 is.close();
 }
  1. 用字符流读写文件

使用FileReader和FileWriter

// 在try() 中打开的文件, JVM会自动关闭
 public static void readAndWriteCharToFile() throws IOException{
 Reader reader = null;
 Writer writer =null;
 try {
 File readFile = new File("d:/FileInputStreamTest.txt");
 reader = new FileReader(readFile);
 File writeFile = new File("d:/FileOutputStreamTest.txt");
 writer = new FileWriter(writeFile);
 char[] byteArray = new char[(int) readFile.length()];
 int size = reader.read(byteArray);
 System.out.println("大小:" + size + "个字符;内容:" + new String(byteArray));
 writer.write(byteArray);
 } catch (Exception e) {
 e.printStackTrace();
 }finally{
 reader.close();
 writer.close();
 }
 }

使用StringReader和StringWriter

StringReader sr =null;
 StringWriter sw =null;
 try {
 String str = "学习不刻苦
" + "不如卖红薯;
";
 char[] buf = new char[32];
 int hasRead = 0;
 // StringReader将以String字符串为节点读取数据
 sr = new StringReader(str);
 while ((hasRead = sr.read(buf)) > 0) {
 System.out.print(new String(buf, 0, hasRead));
 }
 // 由于String是一个不可变类,因此创建StringWriter时,实际上是以一个StringBuffer作为输出节点
 sw = new StringWriter();
 sw.write("黑夜给了我黑色的眼睛
");
 sw.write("我却用它寻找光明
");
 // toString()返回sw节点内的数据
 System.out.println(sw.toString());
 } catch (Exception e) {
 e.printStackTrace();
 }finally{
 sw.close(); 
 sr.close(); 
 }
  1. 字节流转化为字符流

在3中使用字符流读文件时打印到控制台会产生乱码,但是使用转化流可以解决这个问题。

InputStream is =null;
 Reader reader = null;
 try {
 File file = new File("d:/FileInputStreamTest.txt");
 is = new FileInputStream(file);
 reader = new InputStreamReader(is,"gbk");
 char[] byteArray = new char[(int) file.length()];
 int size = reader.read(byteArray);
 System.out.println("大小:" + size + ";内容:" + new String(byteArray));
 } catch (Exception e) {
 e.printStackTrace();
 }finally{
 reader.close();
 is.close();
 }

以上参考自https://www.cnblogs.com/CQqf/p/10795656.html

posted @ 2020-06-12 14:03  两小无猜  阅读(111)  评论(0编辑  收藏  举报