Pagini recente » Cod sursa (job #66848) | Cod sursa (job #1172771) | Cod sursa (job #1272592) | Cod sursa (job #1017294) | Cod sursa (job #1350983)
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
class Tester {
private List<ArrayList<String>> storage;
private String fileInput;
private String fileOutput;
private static int mainField = 0;
private static int query = 0;
public Tester() {
storage = new ArrayList<ArrayList<String>>();
fileInput = "convertor.in";
fileOutput = "convertor.out";
}
public void buildMainFields() {
storage.add(new ArrayList<String>());
}
public ArrayList<String> getMainFields() {
return storage.get(0);
}
public int mainFieldValue(int counter, int query) {
if(counter % 2 == 0) return 0;
return query;
}
public void readFromInternFile() throws IOException {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(
new FileInputStream(fileInput), "UTF8"));
String line = br.readLine();
if(line != null) processLine(line);
while((line = br.readLine()) != null) {
processLine(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
br.close();
}
}
public void printCSVinFile() throws FileNotFoundException, UnsupportedEncodingException {
PrintWriter pw = new PrintWriter(fileOutput,"UTF8");
int size = storage.size();
for(int j = 0; j < size; j++) {
for(int k = 0; k < storage.get(j).size(); k++) {
pw.print(storage.get(j).get(k) + ",");
}
pw.println();
}
pw.close();
}
public void processLine(String line) throws FileNotFoundException, UnsupportedEncodingException {
String[] helper;
helper = line.split("\\{|\\}");
int current = 0;
while(current < helper.length) {
if(helper[current].contains("[")) {
buildMainFields();
storage.add(new ArrayList<String>());
query++;
mainField = 0;
current++;
continue;
}
if(helper[current].contains("]")) {
break;
}
String[] auxiliary = helper[current].split("\"");
for(int j = 0; j < auxiliary.length; j++) {
String[] auxiliary2 = auxiliary[j].split(",");
if(auxiliary2.length == 0) {continue;}
auxiliary[j] = auxiliary2[0];
if(auxiliary[j].contains(":")) {
auxiliary[j] = auxiliary[j].split(":")[1];
}
while(auxiliary[j].startsWith(" ")) auxiliary[j] = auxiliary[j].substring(1);
if(auxiliary[j].length() == 0) {continue;}
if(mainField != 0) {
storage.get(mainField).add(auxiliary[j]);
mainField = 0;
} else {
if(getMainFields().indexOf(auxiliary[j]) == 0) {
query++;
mainField = query;
storage.add(new ArrayList<String>());
continue;
}
if(getMainFields().contains(auxiliary[j])) {
mainField = query;
continue;
}
getMainFields().add(auxiliary[j]);
mainField = query;
}
}
current++;
}
printCSVinFile();
}
public static void main(String[] args) throws IOException {
Reader reader = new Reader();
reader.readFromInternFile();
}
}