dimanche 22 mai 2016

Right way to Design a "file reader" for multiple files in a java project

For be clear, I'm just starting whit design patterns.

I'm doing a java program where i need to read data from different sources and in different styles. So i did a "multiple read-file" class where i read all these files and make an instance of the object who will store this data.

Example methods of the class:

public National readGeographic(String file_name, String name);
public void readPeople(String file_name, National national);
public Content readContent(String name);

Obviously this in not a very "OO principies" solution. (i can notice it now).

So i was thinking on doing this:

  1. Make an interface who defines methods to read, modify and delete a file.
  2. Make a "file type" with his own format.
  3. Make a class who can read it.
  4. Every class who needs fills his data from a file must implement the interface for read this kind of file.

The format of the file:

[Type]:<Zone>
<begin>
North,South,Center
<end>

This is my Special file read class:

public class DataTypeFileAccess{
private ArrayList<String> lines;
private RandomAccessFile file;
public DataTypeFileAccess(String file_name) throws IOException{
...
}

private Iterator getValues (String type) throws SintaxisFileException { //get the values from a type
...
}

public List<String> getData (String type) throws SintaxisFileException{ // return the values from a specific type in the file
...
}
public int numbersOflinesPointer () throws IOException{
...
}
private void indexFile() throws IOException{
...
}
private int findType(String type){
...
}

private int findBegin(int index) {
...
}
private int findEnd(int index){
...
}
}

So for example in a class i implement the interface reader and i read the file in this way.

public void read(String file_path) throws IOException, SintaxisFileException {
    DataTypeFileAccess reader = new DataTypeFileAccess(file_path);
    DataLine l;
    for(String line : reader.getData("Zone")){
        l = new DataLine(line);
        l.splitTokens(",").forEach(this::setZone);
    }
}

Is this a right way to do thing or is just as bad like the first version? there's a better way?

Every advice will be appreciated.

Thanks!

Aucun commentaire:

Enregistrer un commentaire