首页 > 其他分享 >IO数据流

IO数据流

时间:2022-10-13 19:46:30浏览次数:31  
标签:字节 int void IOException IO 数据流 new throws

IO流

节点流

字节流

文件字节输入流

  1. 凡是以 InputStream 结尾的流,都是以字节的形式向程序输入数据

  1. 继承自 InputStream 的流都是用于向程序中输入数据,且数据的单位为字节(8bit)

基本方法:

 /**
  * 一个字节一个字节往外读,每读取一个字节,就处理一个字节
  * 读取一个字节并以整数的形式返回(0-255)
  * 如果返回 -1 就说明已经到了输入流的末尾
  */
 int read() throws IOException
     
 /**
  * 读取一系列字节并存储到一个数组 buffer
  * 返回实际读取的字节数,如果读取前已到输入流的末尾,则返回 -1
  */
 int read(byte[] buffer) throws IOException
 ​
 /**
  * 读取 length 个字节
  * 并存储到一个字节数组 buffer,从 length 位置开始
  * 返回实际读取的字节数,如果读取前已到输入流的末尾返回 -1
  */
 int read(byte[] buffer,int offset,int length) throws IOException
 ​
 /**
  * 关闭流释放内存资源
  */
 void close() throws IOException
 ​
 /**
  * 跳过 n 个字节不读,返回实际跳过的字节数
  */
 long skip(long n) throws IOException
 ​

案例:

 public class TestFileInputStream {
     public static void main(String[] args) throws IOException {
     // 1 创建FileInputStream 并指定文件路径
         InputStream in=new FileInputStream("d:\\test.txt");
 ​
         byte[] b=new byte[1024];
         while (in.read(b)!=-1){
             System.out.println(new String(b));
        }
         in.close();
 ​
 ​
 } }

输出结果

 test :this is a TEXT

文件字节输出流

基本方法

 /**
  * 向输出流中写入一个字节数据,该字节数据为参数b的低 8 位
  */
 void write(int b) throws IOException
 ​
 /**
  * 将一个字节类型的数组中的数据写入输出流
  */
 void write(byte[] b) throws IOException
     
 /**
  * 将一个字节类型的数组中的从指定位置(off)开始的len个字节写入到输出流
  */
 void write(byte[] b,int off,int len) throws IOException
 ​
 /**
  * 关闭流释放内存资源
  */
 void close() throws IOException
 ​
 /**
  * 将输出流中缓冲的数据全部写出到目的地
  */
 void flush() throws IOException
 ​

案例:

 public class TestFileOutPutStream {
     public static void main(String[] args) {
 //       byte[] b = new byte[3];//第三个字节
         int b;
         try (FileInputStream in = new FileInputStream("D:\\test.txt");
              FileOutputStream out=new FileOutputStream("D:\\test2.txt")) {
 //           while ((in.read(b))!=-1){
 //               System.out.println(new String(b));
 //               out.write(b);
 //           }
             while ((b=in.read())!=-1){
                 out.write(b);
            }
 ​
        }catch (Exception e){
             e.printStackTrace();
        }
    }
 }

字符流

文件字符输入流

基本方法

 /**
  * 读取一个字节并以整数的形式返回(0-255)
  * 如果返回 -1 就说明已经到了输入流的末尾
  */
 int read() throws IOException
     
 /**
  * 读取一系列字节并存储到一个数组 buffer
  * 返回实际读取的字节数,如果读取前已到输入流的末尾,则返回 -1
  */
 int read(byte[] buffer) throws IOException
 ​
 /**
  * 读取 length 个字节
  * 并存储到一个字节数组 buffer,从 length 位置开始
  * 返回实际读取的字节数,如果读取前以到输入流的末尾返回 -1
  */
 int read(byte[] buffer,int offset,int length) throws IOException
 ​
 /**
  * 关闭流释放内存资源
  */
 void close() throws IOException
 ​
 /**
  * 跳过 n 个字节不读,返回实际跳过的字节数
  */
 long skip(long n) throws IOException
 ​

案例

 public class TestFileReader {
     public static void main(String[] args) throws IOException {
         char[] bytes = new char[2];
         FileReader fileReader = new FileReader("D:\\test3.txt");
         while ((fileReader.read(bytes))!=-1){
             System.out.println(new String(bytes));
        }
    }
 }

输出结果:

 你好
 呀!

 

文件字符输出流

基本方法

 /**
  * 向输出流中写入一个字节数据,该字节数据为参数 b 的低 16 位
  */
 void write(int b) throws IOException
 ​
 /**
  * 将一个字节类型的数组中的数据写入输出流
  */
 void write(byte[] b) throws IOException
 ​
 /**
  * 将一个字节类型的数组中的从指定位置(off)开始的 len 个字节写入到输出流
  */
 void write(byte[] b,int off,int len) throws IOException
 ​
 /**
  * 关闭流释放内存资源
  */
 void close() throws IOException
 ​
 /**
  * 将输出流中缓冲的数据全部写出到目的地
  */
 void flush() throws IOException
 ​

案例

 public class TestFileWriter {
     public static void main(String[] args) throws IOException {
 ​
         FileReader reader = new FileReader("D:\\test3.txt");
         FileWriter writer = new FileWriter("D:\\test4.txt");
         int a;
         while ((a=reader.read())!=-1){
             System.out.println((char) a);
             writer.write(a);
        }
         reader.close();
         writer.close();
    }
 }

缓冲流

字节缓冲输入输出流

 public class TestBufferedReaderAndWriter {
     public static void main(String[] args) throws IOException {
         FileInputStream inputStream = new FileInputStream("D:\\test.txt");
         BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
 ​
         FileOutputStream fileOutputStream = new FileOutputStream("D:\\test6.txt");
         BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
 ​
         int a;
         while ((a=bufferedInputStream.read())!=-1){
             System.out.print((char) a);
             bufferedOutputStream.write(a);
        }
         bufferedOutputStream.flush();
 ​
         inputStream.close();
         bufferedInputStream.close();
         fileOutputStream.close();
         bufferedOutputStream.close();
 ​
    }
 }

字符缓冲输入输出流

 public class TestBufferedWriter {
     public static void main(String[] args) throws IOException {
 ​
         FileReader reader = new FileReader("D:\\test3.txt");
         BufferedReader bufferedReader = new BufferedReader(reader);
 ​
         FileWriter writer = new FileWriter("D:\\test5.txt");
         BufferedWriter bufferedWriter = new BufferedWriter(writer);
         
         String s=bufferedReader.readLine();
         while (s!=null){
             System.out.print(s);
             bufferedWriter.write(s);
             s=bufferedReader.readLine();
        }
         //一次写出
         bufferedWriter.flush();
         //关闭通道
         reader.close();
         bufferedReader.close();
         writer.close();
         bufferedWriter.close();
    }
 }
 

标签:字节,int,void,IOException,IO,数据流,new,throws
From: https://www.cnblogs.com/sukidakara/p/16789404.html

相关文章