repl.it
@bikey/

Java-FileIO-Sample

Java

No description

fork
loading
Files
  • Main.java
  • jdt.ls-java-project
  • in.txt
  • out.csv
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.Scanner;
import java.util.InputMismatchException;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
  public static void main(final String[] args) {
    Scanner sc = null;
    FileWriter fw = null;

    try {
      sc = new Scanner(new File("./in.txt"));
      File output = new File("./out.csv");
      fw = new FileWriter(output);
      
      int n = 1;
      while (sc.hasNextLine()) {
        if(n==1){

        }else if (n % 5 == 0){
          fw.write("\n");
        }else{
          fw.write(",");
        }
        String str = sc.nextLine();
        fw.write("\"" + str + "\"");
        n++;
      }

      fw.flush();
    } catch (InputMismatchException e) {
      System.out.println(e);
    } catch (IOException e) {
			System.out.println(e);
    } finally {
      sc.close();
      try{ fw.close(); }
      catch(IOException e) { System.out.println(e); }
    }
  }
}
?