2015-11-06 177 views
0

我想按行保存一個文件作爲一個字符串數組,但我是一個初學者和困惑。如何將文件保存爲Java中的字符串數組?

package com.java24hours; 

import java.io.*; 

public class ShorteningVelocity { 
    public static void main(String[] arguments) throws IOException { 

     FileReader SVfile = new FileReader(new File("C:\\Documents\\10-27-15110mMKPSS3.dat")); 
     BufferedReader br = new BufferedReader(SVfile); 
     String temp = br.readLine(); 
     while (temp != null) { 
      temp = br.readLine(); //reads file line by line 
      System.out.println(); 
     } 
    } 
} 
+1

至於你的問題,我也搞不清楚初學者...你有什麼問題嗎?你有什麼問題? – Tom

+2

使用['Files.readAllLines()'](http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#readAllLines-java.nio.file.Path- java.nio.charset.Charset-)。 – fge

回答

2

Java數組的大小不能增長,因此您使用了一個列表(來自java.util)。這些列表動態增長,因此您可以隨意使用add()。由於列表可能包含所有內容,因此可以使用List<TypeOfWhatYouWantToStore>指定要包含的內容。因此,List類被稱爲泛型類。

將它轉換爲數組(因爲這是你想要的)有點奇怪。您分配列表大小的陣列,然後在列表上使用toArray。這將返回列表,並轉換爲數組。它必須將數組作爲參數,因爲編譯器需要使用泛型進行編譯。

package com.java24hours; 

import java.io.*; 
import java.util.*; 

public class ShorteningVelocity { 
    public static void main(String[] arguments) throws IOException { 

     FileReader SVfile = new FileReader(new File("C:\\Documents\\10-27-15110mMKPSS3.dat")); 
     BufferedReader br = new BufferedReader(SVfile); 
     String temp = br.readLine(); 
     List<String> tmpList = new ArrayList<>(); 
     while (temp != null) { 
      tmpList.add(temp); 
      temp = br.readLine(); //reads file line by line 
     } 
     String[] myArray = new String[tmpList.size()]; 
     myArray = tmpList.toArray(myArray); 
    } 
} 

編輯:使用Files.readAllLines()(見你的問題的意見)是更容易和更快的可能,但使用這個循環中,你可以看到更多的是怎麼回事的。

編輯2:更常見的是使用這種循環:

 String temp; 
     List<String> tmpList = new ArrayList<>(); 
     while ((temp = br.readLine()) != null) { 
      tmpList.add(temp); 
     } 

環路現在做了以下內容:

  • br.readLine()temp
  • 如果temp爲空,離開循環
  • 如果temp不爲空,則添加i噸至列表

您還可以通過執行打印數組:

for (String str : myArray) { 
    System.out.println(str); 
} 
+0

*「交換循環內部的行更爲常見」*將「null」作爲列表的最後一個元素添加爲「更常見」?您應該使用適當的'while'循環。 – Tom

+0

哦對了......修理它 –

+1

@Tom應該現在好吧 –

0
public static void main(String[] args) { 
     try { 
      FileWriter fw = new FileWriter("C:\\Documents\\10-27-15110mMKPSS3-out.dat"); 
      BufferedWriter bw = new BufferedWriter(fw); 
      PrintWriter outFile = new PrintWriter(bw); 

      FileReader SVfile = new FileReader(new File("C:\\Documents\\10-27-15110mMKPSS3.dat")); 
      BufferedReader br = new BufferedReader(SVfile); 
      String temp; 
      while ((temp = br.readLine()) != null) { 
       //reads file line by line 
       System.out.println(); 
       outFile.println(temp); 
      } 
      outFile.close(); 
     } catch (IOException e) { 
     } 
    } 
+0

這是偉大的謝謝! – ChibiChibi

+0

ChibiChibi,如果我的回答是最好的,你能檢查我的答案嗎? – sgrillon

0
public static void main(String[] args){ 
    List<String> tmpList = new ArrayList<>(); 
    try { 
     FileReader fileReader = new FileReader(new File("D:\\input.txt")); 
     BufferedReader bufferedReader = new BufferedReader(fileReader); 
     String temp; 

     while ((temp = bufferedReader.readLine()) != null) { 
      tmpList.add(temp); 
     } 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    String[] myArray = new String[tmpList.size()]; 
    myArray = tmpList.toArray(myArray); 
    for(int i = 0; i< myArray.length ; i ++){ 
     System.out.println(myArray[i]); 
    } 
相關問題