java流的使用

在这里插入图片描述

FileInputStream/FileOutputStream的使用

    public static void main(String [] agrs){
        //创建写入目标文件
        File file=new File("E:\\112.txt");
        FileInputStream fileInputStream=null;
        FileOutputStream fileOutputStream= null;
        try {
            //获取文件输入流
            fileInputStream=new FileInputStream(new File("E:\\11.txt"));
            fileOutputStream = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            int count;
            //fileInputStream.read() 读取方法,一次读取一个字节
            while((count=fileInputStream.read())>-1){
                System.out.println((char)count);
                //目标文件写入
                fileOutputStream.write(count);
            }
            //强制刷新,输出缓冲区中的内容,否则会导致丢失数据
             fileOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                fileInputStream.close();;
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

FileInputStream常用方法

构造方法

public FileInputStream(File file)

通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。

public FileInputStream(String name)

通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。

public abstract int read() throws IOException:

一次读取一个字节,返回下一个数据的字节;如果已发文件末尾,则返回-1

public int read(byte[] b) throws IOException:

byte[] buff = new byte [1024];
一次读取一个字节数组,指定数据的长度是1024的倍数,使用缓冲数组,可以有效的减少对硬件的io读写次数.

public void close() throws IOException

关闭此文件输入流并释放与此流有关的所有系统资源。

FileOutpurStream 常用方法

构造方法

public FileOutputStream(File file) throws FileNotFoundException

创建一个向指定 File 对象表示的文件中写入数据的文件输出流。

public FileOutputStream(File file,boolean append)throws FileNotFoundException

append==true 从文件末尾写入

public void write(int b) throws IOException:

一次写一个字节 b- 要写入的字节。

public void write(byte[] b) throws IOException:

一次写一个字节数组

public void write(byte[] b, int off,int len) throws IOException:

一次写一部分字节数组

public void flush() throws IOException

强制刷新缓冲区内容,防止数据丢失

public void close()throws IOException

关闭此文件输出流并释放与此流有关的所有系统资源。如果是包含在缓冲流中则不需要关闭,关闭缓冲流就会自动关闭.

OutputStreamWriter/InputStreamReader 读取文件

public static void main(String [] agrs){
        //创建写入目标文件
        File file=new File("E:\\360MoveData\\Users\\Administrator\\Desktop\\work\\版本管理\\112.txt");
        InputStream inputStream=null;
        if(file.exists()){
            file.delete();
        }
        Reader reader=null;
        Writer writer= null;
        try {
        	/读入字节流
            inputStream=new FileInputStream("E:\\360MoveData\\Users\\Administrator\\Desktop\\work\\版本管理\\11.txt");
            //获取文件输入流,使用FieldWriter/FileReader父类. InputStreamReader/OutputStreamWriter 指定编码集,否者读出乱码,
            reader=new InputStreamReader(inputStream,"GB2312");
            //写入文件的编码格式默认为系统个人,cmd->chcp 可查看系统默认编码格式
            writer = new OutputStreamWriter(new FileOutputStream(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            int count;
            //可以使用缓冲数组
            //char[] buff = new char[1024];
            while((count=reader.read())>-1){
                System.out.println((char)count);
                //目标文件写入
                writer.write(count);
            }
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                writer.close();;
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

注:OutputStreamWriter是从字符流到字节流的桥接,通过指定的字符集把流中的字符编码成字节输出到字节流中.会频繁调用转换器,可以使用BufferedWriter缓冲字符流提高效率.
public void write(int var1) throws IOException

写入一个字符

public void write(String var1) throws IOException

写入一个字符串

public void write(char[] var1) throws IOException

写入一个字符数组

public abstract void write(char[] var1, int var2, int var3) throws IOException

写入一个字符数组的部分

public void write(String var1, int var2, int var3) throws IOException

写入一个字符串的部分

public abstract void flush() throws IOException;

强制刷新缓冲区

缓冲流

    public static void main(String agrs []) throws IOException {
        //获取文件输入流
        //读入的缓冲区
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(new FileInputStream("E:\\360MoveData\\Users\\Administrator\\Desktop\\work\\版本管理\\11.txt"),"GBK"));
        //写入的缓冲区
        BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("E:\\360MoveData\\Users\\Administrator\\Desktop\\work\\版本管理\\112.txt")));
        String line=bufferedReader.readLine();
        String flag="true";
        while(line!=null){
            System.out.println(line);
            if("true".equals(flag)){
                bufferedWriter.write(line);
                flag="false";
            }else{
               //读取整行,不会读取换行符
                bufferedWriter.write("\r\n"+line);
            }
            line=bufferedReader.readLine();
        }
        bufferedWriter.flush();
        bufferedWriter.close();
        bufferedReader.close();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值