Display object from file in java

I want to display my object from a text file, the contents of the text file are as follows:

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

Thus, for each of the 5 attributes that I want to create a new object and display these 6 properties for it (which do not produce), the question is, how can I distinguish lines while reading, how can I get the first group, the second group etc. thanks

+3
source share
4 answers

. () , ( ), . , .

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;

} 
+1

, ​​ Flatworm, .

+4

, 6 :

java.io.BufferedReader .

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

, , 100% , '~'.

.

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

Source: https://habr.com/ru/post/1755303/


All Articles