• java基础之I/O操作


    字节流

    直接上代码:

    import java.io.*;
    
    class Test{
        public static void main(String[] args){
            FileInputStream inputfile = null;
            FileOutputStream outputfile = null;
            try{
                inputfile = new FileInputStream("./input.txt");
                outputfile = new FileOutputStream("./output.txt");
                byte[] buffer = new byte[100];
                int temp = inputfile.read(buffer,0,buffer.length);
                String s = new String(buffer);
                s = s.trim();
                System.out.println(s);
                outputfile.write(buffer,0,temp);
                
            }
            catch(Exception e){
                System.out.println(e);
            }
            
        }
    }

    优化版:

    通过循环1M 1M读取文件

    import java.io.*;
    
    class Test{
        public static void main(String[] args){
            FileInputStream inputfile = null;
            FileOutputStream outputfile = null;
            try{
                inputfile = new FileInputStream("./input.txt");
                outputfile = new FileOutputStream("./output.txt");
                byte[] buffer = new byte[1024];
                while(true){
                    int temp = inputfile.read(buffer,0,buffer.length);
                    if(temp == -1){
                        break;
                    }
                    outputfile.write(buffer,0,temp);
                    
                }
                
                
            }
            catch(Exception e){
                System.out.println(e);
            }
            finally{
                try{
                    inputfile.close();
                    outputfile.close();
                }
                catch(Exception e){
                    System.out.println(e);    
                }
                
            }
            
        }
    }

    字符流

    import java.io.*;
    
    class TestString{
        public static void main(String[] args){
            FileReader inputfile = null;
            FileWriter outputfile = null;
            try{
                inputfile = new FileReader("./input.txt");
                outputfile = new FileWriter("./output.txt");
                char[] buffer = new char[1024];
                while(true){
                    int temp = inputfile.read(buffer,0,buffer.length);
                    if(temp == -1){
                        break;
                    }
                    outputfile.write(buffer,0,temp);
                    
                }
                
                
            }
            catch(Exception e){
                System.out.println(e);
            }
            finally{
                try{
                    inputfile.close();
                    outputfile.close();
                }
                catch(Exception e){
                    System.out.println(e);    
                }
                
            }
            
        }
    }

     BufferedReader.readLine

    import java.io.*;
    
    class TestBufferReader{
        public static void main(String[] args){
            FileReader fileReader = null;
            BufferedReader bufferReader = null;
            try{
                fileReader = new FileReader("./input.txt");
                bufferReader = new BufferedReader(fileReader);
                String line =null;
                while(true){
                    line = bufferReader.readLine();
                    if(line == null){
                        break;
                    }
                    System.out.println(line);
                }
            }
            catch(Exception e){
                System.out.println(e);
            }
            
        }
    }

  • 相关阅读:
    HTTPS安全超文本传输协议
    前端性能监控工具
    跨域:跨域及解决方法
    源码安装nginx
    浏览器工作原理(四):浏览器事件解读
    浏览器工作原理(三):js运行机制及Event Loop
    前端性能优化:gzip压缩文件传输数据
    Vue源码学习(零):内部原理解析
    netsh命令操作ipsec
    netsh命令操作防火墙
  • 原文地址:https://www.cnblogs.com/endust/p/11846040.html
Copyright © 2020-2023  润新知