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