IO字符流

 

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();
            }
        }
    }
}

 

  

posted @ 2019-06-16 14:20  殇之弑梦  阅读(153)  评论(0编辑  收藏  举报