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

  • 相关阅读:
    通过Xshell连接CentOS虚拟机
    Linux学习笔记
    JAVA学习摘要
    4k 对齐,你准备好了吗?
    图片种子制作方法,利用图片上传附件
    利用京东服务免费打造属于自己的网站
    PE制作实录 —— 补充说明
    PE制作实录 —— 定义我的 PE 工具箱
    浏览器 — 各项基准测试
    Windows 8.1 归档 —— Step 3 软件的选择与安装
  • 原文地址:https://www.cnblogs.com/endust/p/11846040.html
Copyright © 2011-2022 走看看