3

I want map my object from text file, the text file contents are like this :

~
attribute1value
attribute2value
attribute3value
attribute4value
attribute5value
attribute6value
~
attribute1value
attribute2value
attribute3value
attribute4value
attribute5value
attribute6value
...continued same 

So for each 5 attributes I want to create new object and map those 6 properties to it(that is not issue), the issue is how can I distinguish lines while reading, how can I get the first group, second group etc . thank you

2
  • Forget java, when you see the file how do you differentiate between one set and another set? Is there is a key somewhere? Any delimiters? Are the attribute values always one after another, in a particular order? Please rephrase the question, and provide more details. Commented Jul 19, 2010 at 12:25
  • 2
    @Nivas the delimeter is ~ every entry starts with ~ Commented Jul 19, 2010 at 12:33

4 Answers 4

4

I suggest using a 3rd-party utility such as Flatworm to handle this for you.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you for yoru response, can you give me an example please
@c0mrade - did you even visit the link from skaffman? there is an example there...
@willcodejavaforfood I went trough it I couldn't find it thats why I ask can he point where is that example
2

Adapted from here, and assuming there are always 6 properties per object:

You can use java.io.BufferedReader to read a file line by line.

BufferedReader reader = new BufferedReader(new FileReader("/path/to/file.txt"));
String line = null;
int count = 0;
MyObject obj = null;
while ((line = reader.readLine()) != null) {
    if(obj == null) obj = new MyObject();
    if(count <= 6) {
      switch(count) {
        case 0: // ignore: handles '~'
          break;
        case 1: // assign value of line to first property, like:
          obj.prop1 = line;
          break;
        // etc up to case 6
      }
      count++;
    } else {
      // here, store object somewhere, then...
      obj = null;
      count = 0;
    }
}

1 Comment

+1 - simple solution for a simple file format. Proves, that we still can code without using some xxx4j libraries :)
1

Here is a more flexible approach. We can specify a custom (single-line) delimiter, no delimiter is actually needed at the beginning or at the end of the file (but can be given), the number of lines of a record is flexible. The data is parsed into a simple model which can be used to validate data and create the final objects.

private String recordDelimiter = "~";

public static List<List<String>> parse(Reader reader) {

   List<List<String>> result = new ArrayList<List<String>>();
   List<String> record = new ArrayList<String>();
   boolean isFirstLine = true;

   while ((line = reader.readLine()) != null) {

      line = line.trim();

      if (line.length() == 0) {
        continue;  // we skip empty lines
      }

      if (delimiter.equals(line.trim()) {
        if (!isFirstLine) {
          result.add(record);
          record = new ArrayList<String>();
        } else {
          isFirstLine = false;   // we ignore a delimiter in the first line.
        }
        continue;
      } 

      record.add(line);
      isFirstLine = false;
   }

   if (!result.contains(record))
     result.add(record);   // in case the last line is not a separator

   return result;

} 

Comments

0

Primitive code, no exception handling, requires 100% perfect file structure and file which ends in the record delimiter character '~'.

Gives you a start though.

public class Record {

    private String field1 = null;
    private String field2 = null;
    private String field3 = null;
    private String field4 = null;
    private String field5 = null;
    private String field6 = null;

    private void read(DataInputStream din) throws IOException, ClassNotFoundException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(din));

        field1 = reader.readLine();
        field2 = reader.readLine();
        field3 = reader.readLine();
        field4 = reader.readLine();
        field5 = reader.readLine();
        field6 = reader.readLine();

        reader.readLine(); // Skip separator line "~".
    }


    private static void main(String[] args) throws IOException, ClassNotFoundException {
       FileInputStream fin = new FileInputStream("C:\\file.dat");
       DataInputStream din = new DataInputStream(fin);
       Collection<Record> records = new LinkedList<Record>();

       while(0 < din.available()) {
           Record record = new Record();
           record.read(din);

           records.add(record);
       }

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.