Pagini recente » Cod sursa (job #1394789) | Cod sursa (job #2037872) | Cod sursa (job #1354969) | Cod sursa (job #812569) | Cod sursa (job #1350991)
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 Main {
private static List<ArrayList<String>> storage;
private static int mainField = 0;
private static int query = 0;
public Main() {
storage = new ArrayList<ArrayList<String>>();
}
public static void readFromInternFile() throws IOException {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(
new FileInputStream("convertor.in"), "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 static void printCSVinFile() throws FileNotFoundException, UnsupportedEncodingException {
PrintWriter pw = new PrintWriter("convertor.out","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 static void processLine(String line) throws FileNotFoundException, UnsupportedEncodingException {
String[] helper;
helper = line.split("\\{|\\}");
int current = 0;
while(current < helper.length) {
if(helper[current].contains("[")) {
storage.add(new ArrayList<String>());
storage.add(new ArrayList<String>());
query++;
mainField = 0;
current++;
continue;
}
if(helper[current].contains("]")) {
break;
}
String[] auxiliary = helper[current].split("\"");
int auxiliaryContor = 0;
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(storage.get(0).indexOf(auxiliary[j]) == 0) {
query++;
mainField = query;
storage.add(new ArrayList<String>());
continue;
}
if(storage.get(0).contains(auxiliary[j])) {
mainField = query;
continue;
}
storage.get(0).add(auxiliary[j]);
mainField = query;
}
}
current++;
}
printCSVinFile();
}
public static void main(String[] args) throws IOException {
Main tester = new Main();
readFromInternFile();
}
}