• 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("
    text");
            } 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
    ");
                bw.write("hello"+line);
            } catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

      

  • 相关阅读:
    BZOJ 3506 机械排序臂 splay
    BZOJ 2843 LCT
    BZOJ 3669 魔法森林
    BZOJ 2049 LCT
    BZOJ 3223 文艺平衡树 splay
    BZOJ 1433 假期的宿舍 二分图匹配
    BZOJ 1051 受欢迎的牛 强连通块
    BZOJ 1503 郁闷的出纳员 treap
    BZOJ 1096 ZJOI2007 仓库设计 斜率优化dp
    BZOJ 1396: 识别子串( 后缀数组 + 线段树 )
  • 原文地址:https://www.cnblogs.com/King-boy/p/11031433.html
Copyright © 2020-2023  润新知