import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.io.PrintWriter;

class FileIO {
	public static void main(String[] args) {

		// By default, the Scanner's next() method
		// stop at whitespace (\n, \t, sp)
	
		// \n, \t and sp are delimiters
	
		Scanner kb = new Scanner(System.in);

		// we can change the delimiters like this

		kb.useDelimiter("e|j|\n");

		System.out.println("print a long string");
	
		String token = kb.next();
		System.out.println(token);
		
		token = kb.next();
		System.out.println(token);

		token = kb.next();
		System.out.println(token);
	
		// Initialing scanner to read from file

		Scanner filein = null;

		try {
			filein = new Scanner(new File("grades.csv"));
		} catch (FileNotFoundException e) {
			System.out.println("File not found");
			return;
		}
		
		filein.useDelimiter(",|\n");

		// Initialize an ArrayList to hold the grades

		ArrayList<Integer> list = new ArrayList<>();

		while(filein.hasNext()) {
			int grade = filein.nextInt();
			list.add(grade);
		}

		// Create a PrintWriter to print to another file

		PrintWriter pw = null;

		try {
			pw = new PrintWriter(new File("grades.bkup"));
		}
		catch(FileNotFoundException e) {
			System.out.println("File not found");
			return;
		}
		
		// Write the grades to the backup file using PrintWriter

		for(Integer i : list) {
			pw.println(i);
		}
	}
}
