Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
convert json to single line
package jsonFormatter;
import java.io.*;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.io.IOException;
public final class importer {
public static void main(String... aArgs) throws IOException{
String ROOT = "/Users/davidfauth/bills/109";
FileVisitor<Path> fileProcessor = new ProcessFile();
Files.walkFileTree(Paths.get(ROOT), fileProcessor);
}
private static final class ProcessFile extends SimpleFileVisitor<Path> {
@Override public FileVisitResult visitFile(
Path aFile, BasicFileAttributes aAttrs
) throws IOException {
FileWriter fileWriter = null;
String absolutePath = "";
String filePath="";
String outFilePath = "";
File f = new File(aFile.toString());
System.out.println(f.getName());
absolutePath = f.getAbsolutePath();
filePath = absolutePath.substring(0,absolutePath.lastIndexOf(File.separator));
// System.out.println("file path is: " + filePath);
// System.out.println("new file path is: " + filePath.substring(17));
outFilePath = "/Users/davidfauth/MortarData/" + filePath.substring(18).replace("/", "_");;
System.out.println("new file is " + outFilePath);
if (f.getName().toString().equals("data.json")){
System.out.println("Processing file:" + aFile);
BufferedReader br = new BufferedReader(new FileReader(aFile.toString()));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(" ");
line = br.readLine();
}
File newTextFile = new File(outFilePath+"_data1.json");
fileWriter = new FileWriter(newTextFile);
fileWriter.write(sb.toString());
fileWriter.close();
} finally {
br.close();
}
}
return FileVisitResult.CONTINUE;
}
@Override public FileVisitResult preVisitDirectory(
Path aDir, BasicFileAttributes aAttrs
) throws IOException {
// System.out.println("Processing directory:" + aDir);
return FileVisitResult.CONTINUE;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment