2011-03-17 29 views
0

我想先使用Java將一串字符串和一串字節寫入文件。由於字節數組,我開始使用FileOutputStream。搜索API後,我意識到FileOutputStream不能寫String s,只有int s和byte s,所以我切換到DataOutputStream。當我運行該程序時,出現異常。爲什麼?如何寫一個字符串序列,然後將一個字節數組寫入文件?

這裏是我的代碼的一部分:

try { 
      // Create the file 
      FileOutputStream fos; 
      DataOutputStream dos; // = new DataOutputStream("compressedfile.ecs_h"); 
      File file= new File("C:\\MyFile.txt"); 
      fos = new FileOutputStream(file); 
      dos=new DataOutputStream(fos); 

      /* saves the characters as a dictionary into the file before the binary seq*/ 
      for (int i = 0; i < al.size(); i++) { 
       String name= al.get(i).name; //gets the string from a global arraylist, don't pay attention to this! 
       dos.writeChars(name); //saving the name in the file 
      } 

      System.out.println("\nIS SUCCESFULLY WRITTEN INTO FILE! "); 

      dos.writeChars("><"); 
      String strseq; 

      /*write all elements from the arraylist into a string variable*/ 
      strseq= seq.toString(); 
      System.out.println("sTringSeq: " + strseq); 

      /*transpose the sequence string into a byte array*/ 
      byte[] data = new byte[strseq.length()/8]; 

      for (int i = 0; i < data.length; i++) { 
       data[i] = (byte) Integer.parseInt(strseq.substring(i * 8, (i + 1) * 8), 2); 
       dos.write(data[i]); 
      } 


      dos.flush(); 
      //Close the output stream 
      dos.close(); 
} catch(Exception e){} 
+9

每當你寫出一個涉及異常的問題,*告訴我們什麼是異常*。 – 2011-03-17 14:45:44

+0

即時通訊抱歉,但我不知道。我正在使用try catch! – elena 2011-03-17 15:18:42

+0

問題是你正在使用空的catch塊,壞主意。您應該打印堆棧跟蹤以瞭解您的異常情況。例如 – javanna 2011-03-17 15:48:34

回答

0

您的代碼的問題是,最後一個for循環計數了錯誤的字節數。下面的代碼解決了將測試數據寫入文件的問題。這適用於我的機器。

public static void main(String[] args) { 

    ArrayList<String> al = new ArrayList<String>(); 
    al.add("String1"); 
    al.add("String2"); 

    try { 
     // Create the file 
     FileOutputStream fos = new FileOutputStream("MyFile.txt"); 
     DataOutputStream dos = new DataOutputStream(fos); 

     /* saves the characters as a dictionary into the file before the binary seq */ 
     for (String str : al) { 
      dos.writeChars(str); 
     } 

     System.out.println("\nIS SUCCESFULLY WRITTEN INTO FILE! "); 

     dos.writeChars("><"); 
     String strseq = "001100111100101000101010111010100100111000000000"; 

     // Ensure that you have a string of the correct size 
     if (strseq.length() % 8 != 0) { 
      throw new IllegalStateException(
        "Input String is cannot be converted to bytes - wrong size: " 
          + strseq.length()); 
     } 

     int numBytes = strseq.length()/8; 
     for (int i = 0; i < numBytes; i++) { 
      int start = i * 8; 
      int end = (i + 1) * 8; 
      byte output = (byte) Integer.parseInt(strseq.substring(start, end), 2); 
      dos.write(output); 
     } 

     dos.writeChars("> Enf of File"); 
     dos.flush(); 
     // Close the output stream 
     dos.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

直接寫入字節測試文件確實有一些問題(我認爲這是一個文本文件,在您的測試文件名以.TXT結尾)的方法,其中最明顯的是,一些文本編輯器不會很好地處理/顯示空字符(您的最後一個測試字節是:00000000或null)。如果您想將字節視爲可讀的字節,那麼您可以使用Base64編碼調查它們的編碼。

+0

謝謝,它正在工作。 – elena 2011-03-17 18:58:51

+0

雖然閱讀這個特定的文件時,它不會將它們識別爲字符串,我的意思是第一部分。你建議什麼? – elena 2011-03-18 01:08:14

0

線:

data[i] = (byte) Integer.parseInt(strseq.substring(i * 8, (i + 1) * 8), 2); 

看起來很可疑......

可以提供約strseq及其價值的舉動細節?

+0

壓力就是這樣:String strseq =「001100111100101000101010111010100100111000000000」; – elena 2011-03-17 15:13:52

+0

你的字符串中只有0和1嗎? – Stephan 2011-03-17 16:01:44

+0

是隻有0和1 – elena 2011-03-17 16:13:00

0

這段代碼怎麼樣?

驗證碼:


     byte[] data = new byte[strseq.length()/8]; 

     for (int i = 0; i < data.length; i++) { 
      data[i] = (byte) Integer.parseInt(strseq.substring(i * 8, (i + 1) * 8), 2); 
      dos.write(data[i]); 
     } 

成爲


     byte[] data = strseq.getBytes(); 
+0

strseq是一個字符串,例如保存「001100111100101000101010111010100100111000000000」。 – elena 2011-03-17 15:15:18

+0

所以不要忘記檢查strseq的長度大於8個字符。 – Stephan 2011-03-17 15:57:54

0

隨着FileWriter類,你有一個文件寫入操作的一個很好的抽象。

可能這個類可以幫助你寫你的文件...

可以通過只此類替代其他OutputStreams。它有你想要在文件中寫入字符串和字節數組的所有方法。

相關問題