2012-12-04 78 views
-1

我有一個文件需要解析。雖然問題非常簡單,但我還沒有取得任何進展。問題在於。 該文件包含大約20-22行的數據塊,然後是未知數的空白行,然後是20-22行的塊。我需要製作這些數據的數據結構。 我曾嘗試以下關於文件解析java

File f1 = new File(PATH_TO_TRAINING_FILE); 
FileInputStream fis1 = new FileInputStream(f1); 
readerTrainingFile = new BufferedReader(new InputStreamReader(fis1)); 
String trainLine; 
while ((trainLine =readerTrainingFile.readLine()) != null) { 
    ArrayList<String> train = new ArrayList<String>(); 
    while (!trainLine.trim().equals("")) { 
     train.add(trainLine); 
     trainLine =readerTrainingFile.readLine(); 
    } 
    while (readerTrainingFile.readLine().trim().equals("")) { 
    } 
} 

因此,與上述代碼的問題是,同時在第三而當我完成檢查空行然後,讀取線移動到第一非空間的指針環下一個塊的行。所以,當我的控制到達第一個while循環時,它會跳過我想要的兩行數據。如果問題很簡單,我真的很抱歉。我現在堅持了2天。謝謝你的幫助。

回答

2

對此進行重構,因此行readerTrainingFile.readLine()在您的程序中只出現一次。循環嵌套是使自己的生活變得悲慘的好方法。如果您需要跳過行,請使用continue語句。對於調試,System.out.println(trainLine)可以查看您正在閱讀的內容,並可能每次都跳過。這些步驟可以解決您的問題。

+0

謝謝,我會努力去做這個。 – shaun

0
while ((trainLine =readerTrainingFile.readLine()) != null) { 
      ArrayList<String> train = new ArrayList<String>(); 
      while (!trainLine.trim().equals("")) { 
       train.add(trainLine); 
       trainLine =readerTrainingFile.readLine(); 
      } 
      while (readerTrainingFile.readLine().trim().equals("")) { 

      } 
} 

這就是你的問題。你讀了兩行。只要把這個代碼在第一while循環:

if (trainLine.trim().equals("")) { 
    train.add(trainLine); 
} 

此外,另一個問題:移動這樣的:

ArrayList<String> train = new ArrayList<String>(); 

走出循環。否則,每次你讀一行時都會有新的。

1

考慮這樣的事情

List<List<String>> trains = new ArrayList<List<String>>(); 
List<String> curTrain = null; 
while ((trainLine=readerTrainingFile.readLine()) != null) { 
    if (!trainLine.trim().equals("")) 
     curTrain = null; 
    else 
    { 
     if (curTrain == null) 
     { 
      curTrain = new ArrayList<String>(); 
      trains.add(curTrain); 
     } 
     curTrain.add(trainLine) 
    } 
} 

trains是一個包含所有塊的列表。在讀取數據時,curTrain引用當前正在添加行的塊。每當你得到一個非空行時,你將它添加到當前的塊中,但是如果沒有當前塊(因爲你在開始,或者一個或多個先前的行是空的),你創建一個新塊,將其添加到塊的列表中。

0
Scanner scanner = new Scanner(f1); 
ArrayList<String> train = new ArrayList<String>(); 
while(scanner.hasNextLine()){ 
    String temp = scanner.nextLine(); 
    if(!temp.trim().equals("")) 
     train.add(temp); 
} 

你可以用一個緩衝的讀者等同

(temp = reader.nextLine()) != null 

更換scanner.hasNextLine但掃描儀是有點更容易使用+理解。您正在從第一個while循環內添加字符串,因此arraylist是本地的,並且在循環完成後不會保留(reader.nextLine()== null)。

請注意,您在同一類型上使用!=和!.equals()。這對字符串來說很好,但通常.equals是用於對象的,而==是用於基元的(java將字符串視爲對象和基元之間的某處)。

0

我不知道這些「塊」代表什麼,但是我會首先想象一個比字符串列表更好的抽象。

這裏有一種方法,你可以解決它:

package cruft; 

import org.apache.commons.io.IOUtils; 
import org.apache.commons.lang3.StringUtils; 

import java.io.*; 
import java.util.LinkedList; 
import java.util.List; 
import java.util.Map; 
import java.util.TreeMap; 

/** 
* FileChunkParser description here 
* @author Michael 
* @link 
* @since 12/4/12 6:06 PM 
*/ 
public class FileChunkParser { 

    public static void main(String[] args) { 
     try { 
      File f = new File((args.length > 0) ? args[0] : "resources/chunk.txt"); 
      Reader reader = new FileReader(f); 
      FileChunkParser parser = new FileChunkParser(); 
      Map<Integer, List<String>> chunks = parser.parse(reader); 
      for (int index : chunks.keySet()) { 
       System.out.println(String.format("index: %d chunk: %s", index, chunks.get(index))); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public Map<Integer, List<String>> parse(Reader reader) throws IOException { 
     Map<Integer, List<String>> chunks = new TreeMap<Integer, List<String>>(); 
     BufferedReader br = null; 
     try { 
      if (reader != null) { 
       br = new BufferedReader(reader); 
       int chunkCount = 0; 
       String line = ""; 
       List<String> chunk = null; 
       while ((line = br.readLine()) != null) { 
        if (StringUtils.isBlank(line)) { 
         if (chunk != null) { 
          chunks.put(chunkCount++, new LinkedList<String>(chunk)); 
          chunk = null; 
         } 
         continue; 
        } else { 
         if (chunk == null) { 
          chunk = new LinkedList<String>(); 
         } 
         chunk.add(line); 
        } 
       } 
       if (chunk != null) { 
        chunks.put(chunkCount++, chunk); 
       } 
      } 
     } finally { 
      IOUtils.closeQuietly(reader); 
     } 
     return chunks; 
    } 
} 

我用這個輸入文件運行它:

this 
is 
how 
you 
do 
it 



see 
how 
it 
handles 
arbitrary 
sized 
chunks 
with 
any 
blank 
lines 
between 

try 
it 
and 
see 

和這裏的輸出:

index: 0 chunk: [this, is, how, you, do, it] 
index: 1 chunk: [see, how, it, handles, arbitrary, sized, chunks, with, any, blank, lines, between] 
index: 2 chunk: [try, it, and, see]