2012-11-19 54 views
0

該程序在終端窗口中給我一個「例外」。 prevData.txt的位置如代碼所示。我的問題是什麼?文件輸出/輸入異常

我正在使用Bluej。還有其他問題嗎?請問。

import java.io.*; 

public class prevDataReset{ 

public static void main(String args[]){ 

try{ 
    byte bWrite [] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 

    OutputStream os = new FileOutputStream("C:/prevData.txt"); 
    for(int x=0; x < bWrite.length ; x++){ 
    os.write(bWrite[x]); // writes the bytes 
    } 
    os.close(); 

    InputStream is = new FileInputStream("C:/prevData.txt"); 
    int size = is.available(); 

    for(int i=0; i< size; i++){ 
    System.out.print((double)is.read() + " "); 
    } 
    is.close(); 
    }catch(IOException e){ 
    System.out.print("Exception"); 
    } 
    } 
    } 
+0

打印stacktrace而不是「Exception」...它會更有幫助。 – mre

+1

你應該打印e.getMessage(),這可能會得心應手。我看到的第一個紅旗是windows路徑使用\而不是/。試試看。然後嘗試將prevData放置在與JavaScript文件相同的文件夾中並使其成爲相對路徑 –

+1

而不是'System.out.print(「Exception」);',請使用'e.printStackTrace();'然後發佈跟蹤。 –

回答

2

它適用於我。我建議你不必寫寫權限「C:\」,請嘗試使用user.dir系統屬性,而不是...

喜歡的東西...

public class SimpleTest { 

    public static void main(String args[]) { 

     OutputStream os = null; 
     InputStream is = null; 
     try { 
      String userDir = System.getProperty("user.dir"); 
      byte bWrite[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 

      try { 
       os = new FileOutputStream(userDir + File.separator + "prevData.txt"); 
       for (int x = 0; x < bWrite.length; x++) { 
        os.write(bWrite[x]); // writes the bytes 
       } 
      } finally { 
       try { 
        os.close(); 
       } catch (Exception e) { 
       } 
      } 

      try { 
       is = new FileInputStream(userDir + File.separator + "prevData.txt"); 
       int size = is.available(); 

       for (int i = 0; i < size; i++) { 
        System.out.print((double) is.read() + " "); 
       } 
      } finally { 
       try { 
        is.close(); 
       } catch (Exception e) { 
       } 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

謝謝,它工作得很好 – tiffanyButterfly95

+0

@ user1834165,別忘了接受答案 –

0

好,執行此代碼和改變catch塊爲e.printStackTrace();,而不是僅僅System.out.println("Exception");產生以下錯誤:

java.io.FileNotFoundException: C:\prevData.txt (Access is denied) 
    at java.io.FileOutputStream.open(Native Method) 
    at java.io.FileOutputStream.<init>(FileOutputStream.java:179) 
    at java.io.FileOutputStream.<init>(FileOutputStream.java:70) 
    at fileexception.prevDataReset.main(prevDataReset.java:11) 

確保目錄是讀/寫,可以再試一次。

0

我只是想

 

import java.io.*; 

public class prevDataReset { 
    public static void main(String args[]){ 

     try{ 
       byte bWrite [] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 

       OutputStream os = new FileOutputStream("C:/prevData.txt"); 
       for(int x=0; x < bWrite.length ; x++){ 
       os.write(bWrite[x]); // writes the bytes 
       } 
       os.close(); 

       InputStream is = new FileInputStream("C:/prevData.txt"); 
       int size = is.available(); 

       for(int i=0; i < size; i++){ 
       System.out.print((double)is.read() + " "); 
       } 
       is.close(); 
      } 
      catch(IOException e){ 
      System.out.print("Exception"); 
      } 
    } 
} 

 

,它工作正常。
問題不在於這段代碼。

希望這有助於。