Skip to main content

Read lines with specific characteristics from a file, exclude all others line.

No replies
rjshrd
Offline
Joined: 2014-01-16
Points: 0

Hi I am trying to write method to read lines with some specification from a file. for Example,

my text file contains following:--

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

tab separated values, for different columns and these are the lines only I want method to read.
Now suppose if any is there as below, or even enter

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

That particular line should be escaped. Till now I have done when the file format is proper, and it is as below:--

public static void main(String[] args) {
BufferedReader br = null;
String temp = null;
List arrayRead = new ArrayList();
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 dataReq = new Hashtable();
for(int i=0; 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();
}
}
}
}

Any help will be useful.