Two of the most often used classes for reading and writing files are FileInputStream and FileOutputStream.
When opening files you must use a try-catch block.
public class FileIO { public static void main(String[] args) { Scanner sc; PrintWriter pw; try { sc = new Scanner(new FileInputStream("in.txt")); pw = new PrintWriter(new FileOutputStream("out.txt")); String token; while (sc.hasNext()){ token = sc.next(); pw.println(token); } } catch (FileNotFoundException e) { System.out.println("File not found: " + e.toString()); return; } sc.close(); pw.close(); } }
© 2017 – 2020, Eric. All rights reserved.