Advertisement
Guest User

Untitled

a guest
Apr 25th, 2014
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. public static void main(String[] args) throws Exception {
  2. final Path path = Paths.get("path", "to", "folder");
  3. final Path txt = path.resolve("myFile.txt");
  4. final Path csv = path.resolve("myFile.csv");
  5. try (
  6. final Stream<String> lines = Files.lines(txt);
  7. final PrintWriter pw = new PrintWriter(Files.newBufferedWriter(csv, StandardOpenOption.CREATE_NEW))) {
  8. lines.map((line) -> line.split("\|")).
  9. map((line) -> Stream.of(line).collect(Collectors.joining(","))).
  10. forEach(pw::println);
  11. }
  12. }
  13.  
  14. try (
  15. final Stream<String> lines = Files.lines(txt);
  16. final PrintWriter pw = new PrintWriter(newBufferedWriter(csv, StandardOpenOption.CREATE_NEW))) {
  17. lines.map((line) -> line.split("\|")).
  18. map((line) -> Stream.of(line).collect(joining(","))).
  19. forEach(pw::println);
  20. }
  21.  
  22. public static void main(String[] args) throws Exception {
  23. final Path path = Paths.get("path", "to", "folder");
  24. final Path txt = path.resolve("myFile.txt");
  25. final Path csv = path.resolve("myFile.csv");
  26. final Charset utf8 = Charset.forName("UTF-8");
  27. try (
  28. final Scanner scanner = new Scanner(Files.newBufferedReader(txt, utf8));
  29. final PrintWriter pw = new PrintWriter(Files.newBufferedWriter(csv, utf8, StandardOpenOption.CREATE_NEW))) {
  30. while (scanner.hasNextLine()) {
  31. pw.println(scanner.nextLine().replace('|', ','));
  32. }
  33. }
  34. }
  35.  
  36. try (
  37. final Scanner scanner = new Scanner(newBufferedReader(txt, utf8));
  38. final PrintWriter pw = new PrintWriter(newBufferedWriter(csv, utf8, StandardOpenOption.CREATE_NEW))) {
  39. while (scanner.hasNextLine()) {
  40. pw.println(scanner.nextLine().replace('|', ','));
  41. }
  42. }
  43.  
  44. public class NewClass {
  45.  
  46. public static void main(String[] args) throws IOException {
  47.  
  48. String data = "one|two|three|four"+"n"+
  49. "one|two|three|four";
  50. //Use a BufferedReader to read from actual Text file
  51. String csv = data.replace("|", ",");
  52. System.out.println(csv);
  53.  
  54. PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("MyCSV.csv")));
  55. out.println(csv);
  56. out.close();
  57. }
  58. }
  59.  
  60. run:
  61. one,two,three,four
  62. one,two,three,four
  63. BUILD SUCCESSFUL (total time: 0 seconds)
  64.  
  65. public class Test {
  66.  
  67. public static void main(String[] args) throws URISyntaxException,
  68. IOException {
  69.  
  70. FileWriter writer = null;
  71. File file = new File("d:/sample.txt");
  72. Scanner scan = new Scanner(file);
  73. File file2 = new File("d:/CSV.csv");
  74. file.createNewFile();
  75. writer = new FileWriter(file2);
  76.  
  77. while (scan.hasNext()) {
  78. String csv = scan.nextLine().replace("|", ",");
  79. System.out.println(csv);
  80. writer.append(csv);
  81. writer.append("n");
  82. writer.flush();
  83. }
  84. }
  85. }
  86.  
  87. He|looked|for|a|book.
  88.  
  89. He|picked|up|the|book.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement