Pagini recente » Cod sursa (job #860559) | Cod sursa (job #2200708) | Cod sursa (job #2219123) | Cod sursa (job #48479) | Cod sursa (job #1361751)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class CSVFormat {
public class ValuesFormat {
protected List<String> valueFields = new ArrayList<String>();
public void addField(String field) {
valueFields.add(field);
}
public List<String> getValues() {
return valueFields;
}
}
protected List<String> keyFields = new ArrayList<String>();
protected List<ValuesFormat> objects = new ArrayList<ValuesFormat>();
public void addNewObject() {
objects.add(new ValuesFormat());
}
public void addNewValue(int objectIndex, String value) {
objects.get(objectIndex).addField(value);
}
public List<String> getValues(int index) {
return objects.get(index).getValues();
}
public int getNumberOfValues() {
return objects.size();
}
public void addNewKey(String field) {
keyFields.add(field);
}
public List<String> getKeys() {
return keyFields;
}
}
public class Main {
public static void main(String[] args) {
Scanner reader = null;
CSVFormat csvFormat = new CSVFormat();
String line;
String word;
boolean save, isValue;
int currentCharIndex;
int objectIndex;
try {
reader = new Scanner(new FileInputStream("convertor.in"));
objectIndex = -1;
while (reader.hasNextLine()) {
line = reader.nextLine();
currentCharIndex = 0;
word = "";
save = false;
isValue = false;
while (currentCharIndex < line.length()) {
if (line.charAt(currentCharIndex) == ' ' && save == false) {
//do nothing
} else if (line.charAt(currentCharIndex) == ':') {
isValue = true;
} else if (line.charAt(currentCharIndex) == '{') {
objectIndex++;
csvFormat.addNewObject();
} else if ((line.charAt(currentCharIndex) == '"' || Character.isDigit(line.charAt(currentCharIndex))) && save == false) {
word = ""; save = true;
if (Character.isDigit(line.charAt(currentCharIndex))) {
word += line.charAt(currentCharIndex);
}
} else if ((line.charAt(currentCharIndex) == ',' || line.charAt(currentCharIndex) == '"' || line.charAt(currentCharIndex) == '}') && save == true) {
if (isValue) {
csvFormat.addNewValue(objectIndex, word);
} else {
if (objectIndex == 0) {
csvFormat.addNewKey(word);
}
}
word = ""; save = false; isValue = false;
} else if (save == true) {
word += line.charAt(currentCharIndex);
}
currentCharIndex++;
}
}
} catch (FileNotFoundException e) {
System.out.println("Input file not found!");
} finally {
reader.close();
}
PrintWriter writer = null;
List<String> values;
List<String> keys;
try {
writer = new PrintWriter("convertor.out");
keys = csvFormat.getKeys();
for (String key : keys) {
writer.print(key + ",");
}
writer.println();
for (objectIndex = 0; objectIndex < csvFormat.getNumberOfValues(); objectIndex++) {
values = csvFormat.getValues(objectIndex);
for (String value : values) {
writer.print(value + ",");
}
writer.println();
}
} catch (FileNotFoundException e) {
System.out.println("Output file not found!");
} finally {
writer.close();
}
}
}