Java IO流0630

1.利用IO流做文件复制

一边读一边写

 

 

package leetcode0606.IO;

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

/**
 * 利用 FileInputStream + FileOutputStream 完成文件的拷贝
 * 使用字节流拷贝文件时,文件类型没规定,万能的。
 */
public class Copy01 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("C:\\Users\\DELL\\Desktop\\大三下\\1\\简历-苑子奇(1).pdf");
            fos = new FileOutputStream("C:\\Users\\DELL\\Desktop\\22.pdf");

            byte[] bytes = new byte[1024 * 1024];
            int readCount = 0;
            while ((readCount = fis.read(bytes))!=-1){
                fos.write(bytes,0,readCount);
            }
            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();
                }
            }
        }
    }

}

2. FileReader

package leetcode0606.IO;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * 文件字符输入流 只能读取普通文本
 *  按读取字符的方式读取数据(中文也是一个字符)
 */
public class FileReaderTest01 {
    public static void main(String[] args) {
        FileReader reader = null;
        try {
            reader = new FileReader("testFile1");

            char[] chars = new char[4]; //一次读取4个字符
            int readCount = 0;
            while((readCount=reader.read(chars))!=-1){
                System.out.println(new String(chars,0,readCount));
                //System.out.println(readCount);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

3.FileWriter

package leetcode0606.IO;

import java.io.FileWriter;
import java.io.IOException;

/**
 * FileWriter
 * 只能输出普通文本
 * 文件字符输出流
 */
public class FileWriterTest01 {
    public static void main(String[] args) {
        FileWriter writer = null;
        try {
            //若不存在会自动创建
            writer = new FileWriter("fileWriterTestFile",true);


            char[] chars = {'我','时','你'};
            writer.write(chars);
            writer.write("\n");

            //可以直接传String
            writer.write("123");

            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

4.利用 FileReader + FileWriter 拷贝文件

package leetcode0606.IO;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * java文件也是普通文件
 * 能被记事本打开的都是普通文件
 */
public class Copy02 {
    public static void main(String[] args) {
        FileReader reader = null;
        FileWriter writer = null;

        try {
            //读
            reader = new FileReader("fileWriterTestFile");
            //写
            writer = new FileWriter("C:\\Users\\DELL\\Desktop\\test.txt");

            char[] chars = new char[4];  // new char[1024 * 512] 是 1MB ,因为一个字符是两个字节
            int readCount = 0;
            //往数组里读
            while((readCount = reader.read(chars))!=-1){
                writer.write(chars,0,readCount);
            }
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (writer == null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

5.带有缓冲区的IO流(缓冲区代替了数组)

package leetcode0606.IO;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * 自带缓冲区
 */
public class BufferedReaderTest01 {
    public static void main(String[] args) {
        BufferedReader br = null;
        FileReader reader = null;
        //构造方法传的流是节点流  , 外面的是包装流(处理流)
        try {
            reader = new FileReader("myFile");
            br = new BufferedReader(reader);
//            System.out.println(br.readLine());
//            System.out.println(br.readLine());
            String s = null;
            //读到没有数据时,返回null
            while((s = br.readLine())!=null){
                System.out.println(s);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        try {
            //关闭流,只需要关闭包装流,以内源代码里把节点流关闭了
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

====================================

源文件
abcdabcdababcdababcdab我是一个中国人!!1abcdab我是一个中国人!!1

dadadadad


====================================
结果

abcdabcdababcdababcdab我是一个中国人!!1abcdab我是一个中国人!!1

dadadadad

Process finished with exit code 0

此方法 不识别换行符 

6.转换流

package leetcode0606.IO;

import java.io.*;
import java.nio.Buffer;

public class BufferedReaderTest02 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        BufferedReader reader = null;

        try {
            fis = new FileInputStream("myFile");
            // 转换流
            reader = new BufferedReader(new InputStreamReader(fis));
            String s = null;
            while((s=reader.readLine())!=null){
                System.out.println(s);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
package leetcode0606.IO;

import java.io.*;

public class BufferedWriterTest01 {
    public static void main(String[] args) {
        BufferedWriter writer = null;
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("myFile",true);
            writer = new BufferedWriter(new OutputStreamWriter(fos));

            writer.write("hahahhahahahahah");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值