现在的位置: 首页 > 综合 > 正文

Java I/O操作

2018年04月09日 ⁄ 综合 ⁄ 共 2780字 ⁄ 字号 评论关闭

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

  1. public class CopyBytes {  
  2.     public static void main(String[] args) throws IOException {  
  3.         FileInputStream in = null;  
  4.         FileOutputStream out = null;  
  5.         try {  
  6.             in = new FileInputStream("xanadu.txt");  
  7.             out = new FileOutputStream("outagain.txt");  
  8.             int c;  
  9.  
  10.             while ((c = in.read()) != -1) {  
  11.                 out.write(c);  
  12.             }  
  13.  
  14.         } finally {  
  15.             if (in != null) {  
  16.                 in.close();  
  17.             }  
  18.             if (out != null) {  
  19.                 out.close();  
  20.             }  
  21.         }  
  22.     }  
  23. }  

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

缓冲存储

  1. public class CopyCharacters {  
  2.     public static void main(String[] args) throws IOException {  
  3.         FileReader inputStream = null;  
  4.         FileWriter outputStream = null;  
  5.  
  6.         try {  
  7.             inputStream = new FileReader("xanadu.txt");  
  8.             outputStream = new FileWriter("characteroutput.txt");  
  9.  
  10.             int c;  
  11.             while ((c = inputStream.read()) != -1) {  
  12.                 outputStream.write(c);  
  13.             }  
  14.         } finally {  
  15.             if (inputStream != null) {  
  16.                 inputStream.close();  
  17.             }  
  18.             if (outputStream != null) {  
  19.                 outputStream.close();  
  20.             }  
  21.         }  
  22.     }  
  23. }  

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

按行读取

  1. public class CopyLines {  
  2.     public static void main(String[] args) throws IOException {  
  3.         BufferedReader inputStream = null;  
  4.         PrintWriter outputStream = null;  
  5.  
  6.         try {  
  7.             inputStream =   
  8.                 new BufferedReader(new FileReader("xanadu.txt"));  
  9.             outputStream =   
  10.                 new PrintWriter(new FileWriter("characteroutput.txt"));  
  11.  
  12.             String l;  
  13.             while ((l = inputStream.readLine()) != null) {  
  14.                 outputStream.println(l);  
  15.             }  
  16.         } finally {  
  17.             if (inputStream != null) {  
  18.                 inputStream.close();  
  19.             }  
  20.             if (outputStream != null) {  
  21.                 outputStream.close();  
  22.             }  
  23.         }  
  24.     }  
  25. }  

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

直接读取文件内容

 

  1. public class ScanXan {     
  2.     public static void main(String[] args) throws FileNotFoundException {     
  3.         Scanner s = null;     
  4.         try {     
  5.             s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));     
  6.     
  7.             while (s.hasNext()) {     
  8.                 System.out.println(s.next());     
  9.             }     
  10.         } finally {     
  11.             if (s != null) {     
  12.                 s.close();     
  13.             }     
  14.         }     
  15.     }     
  16. }  

 

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

抱歉!评论已关闭.