IO字符流

本文详细介绍了使用Java进行文件读写的多种方法,包括FileWriter、FileReader、BufferedReader和BufferedWriter的基本用法和性能对比。通过示例代码展示了如何创建、写入和读取文本文件,并对不同缓冲策略的影响进行了分析。

 

package base;

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

public class FileWriterDemo {
    public static void main(String[] args) {
        FileWriter fw=null;
        try {
            fw=new FileWriter("D:\\helo.txt",true);
            fw.write("text");
            
            fw.write("\r\ntext");
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            
            try {
                if(fw!=null) {
                    fw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("end");
    }
}
package base;

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

public class FileReaderDemo {
    public static void main(String[] args) {
        FileReader reader=null;
        try {
            reader=new FileReader("D:\\helo.txt");
            char[] buffer=new char[3];
//            while(true) {
//                int length=reader.read(buffer);
//                if(length==-1) {
//                    break;
//                }else {
//                    System.out.print(new String(buffer,0,length));
//                }
//            }
            int len=-1;
            while((len=reader.read(buffer))!=-1) {
                System.out.print(new String(buffer,0,len));
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(reader!=null) {
                    reader.close();
                    
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

package base;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;

import org.junit.jupiter.api.Test;

public class BufferedWriterDemo2 {
    public static void main(String[] args) throws IOException {
        int size=1024;
        for(int i=1;i<=1024*2;i*=2) {
            long start=System.currentTimeMillis();
            BufferedWriter writer=new BufferedWriter(new FileWriter("D:\\helo.txt",false),i*size);
            for(int j=0;j<10000000;j++) {
                writer.write("fja中fjlkds");
            }
            writer.close();
            long duration = System.currentTimeMillis()-start;
            System.out.println(i+"k:"+duration);
        }
    }
    /**
     * 单元测试
     * @throws IOException
     */
    @Test
    public void readFile() throws IOException {
        FileReader reader=new FileReader("D:\\\\helo.txt");
        long start=System.currentTimeMillis();
        int c=-1;
        while((reader.read())!=-1) {
        }
        reader.close();
        System.out.println(System.currentTimeMillis()-start);
    }
    
    /**
     * 测试BufferedReaderTest(){
     * @throws IOException 
     * 
     */
    @Test
    public void readFileWithBuffered() throws IOException {
        BufferedReader br=new BufferedReader(new FileReader("D:\\helo.txt"));
        long start=System.currentTimeMillis();
        char[] cbuf=new  char[110000000];
        br.read(cbuf);
        br.close();
        System.out.println(System.currentTimeMillis()-start);
    }
    
    /**
     * 测试BufferedReader.readLine
     * @throws IOException 
     */
    @Test
    public void readLineFileWithBuffered() throws IOException {
        BufferedReader br=new BufferedReader(new FileReader("D:\\helo.txt"));
        String line=null;
        while(((line=br.readLine())!=null)) {
            System.out.println(line);
        }
        br.close();
    }
    
    /**
     * 测试LineNumberReaderTest
     * @throws IOException 
     */
    @Test
    public void lineNumberReaderTest() throws IOException {
        LineNumberReader reader=new LineNumberReader(new FileReader("D:\\helo.txt"));
        String line=null;
        while((line=reader.readLine())!=null) {
            System.out.println(reader.getLineNumber());
        }
        reader.close();
    }
    
}
package base;

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

public class BufferedWriterDemo {
    public static void main(String[] args) {
        BufferedWriter bw=null;
        try {
            String line=System.getProperty("line.separator");
            bw=new BufferedWriter(new FileWriter("D:\\helo.txt",false));
            bw.write("hello"+line);
            bw.write("hello"+line);
            bw.write("hello"+line);
            bw.write("hello\n");
            bw.write("hello"+line);
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

  

转载于:https://www.cnblogs.com/King-boy/p/11031433.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值