patternjavaMinor
Simple query language for CSV files
Viewed 0 times
simplequerycsvlanguagefilesfor
Problem
I'm struggling to improve this command-line java application, which reads CSV or txt files, ands allows the user do some "queries" with the data. I can't use any external library, so I'm doing it all and now I'm trying to do some refactoring on it, to let it be scalable, and maybe in the future I could add some new commands / queries. I tried some design patterns like builders, interfaces and etc ..., but it does not seem right.
Do you have any tip to abstract this in some elegant way? I will post the first working version of the application to you guys give it a look:
Reader.java - this class reads the file and returns an array with all the rows and each row another array in which it will store each attribute of the row:
ReaderConfig.java - It will parameterize Reader.java, based on user input:
```
public class ReaderConfig {
private String path;
private String delimiter;
private boolean hasHeader;
private Scanner scanner = new Scanner(System.in);
public void setPathFile() {
System.out.println("Enter the full path of the file ");
String input = scanner.nextLine();
if(FileUtils.isFileExtensionValid(FileUtils.getFileExtension(input))){
try {
new FileReader(input);
this.path = input;
} catch
Do you have any tip to abstract this in some elegant way? I will post the first working version of the application to you guys give it a look:
Reader.java - this class reads the file and returns an array with all the rows and each row another array in which it will store each attribute of the row:
public class Reader {
public ReaderConfig readerConfig;
public Reader(ReaderConfig readerConfig){
this.readerConfig = readerConfig;
}
public List read() {
List rows = new ArrayList();
try {
BufferedReader br = new BufferedReader(new FileReader(readerConfig.getPath()));
String row = "";
while ((row = br.readLine()) != null) {
String[] properties = row.split(readerConfig.getDelimiter());
rows.add(properties);
}
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return rows;
}
}ReaderConfig.java - It will parameterize Reader.java, based on user input:
```
public class ReaderConfig {
private String path;
private String delimiter;
private boolean hasHeader;
private Scanner scanner = new Scanner(System.in);
public void setPathFile() {
System.out.println("Enter the full path of the file ");
String input = scanner.nextLine();
if(FileUtils.isFileExtensionValid(FileUtils.getFileExtension(input))){
try {
new FileReader(input);
this.path = input;
} catch
Solution
try-with-resourcestry {
BufferedReader br = new BufferedReader(new FileReader(readerConfig.getPath()));
String row = "";
while ((row = br.readLine()) != null) {
String[] properties = row.split(readerConfig.getDelimiter());
rows.add(properties);
}
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}This is exactly the situation for which
try-with-resources was designed: try (BufferedReader br = new BufferedReader(new FileReader(readerConfig.getPath()))) {
String row = "";
while ((row = br.readLine()) != null) {
String[] properties = row.split(readerConfig.getDelimiter());
rows.add(properties);
}
} catch (IOException ex) {
ex.printStackTrace();
}If you have to compile against an old version of Java, the old way of doing this was to use a
finally block. } finally {
if (br != null) {
br.close;
}But that means that we have to define
br before the try block. It's easier to just do the try-with-resources. equalsIgnoreCaseif(input.toLowerCase().equals("quit")){Java has a method for this situation.
if (input.equalsIgnoreCase("quit")) {Perhaps that just does the same thing. Or maybe Java has a more efficient way to check. Note that if the strings are not equal, it doesn't have to lower case the whole string. Only the part that it checks.
Typo
At least twice you write "Avaliable" when you presumably meant "Available".
Code Snippets
try {
BufferedReader br = new BufferedReader(new FileReader(readerConfig.getPath()));
String row = "";
while ((row = br.readLine()) != null) {
String[] properties = row.split(readerConfig.getDelimiter());
rows.add(properties);
}
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}try (BufferedReader br = new BufferedReader(new FileReader(readerConfig.getPath()))) {
String row = "";
while ((row = br.readLine()) != null) {
String[] properties = row.split(readerConfig.getDelimiter());
rows.add(properties);
}
} catch (IOException ex) {
ex.printStackTrace();
}} finally {
if (br != null) {
br.close;
}if(input.toLowerCase().equals("quit")){if (input.equalsIgnoreCase("quit")) {Context
StackExchange Code Review Q#152089, answer score: 3
Revisions (0)
No revisions yet.