开发者

Mapping Object from file in java

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 t开发者_如何学运维hose 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


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


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;
    }
}


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;

} 


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);
       }

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜