zoukankan      html  css  js  c++  java
  • Java I/O操作

    按字节读取读取文件,并且将文件里面的内容写到另外一个文件里面去

    public class CopyBytes {  
        public static void main(String[] args) throws IOException {  
            FileInputStream in = null;  
            FileOutputStream out = null;  
            try {  
                in = new FileInputStream("xanadu.txt");  
                out = new FileOutputStream("outagain.txt");  
                int c;  
     
                while ((c = in.read()) != -1) {  
                    out.write(c);  
                }  
     
            } finally {  
                if (in != null) {  
                    in.close();  
                }  
                if (out != null) {  
                    out.close();  
                }  
            }  
        }  
    }  

    ------------------------------------------------------------------------

    缓冲存储

    public class CopyCharacters {  
        public static void main(String[] args) throws IOException {  
            FileReader inputStream = null;  
            FileWriter outputStream = null;  
     
            try {  
                inputStream = new FileReader("xanadu.txt");  
                outputStream = new FileWriter("characteroutput.txt");  
     
                int c;  
                while ((c = inputStream.read()) != -1) {  
                    outputStream.write(c);  
                }  
            } finally {  
                if (inputStream != null) {  
                    inputStream.close();  
                }  
                if (outputStream != null) {  
                    outputStream.close();  
                }  
            }  
        }  
    } 

     

    ------------------------------------------------------------------------------------

    按行读取

    public class CopyLines {  
        public static void main(String[] args) throws IOException {  
            BufferedReader inputStream = null;  
            PrintWriter outputStream = null;  
     
            try {  
                inputStream =   
                    new BufferedReader(new FileReader("xanadu.txt"));  
                outputStream =   
                    new PrintWriter(new FileWriter("characteroutput.txt"));  
     
                String l;  
                while ((l = inputStream.readLine()) != null) {  
                    outputStream.println(l);  
                }  
            } finally {  
                if (inputStream != null) {  
                    inputStream.close();  
                }  
                if (outputStream != null) {  
                    outputStream.close();  
                }  
            }  
        }  
    }  

    ----------------------------------------------------------------------------

    直接读取文件内容

    public class ScanXan {     
        public static void main(String[] args) throws FileNotFoundException {     
            Scanner s = null;     
            try {     
                s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));     
        
                while (s.hasNext()) {     
                    System.out.println(s.next());     
                }     
            } finally {     
                if (s != null) {     
                    s.close();     
                }     
            }     
        }     
    }  

    本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/751843

  • 相关阅读:
    docker启动centos容器后如何用putty连接
    Nginx——静态资源服务器(一)
    在win10 docker启动的centos容器中安装nginx
    Spring简单实现数据源的动态切换
    fastjson将json字符串中时间戳转化为日期
    springmvc的异常统一处理
    检查性异常和非检查性异常的区别
    前端其他目录
    JavaScript目录
    React目录
  • 原文地址:https://www.cnblogs.com/umgsai/p/3908213.html
Copyright © 2011-2022 走看看