basic file i/o

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2006
Posts: 17
Reputation: jaden403 is an unknown quantity at this point 
Solved Threads: 0
jaden403 jaden403 is offline Offline
Newbie Poster

basic file i/o

 
0
  #1
Oct 14th, 2006
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:

  1.  
  2. Scanner inFile = new Scanner(new FileReader("File1.txt"));
  3. 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
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: basic file i/o

 
0
  #2
Oct 15th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 17
Reputation: jaden403 is an unknown quantity at this point 
Solved Threads: 0
jaden403 jaden403 is offline Offline
Newbie Poster

Re: basic file i/o

 
0
  #3
Oct 15th, 2006
The first example works fine:
  1. import java.io.*;
  2. import java.util.*;
  3. public class File
  4. {
  5. public static void main(String[] args) throws FileNotFoundException
  6. {
  7. PrintWriter outFile = new PrintWriter(new PrintWriter("outputdocument.txt"));
  8.  
  9. for (int i = 1; i <= 10; i++)
  10. outFile.printf("%d%n", i);
  11.  
  12. outFile.close();
  13. }
  14. }
However, when I try to do the same thing in a function:
  1. import java.io.*;
  2. import java.util.*;
  3. public class File2
  4. {
  5. public static void main(String[] args)
  6. {
  7. printFunction();
  8. }
  9.  
  10. public static void printFunction() throws FileNotFoundException
  11. {
  12. PrintWriter outFile = new PrintWriter(new PrintWriter("outputdocument.txt"));
  13.  
  14. for (int i = 1; i <= 10; i++)
  15. outFile.printf("%d%n", i);
  16.  
  17. outFile.close();
  18. }
  19. }
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: basic file i/o

 
0
  #4
Oct 16th, 2006
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.
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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 17
Reputation: jaden403 is an unknown quantity at this point 
Solved Threads: 0
jaden403 jaden403 is offline Offline
Newbie Poster

Re: basic file i/o

 
0
  #5
Oct 16th, 2006
Okay, thanks...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC