In: Computer Science
Source file example:
John
Peter
Jane
Mary
Evelyn
Daniel
New CSV File example:
Number, Name
1,John
2,Peter
3,Jane
4,Mary
5,Evelyn
6,Daniel
Example Scenario 1 – Normal run:
Enter source file name: source.txt
Enter destination file name: destination.csv
Your file is ready!
Example Scenario 2 – File doesn’t exist:
Enter source file name: source.txt
Enter destination file name: destination.csv
Source file doesn’t exist!
Example Scenario 3 – Target file already exists (overwrite):
Enter source file name: source.txt
Enter destination file name: destination.csv
Target file already exists. Overwrite existing file? (Y/N): N
Enter destination file name: destination.csv
Target file already exists. Overwrite existing file? (Y/N): N
Enter destination file name: destination.csv
Target file already exists. Overwrite existing file? (Y/N): N
Enter destination file name: destination.csv
Target file already exists. Overwrite existing file? (Y/N): Y
Your file is ready!
Example Scenario 4 – Target file already exists (new name given):
Enter source file name: source.txt
Enter destination file name: destination.csv
Target file already exists. Overwrite existing file? (Y/N): N
Enter destination file name: destination.csv
Target file already exists. Overwrite existing file? (Y/N): N
Enter destination file name: destination2.csv
Your file is ready!
Please find the code below:
import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class LoopsFiles { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); System.out.println("Enter source file name: "); String source = sc.next(); ArrayList<String> arrayList = new ArrayList<>(); try { File file = new File(source); Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String data = scanner.nextLine(); arrayList.add(data); System.out.println(data); } scanner.close(); } catch (FileNotFoundException e) { System.out.println("Source file doesn’t exist!"); e.printStackTrace(); } while(true) { System.out.println("Enter destination file name: "); String destination = sc.next(); File file = new File(destination); if (file.createNewFile()) { System.out.println("File has been created."); FileOutputStream fileOutputStream = new FileOutputStream(file); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fileOutputStream)); bw.write("Number,Name"); bw.newLine(); for (int i=1;i<=arrayList.size();i++) { String temp = i + "," + arrayList.get(i-1); System.out.println(temp); bw.write(temp); bw.newLine(); } System.out.println("Your file is ready!"); break; } else { System.out.println("Target file already exists. Overwriting existing file? (Y/N):"); String inp = sc.next(); if(inp.equals("N")) { continue; } else { System.out.println("File has been created."); FileOutputStream fileOutputStream = new FileOutputStream(file); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fileOutputStream)); bw.write("Number,Name"); bw.newLine(); for (int i=1;i<=arrayList.size();i++) { String temp = i + "," + arrayList.get(i-1); System.out.println(temp); bw.write(temp); bw.newLine(); } System.out.println("Your file is ready!"); break; } } } } }