一个解决java.io.EOFException的方法

 

 

纪录一下解决java.io.EOFException的方法

Code:
  1. package onlyfun;   
  2.   
  3. import java.io.*;   
  4. import java.util.*;   
  5.   
  6. public class Test_5 {   
  7.        
  8.     List list = new ArrayList();   
  9.     DataOutputStream dos = null;   
  10.     OutputStream out = null;   
  11.     DataInputStream dis = null;   
  12.     InputStream is = null;   
  13.        
  14.     public static void main(String[] args) {   
  15.         new Test_5();   
  16.     }   
  17.        
  18.     public Test_5() {   
  19.         list.add("first");   
  20.         list.add("second");   
  21.         list.add("third");   
  22.            
  23.         write();   
  24.         List ret = read();   
  25.         Iterator it = ret.iterator();   
  26.         while(it.hasNext()) {   
  27.             String temp = (String)it.next();   
  28.             System.out.println(temp);   
  29.         }   
  30.     }   
  31.        
  32.     public void write() {   
  33.         try {   
  34.             out = new FileOutputStream("test.txt");   
  35.             dos = new DataOutputStream(out);   
  36.                
  37.             for(int i=0; i
  38.                 String str = list.get(i);   
  39.                 dos.writeUTF(str);   
  40.             }   
  41.                
  42.             if(out != null) {   
  43.                 out.close();   
  44.                 out = null;   
  45.             }   
  46.         } catch (FileNotFoundException e1) {   
  47.             e1.printStackTrace();   
  48.         } catch (IOException e) {   
  49.             e.printStackTrace();   
  50.         }   
  51.     }   
  52.        
  53.     public List read() {   
  54.         List ret = new ArrayList();    
  55.            
  56.         try {   
  57.             is = new FileInputStream("test.txt");   
  58.             dis = new DataInputStream(is);   
  59.   
  60.             String temp = null;   
  61.             temp = dis.readUTF();   
  62.             while(temp != null) {   
  63.                 ret.add(temp);   
  64.                 if(dis.available()>0) {   
  65.                     temp = dis.readUTF();   
  66.                 }   
  67.                 else {   
  68.                     break;   
  69.                 }   
  70.             }   
  71.                
  72.         } catch (FileNotFoundException e) {   
  73.             e.printStackTrace();   
  74.         } catch (IOException e) {   
  75.             e.printStackTrace();   
  76.         }      
  77.         return ret;   
  78.     }   
  79.   
  80. }   

以下为出错时的代码:read()方法

 

Code:
  1.     public List read() {   
  2.         List ret = new ArrayList();    
  3.            
  4.         try {   
  5.             is = new FileInputStream("test.txt");   
  6.             dis = new DataInputStream(is);   
  7.   
  8.             String temp = null;   
  9.             temp = dis.readUTF();   
  10.             while(temp != null) {   
  11. //              ret.add(temp);   
  12. //              if(dis.available()>0) {   
  13. //                  temp = dis.readUTF();   
  14. //              }   
  15. //              else {   
  16. //                  break;   
  17. //              }   
  18.                 ret.add(temp);   
  19.                 temp = dis.readUTF();   
  20.             }   
  21.                
  22.         } catch (FileNotFoundException e) {   
  23.             e.printStackTrace();   
  24.         } catch (IOException e) {   
  25.             e.printStackTrace();   
  26.         }      
  27.         return ret;   
  28.     }  

 

 

 

 

你可能感兴趣的:(一个解决java.io.EOFException的方法)