2013-03-16 128 views
2

我有這個文本文件:Java:如何從文本文件讀取特定行?

2013001 Jaymarion Manalo L. Service Crew March 15, 2013 8:00 12:00 13:00 17:00 10.0 
2013001 Jaymarion Manalo L. Service Crew March 16, 2013 8:00 12:05 13:05 17:30 10.0 

現在,我有一個條件,如果字符串DATE1 =「2013年3月15日」,它會讀取它所屬的線路,將只得到「8:00 「,」12:00「,」13:00「和」17:00「,並將其顯示在我已聲明的文本字段中。我的意思是,這甚至可能嗎?它是如何完成的?我希望你能得到我想說的話。我在Java的是使新的-_-

+1

,所有你能做的就是讀取文件順序,一行一行,直到你找到合適的人。 – 2013-03-16 11:49:27

+1

你可以遍歷所有行,檢查if(line.contains(「2013年3月15日」){'並打印它或做任何你想做的事情。 – Pshemo 2013-03-16 11:50:17

回答

0

正如建議你可以循環到你想要的行,然後你可以嘗試這樣的事情: - 對FileUtils.readLines()檢查this

String s= FileUtils.readLines(filename).get(lineNumber); 

欲瞭解更多信息。

1

您可以使用FileUtils.readLines() 作爲字符串列表讀取文件這會給你一個字符串列表。然後遍歷列表並使用string.contains("March 15, 2013")查找日期。

說明:我可以與您分享完整的代碼,但您應該嘗試使用上述信息進行編碼。

0

來讀取行線...

try { 
    FileReader fileReader = 
     new FileReader(fileName); 

    BufferedReader bufferedReader = 
     new BufferedReader(fileReader); 
    //in any loop use bufferedreader.readline() untill you get your desired line 
    //to part your string use split() 
    bufferedReader.close();   
} 
0

你可以試試這個:

import java.io.BufferedReader; 
import java.io.DataInputStream; 
import java.io.FileInputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 

public class FileReaderService { 

    public static void main(String[] args) { 
     FileReaderService fileReaderService = new FileReaderService(); 
     fileReaderService.performExecute("March 15, 2013"); 
    } 

    public void performExecute(String inputPattern) { 
     List<String[]> matches = new ArrayList<String[]>(); 
     try { 
      FileInputStream fileInputStream = new FileInputStream("C:/Users/Guest/Desktop/your-file.txt"); /*change your file path here*/ 
      DataInputStream dataInputStream = new DataInputStream(fileInputStream); 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dataInputStream)); 
      String strLine; 
      while ((strLine = bufferedReader.readLine()) != null) { 
       if (strLine.contains(inputPattern)) { 
        String[] splits = strLine.split("[\\s]{3,}"); /*splits matching line with 3 consecutive white spaces*/ 
        matches.add(splits); 
       } 
      } 
      dataInputStream.close(); 

      for (String[] items : matches) { 
       System.out.println(items[1] + "\t" + items[2] + "\t" + items[3] + "\t" + items[4]); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 
} 

輸出:

8:00 12:00 13:00 17:00 
1

Ofcource這成爲可能。

您需要逐行讀取文件。檢查每行是否包含date1。如果它確實提取並解析你想要的數據。

下面是一個示例代碼如何通過標準API實現這一點:如果條件是基於在線內容

public class ReadFileLineByLineAnExtractSomeText { 

    private static final String src = "test.txt"; 
    private static final String date1 = "March 15, 2013"; 

    @BeforeClass 
    public static void genTextFile() throws IOException { 
     OutputStream os = new FileOutputStream(src); 
     os.write(("blah bla foo bar\n" + 
       "2013001 Jaymarion Manalo L. Service Crew March 15, 2013 8:00 12:00 13:00 17:00 10.0\r\n" + 
       "2013001 Jaymarion Manalo L. Service Crew March 16, 2013 8:00 12:05 13:05 17:30 10.0\r" + 
       "... the end").getBytes()); 
     os.close(); 
    } 


    @Test 
    public void testReadAndExtract() throws IOException { 
     BufferedReader br = new BufferedReader(new FileReader(src)); 
     String line = br.readLine(); 
     int lineNumber = 0; 
     while(line != null) { 
      lineNumber++; 
      int i = line.indexOf(date1); 
      if(i != -1) { 
       int s = i + date1.length(); 
       int e = line.length(); 
       System.out.println(date1 + " found in line " + lineNumber + " at index " + i + ", extract text from " + s + " to " + e); 
       String extractedText = line.substring(s, e); 
       String[] extractedTextParts = extractedText.trim().split("\\s+"); 
       for(String part : extractedTextParts) { 
        if(isTime(part)) { 
         System.out.println(" '" + part + "'"); 
        } 
       } 
      } 
      line = br.readLine(); 
     } 
     br.close(); 
    } 

    private boolean isTime(String part) { 
     return part == null ? false : part.matches("\\d{1,2}:\\d{1,2}"); 
    } 
} 

輸出

March 15, 2013 found in line 2 at index 42, extract text from 56 to 94 
    '8:00' 
    '12:00' 
    '13:00' 
    '17:00'