Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) throws Exception {
- final Path path = Paths.get("path", "to", "folder");
- final Path txt = path.resolve("myFile.txt");
- final Path csv = path.resolve("myFile.csv");
- try (
- final Stream<String> lines = Files.lines(txt);
- final PrintWriter pw = new PrintWriter(Files.newBufferedWriter(csv, StandardOpenOption.CREATE_NEW))) {
- lines.map((line) -> line.split("\|")).
- map((line) -> Stream.of(line).collect(Collectors.joining(","))).
- forEach(pw::println);
- }
- }
- try (
- final Stream<String> lines = Files.lines(txt);
- final PrintWriter pw = new PrintWriter(newBufferedWriter(csv, StandardOpenOption.CREATE_NEW))) {
- lines.map((line) -> line.split("\|")).
- map((line) -> Stream.of(line).collect(joining(","))).
- forEach(pw::println);
- }
- public static void main(String[] args) throws Exception {
- final Path path = Paths.get("path", "to", "folder");
- final Path txt = path.resolve("myFile.txt");
- final Path csv = path.resolve("myFile.csv");
- final Charset utf8 = Charset.forName("UTF-8");
- try (
- final Scanner scanner = new Scanner(Files.newBufferedReader(txt, utf8));
- final PrintWriter pw = new PrintWriter(Files.newBufferedWriter(csv, utf8, StandardOpenOption.CREATE_NEW))) {
- while (scanner.hasNextLine()) {
- pw.println(scanner.nextLine().replace('|', ','));
- }
- }
- }
- try (
- final Scanner scanner = new Scanner(newBufferedReader(txt, utf8));
- final PrintWriter pw = new PrintWriter(newBufferedWriter(csv, utf8, StandardOpenOption.CREATE_NEW))) {
- while (scanner.hasNextLine()) {
- pw.println(scanner.nextLine().replace('|', ','));
- }
- }
- public class NewClass {
- public static void main(String[] args) throws IOException {
- String data = "one|two|three|four"+"n"+
- "one|two|three|four";
- //Use a BufferedReader to read from actual Text file
- String csv = data.replace("|", ",");
- System.out.println(csv);
- PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("MyCSV.csv")));
- out.println(csv);
- out.close();
- }
- }
- run:
- one,two,three,four
- one,two,three,four
- BUILD SUCCESSFUL (total time: 0 seconds)
- public class Test {
- public static void main(String[] args) throws URISyntaxException,
- IOException {
- FileWriter writer = null;
- File file = new File("d:/sample.txt");
- Scanner scan = new Scanner(file);
- File file2 = new File("d:/CSV.csv");
- file.createNewFile();
- writer = new FileWriter(file2);
- while (scan.hasNext()) {
- String csv = scan.nextLine().replace("|", ",");
- System.out.println(csv);
- writer.append(csv);
- writer.append("n");
- writer.flush();
- }
- }
- }
- He|looked|for|a|book.
- He|picked|up|the|book.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement