Java中,IO流的基本认识以及常用的指令

IO流

字节输入流与字节输出流

定义

字节与字符的不同:字节指计算机语言,是我们所读不懂的,就好比我们打开txt文件,当这个文件是乱码的时候,可简单理解为这就是字节文件,当我们看得懂的,可简单理解为字符文件。

相关的命令

  1. 创建一个txt文件,以字节输出的方式,输入内容:10行hello

    reader是字符输入流抽象类

    writer是字节输出流抽象类

    public class FileOutputStreamDemo02 {
            public static void main(String[] args) throws IOException {
                FileOutputStream fos3 = new FileOutputStream("src\\fos3.txt");
                for (int i = 0; i < 10; i++) {
                fos3.write("hello" .getBytes());
                fos3.write("\n" .getBytes());
    
  2. 将这个字节文件,输出到操作台上显示

               int by;
                while((by = fis.read()) != -1){
                    System.out.println((char) by);
                    by = fis.read();
                }
    
  3. 将D盘下的"C:\Users\street\Desktop\窗里窗外.txt"文件,复制到C盘下的“src\fos3.txt”文件下

    • 先创建出456.txt文件,再读取123.txt文件,将读取的内容,输入到456.txt文件中

    • public class FileOutputStreamDemo02 {
              public static void main(String[] args) throws IOException {
                  
                  //将异常抛出
                  //一个是读取的文件  窗里窗外.txt  ,一个是要输出的文件fos3
                  FileInputStream fis = new FileInputStream("C:\\Users\\street\\Desktop\\窗里窗外.txt");
                  FileOutputStream fos3 = new FileOutputStream("src\\fos3.txt");
                  
                  //抛出异常
                  //读取内内容的同时,输出内容
                              int by;
                  while ((by = fis.read()) != -1){
                      fos3.write(by);
                  }
                  //释放资源
                  fis.close();
                  fos3.close();
              }
      }          
      
    1. 在1这个例子中,是以一次输入一个字节数据,那我们想以一次输入一个字节数组的方式,又会有什么不同。

      public class FileOutputStreamDemo03 {	
      public static void main(String[] args) throws IOException {
              FileInputStream fis = new FileInputStream("src\\fos.txt");
              byte[] bytes = new byte[1024];   //数组长度为1024或1024的整数倍。
              int len
              while ((len =fis.read(bytes)) != -1){
                  System.out.println(new String(bytes,0,len));
              }
          }
      }
      
      • 利用循环读取数组,当读取到de数组的长度为-1时,作为跳出循环的依据。
      • 此处的len表示:读取到的数组长度,当没有数组可读取的时候,len为-1.
      • 数组长度为1024或1024的整数倍,则可输出全部内容。
      • 可这么理解:要读取1024位的数组,但是到了len=-1时,就停止读取。
    2. 利用字节流,复制图片。

      • 根据字节流创建字节对象
      • 根据目的地创建字节对象
      • 读写数据,复制图片,(一次读取一个字节数组,一次写入一个字节数组)
      • 释放内存
      public class FileOutputStreamDemo03 {
          public static void main(String[] args) throws IOException {
      FileOutputStream fos = new FileOutputStream("src\\foe4.png");
              FileInputStream fis = new FileInputStream("C:\\Users\\street\\Desktop\\截屏保存文件\\Snipaste_2022-02-04_14-05-07.png");
              byte[] bytes = new byte[1024];
              int len;
              while ((len = fis.read(bytes)) != -1){
                  fos.write(bytes,0,len);
            }
              fos.close();
              fis.close();
          }
      }
      
      1. 字节缓冲流读取、输出数据

        • 创建字节缓冲流
        • 并写入数据
        public class FileOutputStreamDemo04 {
            public static void main(String[] args) throws IOException {
                //创建字节缓冲输出流
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("src\\fos5.txt"));
                //写数据
                bos.write("hello \r\n".getBytes());
                bos.write("world \r\n".getBytes());
                bos.close();
            }
        }
        
        • 创建字节输入缓冲流,读取数据
        public class FileOutputStreamDemo04 {
            public static void main(String[] args) throws IOException {
                //创建字节缓冲输出流
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("src\\fos5.txt"));
                //写数据
                bos.write("hello \r\n".getBytes());
                bos.write("world \r\n".getBytes());
        
                bos.close();
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("src\\fos5.txt"));
                //一次读取一个字节数据
                int by;
                while((by = bis.read())!= -1){
                    System.out.print((char) by);
                }
                //一次读取一个字节数组
        
                byte[] bytes = new byte[1024];
                int len;
                while((len=bis.read(bytes)) != -1){
                    System.out.println(new String(bytes,0,len));
                }
        
            }
        }
        
  4. 字节流复制视频

    OutputStreamwriter、InputStreamwriter

    OutputStreamwriter是字符流到字节流之间的桥梁;字符输出流

    InputStreamwriter是字节流到字符流之间的桥梁;字符输入流

    使用GBK进行字符流的输入与输出。

    当涉及到解码以及编码的是时候,还是需要OutputStreamwriter、InputStreamwriter

    public static void main(String[] args) throws IOException {
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("src\\fos6.txt"),"GBK");
            osw.write("中国");
            osw.close();
            InputStreamReader isr = new InputStreamReader(new FileInputStream("src\\fos6.txt"),"GBK");
            int a;
            while ((a= isr.read()) != -1){
                System.out.print((char)a);
            }
        }
    }
    

    但是,这并不利于编写程序,太长了,我们可以直接使用其子类进行编写

    Firereader、Firewriter

    new Firereader("文件名");
    相当于替换了
    OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("文件名");
    其余与OutputStreamwriterInputStreamwriter没什么区别
        
    

    字符缓冲流

    Bufferreader、Bufferwriter

    new Bufferreader(Firereader("文件名");
    

    字符缓冲流特有的功能

    • 字符输出流写数据
    new.line;可实现换行,满足windos等不同的系统换行符号不一致的特性
    
    • 字符输入流读数据

    一次只读一行字符

    当没有字符可读时,

    String a = bfr.raeder() = null;char[] ch = new char[] ;
    int len;
     while ((len = isr.read()) != -1){} 
    
    所不同的是,前者a是指读取不到字符的时候,bfr.raeder() = null
        后者指读取不到字符的时候,该字符的位置下标为-1.
         
    

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: JavaIO操作基本程如下: 1. 创建对象:通过File类或其他相关类创建输入或输出对象。 2. 打开:使用输入或输出对象的open()方法打开,这样就可以读取或写入数据。 3. 读取或写入数据:使用输入或输出对象的read()或write()方法读取或写入数据。 4. 关闭:使用输入或输出对象的close()方法关闭,释放资源。 需要注意的是,在使用IO操作时,要遵循“先打开、后关闭”的原则,以确保数据的完整性和的正确性。同时,在操作过程也需要进行异常处理,以避免出现不必要的错误。 ### 回答2: JavaIO基本操作程如下: 1. 打开文件或者建立网络连接:使用File类或者URL类打开文件或者建立网络连接。 2. 创建对象:根据需要选择输入(读取数据)或输出(写入数据),并创建相应的对象。常见的输入有FileInputStream、BufferedReader等,常见的输出有FileOutputStream、BufferedWriter等。 3. 读取或写入数据:使用对象读取或写入数据。对于输入,可以通过调用相关方法(如read()、readline()等)逐个字符或逐行读取数据;对于输出,可以通过调用相应方法(如write()、print()等)逐个字符或逐行写入数据。 4. 关闭:读取或写入完成后,需要关闭文件或网络连接,以释放资源。可以调用对象的close()方法来关闭。 需要注意的是,在处理IO时,应该始终使用try-catch-finally块,以确保在发生异常时能够正确关闭。可以把IO操作放在try块,catch块用于捕获异常,并在finally块关闭。 另外,为了提高IO效率,可以考虑使用缓冲来进行读写操作。缓冲(BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter)可以通过缓冲区将数据从源读入到缓冲区,再从缓冲区写入到目标,提高读写的速度。 以上就是JavaIO基本操作的程。根据实际需要选择合适的对象,并遵循打开、读取/写入、关闭的程,可以实现灵活、高效的IO操作。 ### 回答3: 在JavaIO是用于处理输入输出操作的工具。下面是JavaIO基本操作程: 1. 创建对象:通过使用Java的InputStream和OutputStream类来创建对象。InputStream类用于读取输入,而OutputStream类用于写入输出。 2. 打开:通过使用对象对应的构造函数和方法来打开输入和输出。根据具体情况,可以选择文件、网络或内存来打开。 3. 读取/写入数据:使用对象提供的读取和写入方法来读取和写入数据。例如,使用InputStream的`int read()`方法来读取一个字节的数据,使用OutputStream的`void write(int b)`方法来写入一个字节的数据。 4. 关闭:在读取或写入结束后,必须关闭以释放相关资源。通过调用对象的`close()`方法来关闭。 需要注意的是,在处理异常的时候,我们需要对可能出现的`IOException`进行处理。可以使用try-catch语句块来捕获和处理异常。 程示例: ```java import java.io.*; public class IOExample { public static void main(String[] args) { try { // 1. 创建对象 FileInputStream fis = new FileInputStream("input.txt"); FileOutputStream fos = new FileOutputStream("output.txt"); // 2. 打开 // 3. 读取/写入数据 int data; while ((data = fis.read()) != -1) { fos.write(data); } // 4. 关闭 fis.close(); fos.close(); } catch(IOException e) { e.printStackTrace(); } } } ``` 上述示例,我们创建了一个用于将一个文件的内容拷贝到另一个文件的程序。首先,我们创建了一个FileInputStream对象来读取输入文件的内容,然后创建了一个FileOutputStream对象来写入输出文件。接下来,我们通过循环从输入读取一个字节的数据,并将其写入到输出,直到读取完所有的数据。最后,我们关闭了对象来释放资源。 这就是JavaIO基本操作程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值