How can I read a text file, search for commas, treat commas as new lines, and export it to a new file using Java? -
i have .txt file 1 billion items separated commas. want able read file.txt file, allow script read commas, copy item before comma new file, , start new line after every comma.
example of current text file format:
one, twenty one, five, 1 hundred, seven, ten, iwoi-eiwo, ei123_32323 ... desired output:
one, twenty one, five, 1 hundred, seven, ten, iwoi-eiwo, ei123_32323, ...... any suggestion?
so entire file 1 line? if case, have following:
import java.util.scanner; import java.io.*; public class converttonewline { public static void main(string[] args) throws ioexception { file file = new file("text.txt"); file file2 = new file("textnocommas.txt"); printwriter writer = new printwriter(file2); scanner reader = new scanner(file); string alltext = reader.nextline(); alltext = alltext.replace(", ", ","); // replace commas , spaces commas (take out spaces after commas) alltext = alltext.replace(",", ",\n"); // replace commas comma , newline character writer.print(alltext); writer.close(); system.out.println("finished printing text."); system.out.println("here text:"); system.out.println(alltext); system.exit(0); } }
Comments
Post a Comment