IO(老杜)

IO是什么?

   I:input   输入流

   O:output  输出流

输入输出是相对内存而言,向内存中去是输入,从内存出来是输出

字节流:

有的流按照字节读取,每次取1byte,相当于8个二进制位,可以称为万能流,可以读文本、图片、声音、视频。

字符流:

有的流按照字符读取方便txt文本读取,一次一个字符,只能读取文本文件,不能读取word文件

Stream结尾是字节流,Read/Writer结尾的是字符流 都在java.IO.*包下

重点掌握java.io.FileInputStream   java.io.FileOutputStream ,其他的非常相似

输出文件内容

package IO;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileInputStreamText {
    public static void main(String[] args)  {
        FileInputStream inputStream = null;
        try {
            inputStream =  new FileInputStream("src/IO/a.txt");
            byte[] bytes = new byte[1024];
            int count=0;
            while (( count = inputStream.read(bytes)) != -1){
                System.out.print(new String(bytes,0,count));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream == null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}

  复制文件

​
package IO;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Copy {
    public static void main(String[] args)  {
        FileInputStream fis=null;
        FileOutputStream fos=null;
        try {
            fis=new FileInputStream("src/IO/a.txt");
            fos=new FileOutputStream("src/IO/b.txt");

            byte[] bytes =new byte[1024];
            int count =0 ;
            while (( count= fis.read(bytes)) != -1){
                fos.write(bytes,0,count);
            }

            fos.flush();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fis == null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos == null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

​

BufferReader

package IO;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.stream.Stream;

public class BufferReader {
    public static void main(String[] args) {
        FileReader fileReader = null;
        BufferedReader bufferedReader=null;
        try {
            fileReader=new FileReader("src/IO/a.txt");
            bufferedReader = new BufferedReader(fileReader);

            String s=null;
            while ( (s = bufferedReader.readLine()) != null){
                System.out.println(s);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader == null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

InputStreamReaderTest  字节流转字符流

package IO;

import java.io.*;
import java.util.stream.Stream;

public class InputStreamReaderTest {
    public static void main(String[] args) {
        try {
            //字节流
            FileInputStream fileInputStream = new FileInputStream("src/IO/a.txt");
            //字节流转字符流
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
//            字符流
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String lines = bufferedReader.readLine();
            System.out.println(lines);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值