2013-01-03 19 views
1

我有一個包含數據的文本文件。該文件包含所有月份的信息。假設1月的信息佔用50行。比2月開始,它佔據了40多行。比我有3月等等...是否有可能只讀取部分文件?我可以說「從X行到Y行」嗎?還是有更好的方法來完成這個?我只想打印對應於一個月的數據而不是全部文件。這裏是我的代碼使用java在文本文件中讀取確定數量的行

public static void readFile() 
{ 
    try 
    { 
    DataInputStream inputStream = 
    new DataInputStream(new FileInputStream("SpreadsheetDatabase2013.txt")); 

    while(inputStream.available() != 0) 
    { 
    System.out.println("AVAILABLE: " + inputStream.available()); 
    System.out.println(inputStream.readUTF()); 
    System.out.println(inputStream.readInt()); 
    for (int i = 0; i < 40; i++) 
    { 
     System.out.println(inputStream.readUTF()); 
     System.out.println(inputStream.readUTF()); 
     System.out.println(inputStream.readUTF()); 
     System.out.println(inputStream.readUTF()); 
     System.out.println(inputStream.readUTF()); 
     System.out.println(inputStream.readDouble()); 
     System.out.println(inputStream.readUTF()); 
     System.out.println(inputStream.readBoolean()); 
     System.out.println(); 
    } 
    }// end while 
    inputStream.close(); 
}// end try 
catch (Exception e) 
{ 
    System.out.println("An error has occurred."); 
}//end catch 
}//end method 

謝謝你的時間。

+1

有沒有辦法告訴數據在哪裏變化? (例如從1月到2月)你能提供一些樣本數據嗎? – Daniel

+0

這取決於佈局。例如,我們如何識別一個月開始的地方? –

+0

我不確定。如果我可以使用搜索方法來查找一個月的開始時間和結束時間,我猜... – greinodacosta

回答

0

我的方法是讀取文本文件的全部內容並將其存儲在ArrayList中,並只讀取所需月份的行。

例子:

使用此功能來讀取文件中的所有行。

/** 
* Read from a file specified by the filePath. 
* 
* @param filePath 
*   The path of the file. 
* @return List of lines in the file. 
* @throws IOException 
*/ 

public static ArrayList<String> readFromFile(String filePath) 
     throws IOException { 
    ArrayList<String> temp = new ArrayList<String>(); 
    File file = new File(filePath); 
    if (file.exists()) { 
     BufferedReader brin; 
     brin = new BufferedReader(new FileReader(filePath)); 
     String line = brin.readLine(); 
     while (line != null) { 
      if (!line.equals("")) 
       temp.add(line); 
      line = brin.readLine(); 
     } 
     brin.close(); 
    } 
    return temp; 
} 

然後只讀從ArrayList中臨時需要的人。

例如:如果你想讀月一個月的數據假設由40行的50行數據和啓動

for(int i=40;i<90;i++) 
{ 
    System.out.println(temp.get(i)); 
} 

注意:這僅僅是這樣做只是其中一種方法。我不確定是否有其他方法!

0

如果您有Java 7,則可以使用Files.readAllLines(Path path, Charset cs),例如,

Path path = // Path to "SpreadsheetDatabase2013.txt" 
Charset charset = // "UTF-8" or whatever charset is used 
List<String> allLines = Files.readAllLines(path, charset); 
List<String> relevantLines = allLines.subList(x, y); 

x(含)y(獨家)表示感興趣的行號,見List.subList(int fromIndex, int toIndex)。這種解決方案的

一個好處,如在readAllLines()的JavaDoc指出:

此方法確保在所有的字節已被讀出的文件被關閉或一個I/O錯誤,或其它運行時異常,被拋出。

0

基於你怎麼說你的數據是有組織的,我建議做這樣的事情

ArrayList<String> temp = new ArrayList<String>(); 
int read = 0; 
File file = new File(filePath); 
if (file.exists()) { 
    BufferedReader brin; 
    brin = new BufferedReader(new FileReader(filePath)); 
    String line = brin.readLine(); 
    while (line != null) { 
     if (!line.equals("")){ 
      if(line.equals("March")) 
       read = 1; 
      else if(line.equals("April")) 
       break; 
      else if(read == 1) 
       temp.add(line); 
     } 
     line = brin.readLine(); 
    } 
    brin.close(); 

就嘗試過自己,那將承擔在所有三,四月間的數據。您可以根據需要調整它們或使它們變量。感謝ngoa的基礎代碼。信貸到期貸款

0

我會使用scanner class

Scanner scanner = new Scanner(filename);

使用scanner.nextLine()讓每一個文件的行。如果您只希望從第x行到第y行,您可以使用for循環掃描您需要的行之前不需要的每一行代碼。小心不要在沒有拋出異常的情況下觸發異常。

或者你可以通過掃描儀,併爲每一行,將行的字符串內容添加到ArrayList。祝你好運。