Read line from files based on a pattern

Dear java experts
I have a file test.file. This consists of 1000 or more lines

field1: field1Error: file1path,identifier1
field2: field2Error: file2path,identifier2
field3: field3Error: file3path,identifier3
field4: field4Error: file4path,identifier4

they are all similar pattern
i really wanto have 4 variables

String field
String Error
String Path
String id


and store them appropriately how do i read the file and store them as follows

field = field1
Error=field1Error
Path=file1path
id=identifier1

ALl thehelp is very much apprecaited.
thanks a lot,
Testsubbu
TestsubbuAsked:
Who is Participating?
 
RomanRegaCommented:
I would organize the code like this:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;

public class FileInfo {
   
   public String field;
   public String error;
   public String path;
   public String id;

   public FileInfo(String field, String error, String path, String id) {
      this.field=field;
      this.error=error;
      this.path=path;
      this.id=id;
     
   }
   
   public String toString(){
      return field+": "+error+": "+path+", "+id;
   }
   
   public static FileInfo parseString(String line){
      //code form CEHJ is good
      String[] fields = line.split("[:,]");
      if (fields.length<4) return null;
      String field=fields[0].trim();
      String error=fields[1].trim();
      String path=fields[2].trim();
      String id=fields[3].trim();
      return new FileInfo(field, error, path, id);
   }
   
   public static FileInfo[] loadFromFile(String filename) throws IOException{
      BufferedReader in=new BufferedReader (new FileReader(filename));
      String line;
      ArrayList list=new ArrayList();
      while ((line=in.readLine())!=null){
         FileInfo f=parseString(line);
         if (f!=null) list.add(f);
      }
      return (FileInfo[])list.toArray(new FileInfo[list.size()]);
   }
   
   
}

Just load load from file like this:

FileInfo[] myFiles=FileInfo.loadFromFile("filename.dat");

THe fied, error etc are public, so you can access them directy.
If you wish you can change their visibilty and code some good getter and setter method to access and modify them.

The idea to use a HashMap or Hashtable to fast manage and access the various FileInfo, as aozarov propose, object is good too.
0
 
CEHJCommented:
Just split each line and put it into a custom class:

String[] fields = line.split("[:,]");
FieldInfo info = new FieldInfo();
info.setField1(fields[0].trim());
info.setError(fields[1].trim());

etc. for each line
0
 
CEHJCommented:
Each FieldInfo object can be added to a collection in the loop
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
aozarovCommented:
Set validOptions = new HashSet();
validOptions.add("field");
validOptions.add("error");
validOptions.add("path");
validOptions.add("id");

for each line you read
String[] fields = line.split(",");
Map map = new HashMap();
for (int i = 0; i < fields.length; i++)
{
String keyValues[] = fields[i].split(":");
String key = keyValues[0].trim().toLowerCase();
if (validOptions.contains(key))
{
 write to file key and  keyValue[1] which is the value
}
}
0
 
aozarovCommented:
Oops, I thought you have "," as seperator for key value and ":" for seperator for value.
It actually seems not to be the case.
0
 
CEHJCommented:
>> Oops, I thought you have "," as seperator for key value and ":" for seperator for value.

I've already written the code for that
0
 
aozarovCommented:
>> I've already written the code for that
I saw what you wrote, and I wrote something different which assumes, wrongly, different semantics for ":" and ","
0
 
CEHJCommented:
>>The idea to use a HashMap or Hashtable to fast manage and access the various FileInfo, as aozarov propose

That's not what the code posted by aozarov is actually doing. It's checking the validity of field identifiers in the file, which may not be there and i don't see why they should be. It was actually i who suggested using a collection for the purpose you mentioned:

>> Each FieldInfo object can be added to a collection in the loop


0
 
RomanRegaCommented:
Sorry CEHJ, screwed up copying the name.
0
 
CEHJCommented:
No problem ;-)
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.