Write File to disk

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

Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

Write File to disk

 
0
  #1
Apr 21st, 2007
Hi all,

I am trying to create a file and write something into that file.

Here's what I have tried:

  1. import java.io.*;
  2. import java.util.*;
  3. public static void main (String[] arg)
  4. {
  5. try
  6. {
  7. String file_name = "test.txt";
  8. FileWriter file = new FileWriter(file_name);
  9. BufferedWriter out = new BufferedWriter (file);
  10. String text = "This is goint to be written to the file";
  11. out.write(text);
  12. out.close();
  13. }
  14. catch (IOException e)
  15. {
  16. System.out.println(e.getMessage());
  17. }
  18. }

But, when I run this program, no file is created. Can someone help me out with that. Where I went wrong.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,267
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 493
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Write File to disk

 
0
  #2
Apr 21st, 2007
I do not know how you can run this file when in the state as is it would not compile with about 10 error messages. What about class declaration public class CreateTestTxt for example
  1. import java.io.*;
  2. import java.util.*;
  3. public class CreateTestTxt
  4. {
  5. public static void main (String[] arg)
  6. {
  7. try
  8. {
  9. String file_name = "test.txt";
  10. FileWriter file = new FileWriter(file_name);
  11. BufferedWriter out = new BufferedWriter (file);
  12. String text = "This is goint to be written to the file";
  13. out.write(text);
  14. out.close();
  15. }
  16. catch (IOException e)
  17. {
  18. System.out.println(e.getMessage());
  19. }
  20. }
  21. }

Difficult to comment on realy beginners stuff which you should know
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 4
Reputation: almasalkhi is an unknown quantity at this point 
Solved Threads: 0
almasalkhi almasalkhi is offline Offline
Newbie Poster

Re: Write File to disk

 
0
  #3
Apr 22nd, 2007
kindly, I test your code, and I find there are no errors
and the test.txt file was created
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

Re: Write File to disk

 
0
  #4
Apr 22nd, 2007
Originally Posted by peter_budo View Post
I do not know how you can run this file when in the state as is it would not compile with about 10 error messages. What about class declaration
I know, I realized after that I missed in my post, the class decleration part. However, I had in my code the class declaration like you said
  1. public class CreateTestTxt
.

But question, was that even with class declaration, like the one that you replied back, I wasn't able to make this simple code work.

I created another file, with different name. I compiled it, and ran it, it worked fine.

Originally Posted by peter_budo View Post
Difficult to comment on realy beginners stuff which you should know
very difficult to comment.

But I should tell you, good observation.
Last edited by new_2_java; Apr 22nd, 2007 at 1:24 pm.
Reply With Quote Quick reply to this message  
Reply

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




Views: 9066 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC