zoukankan      html  css  js  c++  java
  • 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();
                }
            }
        }
    }

      

  • 相关阅读:
    IntellJ IDEA快捷键
    【Java基础】Java 增强型的for循环 for each
    Markdown简易入门
    kafka性能调优
    百度地图 libBaiduMapSDK_base_v4_2_1.so" is 32-bit instead of 64-bit错误
    centos7防火墙firewalld拒绝某ip或者某ip段访问服务器任何服务
    华为策略路由配置
    Windows Server 2012 R2 英文版安装中文语言包教程更改为中文版
    linux修改网卡名为eth0
    华为路由配置IPSec
  • 原文地址:https://www.cnblogs.com/King-boy/p/11031433.html
Copyright © 2011-2022 走看看