File I / O: read from one file and write to another (Java)
•
Java
I'm currently working in a lab in my CPE class. We have to create a simple program from Txt file and print them to different Txt file So far, I have drawn the basic program, but my exceptions have been thrown, even though I have all the necessary files Can anyone debug it for me?
import java.io.*; import java.util.*; public class FileIO { public static void main(String args[]) { try { File input = new File("input"); File output = new File("output"); Scanner sc = new Scanner(input); PrintWriter printer = new PrintWriter(output); while(sc.hasNextLine()) { String s = sc.nextLine(); printer.write(s); } } catch(FileNotFoundException e) { System.err.println("File not found. Please scan in new file."); } } }
Solution
You need to find out where it looks for the "input" file When you specify only input, it looks for the file in the current working directory When using the IDE, this directory may not be what you think
Try the following:
System.out.println(new File("input").getAbsolutePath());
See where it finds the file
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码