I'm currently working on a lab in my cpe class and we have to create a simple program that scans in strings from a .txt file and prints them to a different .txt file. So far I have the basic program drawn out but my exception keeps getting throw though I have all the necessary files. Can anyone help me debug?
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."); >> >
asked May 14, 2012 at 17:53
1,327 3 3 gold badges 17 17 silver badges 29 29 bronze badges
Your in/output file doesn't have an extension?
Commented May 14, 2012 at 17:55
He already said they're .txt files. so it looks like that's the problem. A bit of advice for any computer you plan to do programming on: set your file browser to always show file extensions.
Commented May 14, 2012 at 18:50 Don't forget to close scanner and writer. Commented Apr 10, 2014 at 16:51You need to figure out where it looks for the "input" file. When you just specify "input" it looks for the file in the current working directory. When working with an IDE, this directory may not be what you think it is.
Try the following:
System.out.println(new File("input").getAbsolutePath());
to see where it looks for the file.
answered May 14, 2012 at 17:58 419k 114 114 gold badges 826 826 silver badges 837 837 bronze badgesThanks!! It turns out my .txt files were in the wrong folder. So it's finding them but now it is not writing over from my input.txt to my output.txt. Output.txt is still empty.
Commented May 14, 2012 at 20:16That's because you don't call flush. I suggest you call printer.close() when you're done (which will flush it for you).
Commented May 14, 2012 at 20:25 Thanks!! It works now! I closed my scanner but not my printer. Commented May 14, 2012 at 20:34May be you are just forget the flush()
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); >**printer.flush();** > catch (FileNotFoundException e)answered May 14, 2012 at 18:05 8,916 6 6 gold badges 38 38 silver badges 52 52 bronze badges He says that he gets an exception. Commented May 14, 2012 at 18:11 @aioobe what is your exception? Commented May 14, 2012 at 18:12 I don't have any exceptions, Mike (the guy that posted the question) has. Commented May 14, 2012 at 18:13
The exception is thrown because of the file not being found, not because of the printer failing to flush all of its bytes.
Commented May 14, 2012 at 18:18I know this is a super old answer/question but he would indeed need a printer.close() call to finish writing to the file
Commented Dec 7, 2018 at 21:36When accessing files with Java I/O, you must include the file's filetype extension (if one exists).
File input = new File("input.txt"); File output = new File("output.txt");
answered May 14, 2012 at 17:55
28.7k 13 13 gold badges 62 62 silver badges 95 95 bronze badges
I had the .txt extensions prior and I would still throw my exception. I am using eclipse and the .txt files are in the src fold along with my .java files.
Commented May 14, 2012 at 20:12I added the extensions back on and moved the files to the correct folder but now every time I run my program, my output.txt gets updated but when I open it up there is still nothing in it.
Commented May 14, 2012 at 20:22We can do this by reading the file using FileInputStream object and write into another file using FileOutputStream object.
Here is the sample code
package java_io_examples; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Vector; public class Filetest < public static void main(String[] args) < try < FileInputStream fin = new FileInputStream("D:\\testout.txt"); int i = 0; String s = ""; while((i=fin.read())!=-1) < s = s + String.valueOf((char)i); >FileOutputStream fout = new FileOutputStream("D:\\newtestout1.txt"); byte[] b = s.getBytes(); fout.write(b); fout.close(); System.out.println("Done reading and writing!!"); > catch(Exception e) < System.out.println(e); >> >