943,994 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 17199
  • Java RSS
Apr 21st, 2007
0

Write File to disk

Expand Post »
Hi all,

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

Here's what I have tried:

Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 7
Solved Threads: 6
Junior Poster
new_2_java is offline Offline
127 posts
since Apr 2007
Apr 21st, 2007
0

Re: Write File to disk

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
Java Syntax (Toggle Plain Text)
  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
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,658 posts
since Dec 2004
Apr 22nd, 2007
0

Re: Write File to disk

kindly, I test your code, and I find there are no errors
and the test.txt file was created
Reputation Points: 10
Solved Threads: 0
Newbie Poster
almasalkhi is offline Offline
4 posts
since Apr 2007
Apr 22nd, 2007
0

Re: Write File to disk

Click to Expand / Collapse  Quote originally posted by peter_budo ...
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
Java Syntax (Toggle Plain Text)
  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.

Click to Expand / Collapse  Quote originally posted by peter_budo ...
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.
Reputation Points: 7
Solved Threads: 6
Junior Poster
new_2_java is offline Offline
127 posts
since Apr 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: java text field for password please
Next Thread in Java Forum Timeline: Help me how to find a host ip in Network using java program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC