2014-01-16 15 views
0

我正在嘗試編寫方法來讀取文件中某些規範的行。例如,從文件中讀取具有特定特徵的行,排除所有其他行

我的文本文件包含以下內容: -

12-01-01 13:26 San Jose 12.99 DVD 
12-12-30 09:40 Miami 13.50 Music 
14-08-30 10:20 Arizona 16.03 Scientist 
11-07-10 09:10 New York 25.00 ColdPlay 
14-08-30 10:20 Arizona 18.04 MeetYou 
14-08-30 10:20 Arizona 50.03 Scientist 
11-07-10 09:30 New York 25.00 ColdPlay 
11-07-10 09:20 New York 25.00 ColdPlay 

製表符分隔值,對於不同的列和這些線路只是我想方法來讀取。 現在想,如果任何有如下,甚至進入

12-01-01 13:26 San Jose 12.99 DVD 
12-12-30 09:40 Miami 13.50 Music 
14-08-30 10:20 Arizona 16.03 Scientist 
11-07-10 09:10 New York 25.00 ColdPlay 
14-08-30 10:20 Arizona 18.04 MeetYou 
[new lines] 
14-08-30 10:20 Arizona 50.03 Scientist 
11-07-10 09:30 New York 25.00 ColdPlay 
//This line should not be read 
even this should not be read #$%^& 
11-07-10 09:20 New York 25.00 ColdPlay 

這個特定的行應該進行轉義。到現在我已經做了當文件格式是正確的,這是如下: -

public static void main(String[] args) { 
    BufferedReader br = null; 
    String temp = null; 
    List<String> arrayRead = new ArrayList<String>(); 
    try{ 
     br = new BufferedReader(new FileReader("D:\\testing\\SalesData.txt")); 
     while((temp=br.readLine())!= null){ 
      arrayRead.add(temp); 
     } 
     int n = arrayRead.size(); 
     System.out.println("No. of Records in file "+n); 
     //Add arrayList data to String Array 
     String[] linesToRead = arrayRead.toArray(new String[arrayRead.size()]); 

     String[] lineX = null; 
     Hashtable<String, String> dataReq = new Hashtable<String, String>(); 
     for(int i=0; i<arrayRead.size(); i++){ 
      lineX = linesToRead[i].split("\\t"); 
      dataReq.put(lineX[2], lineX[3]); 
     } 

    } 
    catch(FileNotFoundException f){ 
     f.printStackTrace(); 
    } 
    catch(IOException e){ 
     e.printStackTrace(); 
    } 
    finally{ 
     if(br!= null){ 
      try { 
       br.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 
+0

我纔拿到這個權:要提取文本(例如,「亞利桑那」號),價格(或任何在下面的列)從你的文件忽略任何不是那種格式? – omgBob

回答

0

爲什麼不regex?可以有用。

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
import java.util.ArrayList; 
import java.util.Scanner; 
import java.io.IOException; 
import java.io.File; 
import java.io.FileInputStream; 

public class MyLineReader { 

    public static void main(String[] args) { 
     File inputFile = new File("myfile.txt"); 

     // Create pattern object. 
     String pattern = "^(\\d{2}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2})\\s([a-zA-Z\\s]*)\\s(\\d*\\.?\\d*)\\s(\\w*)$"; 
     ArrayList<String[]> collectedLines = new ArrayList<String[]>(); 
     Pattern r = Pattern.compile(pattern); 

     // Match those. 
     Matcher m; 
     FileInputStream fis = null; 

     try{ 
      fis = new FileInputStream(inputFile); 
      Scanner fileScanner = new Scanner(fis); 
      String line; 
      String[] row; 

      while (fileScanner.hasNextLine()){ 
       line = fileScanner.nextLine(); 
       m = r.matcher(line); 

       if (m.find()) { 

        // Regex have groups. 
        row = new String[] { m.group(1), m.group(2), m.group(3), m.group(4) }; 

        collectedLines.add (row); 
         System.out.println (String.format("Date: %s, Name: %s, Decimal: %s, Last: %s", row[0], row[1], row[2], row[3])); 
        } 
       } 
     }catch(IOException ex){ 
      System.err.println(ex.getMessage()); 
     }finally { 
      if (fis != null){ 
       try{ 
        fis.close(); 
       }catch(Exception ex){ 
       } 
      } 
     } 

    } 
} 

使用正則表達式如下,你可以在網上here檢查正則表達式:

String pattern = "^(\\d{2}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2})\\s([a-zA-Z\\s]*)\\s(\\d*\\.?\\d*)\\s(\\w*)$"; 
+0

這是行得通的。 – user1829708

相關問題