| | |
basic file i/o
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2006
Posts: 17
Reputation:
Solved Threads: 0
I have a basic Java file I/O question. I am trying to read/write from/to a text file (obviously) from within a function that is called by main(). Can you do the following within a function:
or does this have to be done within main()? If within main(), then how do you get inFile and outFile to work in the function? Do you pass them like a normal parameter?
I tried creating the inFile and outFile objects within a function, but kept getting errors when the program compiled. Thanks...
Jaden403
Java Syntax (Toggle Plain Text)
Scanner inFile = new Scanner(new FileReader("File1.txt")); PrintWriter outFile = new PrintWriter("File2.txt");
or does this have to be done within main()? If within main(), then how do you get inFile and outFile to work in the function? Do you pass them like a normal parameter?
I tried creating the inFile and outFile objects within a function, but kept getting errors when the program compiled. Thanks...
Jaden403
Yes, of course you can. What are the errors you are getting?
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: May 2006
Posts: 17
Reputation:
Solved Threads: 0
The first example works fine:
However, when I try to do the same thing in a function:
I get this message:
File2.java:14: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
PrintWriter outFile = new PrintWriter(new PrintWriter("outputdocument.txt"));
^
1 error
Java Syntax (Toggle Plain Text)
import java.io.*; import java.util.*; public class File { public static void main(String[] args) throws FileNotFoundException { PrintWriter outFile = new PrintWriter(new PrintWriter("outputdocument.txt")); for (int i = 1; i <= 10; i++) outFile.printf("%d%n", i); outFile.close(); } }
Java Syntax (Toggle Plain Text)
import java.io.*; import java.util.*; public class File2 { public static void main(String[] args) { printFunction(); } public static void printFunction() throws FileNotFoundException { PrintWriter outFile = new PrintWriter(new PrintWriter("outputdocument.txt")); for (int i = 1; i <= 10; i++) outFile.printf("%d%n", i); outFile.close(); } }
File2.java:14: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
PrintWriter outFile = new PrintWriter(new PrintWriter("outputdocument.txt"));
^
1 error
Well, the error says it all.
Your method delares that it throws an exception. That means, that you must either call the method from within a try catch block, or the method (this time main) calling your method, must also declare that it throws the same exception that your method does.
Your method delares that it throws an exception. That means, that you must either call the method from within a try catch block, or the method (this time main) calling your method, must also declare that it throws the same exception that your method does.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
Similar Threads
- Setting an array and reading in a txt file backwards (C++)
- Txt Coordinates into chart... (Java)
- Basic File I/O problem (C++)
- Need to network two computers from different blocks (Networking Hardware Configuration)
- Need help with including C++ code in Word macro (C++)
Other Threads in the Java Forum
- Previous Thread: array problem in java "Snake game"
- Next Thread: My GridLayout Not working Fine
| Thread Tools | Search this Thread |
-xlint add android api applet application array arrays automation bi binary blackberry bluetooth chat class classes client code compile compiler component converter database digit eclipse equation error event exception fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying oracle page pearl print problem program programming project qt recursion scanner screen scrollbar server set size sms sort spamblocker sql string swing system thread threads time tree variablebinding windows xor






