* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/**
* Read CSV file for various purposes
- *
+ *
* @author Instrumental(Jonathan)
*
*/
private Access access;
private boolean processAll;
private char delimiter = ',';
-
+ private boolean go;
+
public CSV(Access access, File file) {
this.access = access;
csv = file;
processAll = false;
+ go = true;
}
-
+
public CSV(Access access, String csvFilename) {
this.access = access;
csv = new File(csvFilename);
processAll = false;
+ go = true;
}
-
+
public CSV setDelimiter(char delimiter) {
this.delimiter = delimiter;
return this;
}
-
+
public String name() {
return csv.getName();
}
}
/*
* Create your code to accept the List<String> row.
- *
+ *
* Your code may keep the List... CSV does not hold onto it.
- *
+ *
* @author Instrumental(Jonathan)
*
*/
public interface Visitor {
void visit(List<String> row) throws IOException, CadiException;
}
-
+
public void visit(Visitor visitor) throws IOException, CadiException {
BufferedReader br = new BufferedReader(new FileReader(csv));
try {
String line;
StringBuilder sb = new StringBuilder();
- while((line = br.readLine())!=null) {
+ while(go && (line = br.readLine())!=null) {
line=line.trim();
if(!line.startsWith("#") && line.length()>0) {
// System.out.println(line); uncomment to debug
br.close();
}
}
-
+
public Writer writer() throws FileNotFoundException {
return new Writer(false);
}
public interface RowSetter {
public void row(Object ... objs);
}
-
+
public static class Saver implements RowSetter {
List<String> ls= new ArrayList<>();
-
+
@Override
public void row(Object ... objs) {
if(objs.length>0) {
}
}
}
-
+
public List<String> asList() {
List<String> rv = ls;
ls = new ArrayList<>();
private Writer(final boolean append) throws FileNotFoundException {
ps = new PrintStream(new FileOutputStream(csv,append));
}
-
+
@Override
public void row(Object ... objs) {
if(objs.length>0) {
ps.println();
}
}
-
+
private void print(String s) {
boolean quote = s.matches(".*[,|\"].*");
if(quote) {
ps.append(s);
}
-
+
}
/**
* Note: CSV files do not actually support Comments as a standard, but it is useful
ps.printf(comment,objs);
ps.println();
}
-
+
public void flush() {
ps.flush();
}
-
+
public void close() {
flush();
ps.close();
}
-
+
public String toString() {
return csv.getAbsolutePath();
}
}
+ /**
+ * Provides a way to stop processing records from inside a Visit
+ */
+ public void stop() {
+ go = false;
+ }
+
public void delete() {
csv.delete();
}
-
+
public String toString() {
return csv.getAbsolutePath();
}