2016-04-06 61 views
0

我對Java很新,我正在做一些練習。我只是簡單地處理簡單的IO東西,從文件讀取然後打印到輸出。我也玩過從輸入中刪除逗號,並讓這部分工作。我的問題是當我嘗試將數組中的值打印到新文件中時。是否我的數組是在BufferedReader的while循環內創建的?如何獲取數組values中的值可以被BufferedWriter看到?我也很好奇,在打印到輸出文件時如何去顛倒數組的順序?感謝任何幫助的人,我已經搜索,但沒有能夠遇到任何具體的事情。傳遞字符串數組的Java

代碼:

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStreamWriter; 
import java.io.Writer; 
import java.io.FileWriter; 

public class Main { 

    public static void main(String[] args) throws Exception { 
     BufferedReader br = new BufferedReader(new FileReader("myImport.txt")); 
     String line = null; 

     while ((line = br.readLine()) != null) { 
      String[] values = line.split(","); 
      System.out.println("Your file has been evaluated, here are the contents:"); 
      for (String str : values) { 
       System.out.println(str); 
      } 
     } 
     br.close(); 

     try { 

      File file = new File("myOutput.txt"); 

      // if file doesnt exists, then create it 
      if (!file.exists()) { 
       file.createNewFile(); 
      } 

      FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
      BufferedWriter bw = new BufferedWriter(fw); 
      bw.write(values); 
      for (int i = 0; i < values.length; i++) { 
       fw.write(values[i] + "\n"); 
      } 
      bw.close(); 

      System.out.println("Done"); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

只需使用bw.write(值[I] + 「\ n」);確保在完成後關閉FileWriter。並且要以相反的順序執行,只需改變for循環的方向即可:'for(int i = values.length - 1; i> = 0; i--){} – ManoDestra

+1

很難說壞縮進,但它看起來像'values'在'while'循環的範圍內,並且'try'塊內不能看到。 –

+0

您使用的是IDE嗎?因爲那會1)保持你的代碼格式良好,並且2)告訴你在嘗試在while循環外使用'values'時出現編譯錯誤。 –

回答

0

要訪問您的values array你必須declar它作爲一個類成員,或者在函數的頂部:

public class Main { 
    String[] values; 
} 
+1

或剛剛在while循環之上 –

+0

我在暗示'String [] values'需要初始化爲'null'。如果沒有,我*認爲*你會得到一個編譯器錯誤,因爲它會認爲你會試圖迭代未實例化的數組。 –

+0

@DrewKennedy類變量對象被初始化爲空 –

1

最簡單的方法,使您的陣列可見作者將在設置之前聲明它。

String line = null; 
String[] values = null; 

然後在你的while循環中,你實例化它。

values = line.split(","); 

這樣它就可以被作者看到。

+0

即使這樣做,當我到我的for循環'for(int i = 0; i javamatrix

+0

然後你可以將values數組初始化爲null或一個空數組(僅在知道大小時)。 –

0

您需要在列表中添加values並遍歷列表,或者也可以在讀取文件時進行編寫。

由於值的範圍在while循環中讀取,因此您可以選擇在打印時進行書寫。那就是:

while ((line = br.readLine()) != null) { 
    String[] values = line.split(","); 
    System.out.println("Your file has been evaluated, here are the contents:"); 
    for (String str : values) { 
     System.out.println(str); 
     fw.write(str + "\n"); 
    } 
} 
0

您可以簡單地存儲一個ArrayList並將該文件的每個分割行添加到該列表中。然後你可以稍後迭代這個列表。

List<String[]> linesList = new ArrayList<>(); 
while ((line = br.readLine()) != null) { 
    String[] values = line.split(","); 
    linesList.add(values); 
    System.out.println("Your file has been evaluated, here are the contents:"); 
    for (String str : values) { 
     System.out.println(str); 
    } 
} 

然後你可以在它們之間迭代後...

for (String[] values : linesList) { 
    for (var i = values.length - 1; i >= 0; i--) { 
     String value = values[i]; 
     // Store your value here using your FileWriter, etc. 
    } 
}