954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Write File to disk

Hi all,

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

Here's what I have tried:

import java.io.*;
import java.util.*;
public static void main (String[] arg)
{
        try
        {
                String file_name = "test.txt";
                FileWriter file = new FileWriter(file_name);
                BufferedWriter out = new BufferedWriter (file);
                String text = "This is goint to be written to the file";
                out.write(text);
                out.close();
        }
        catch (IOException e)
        {
                System.out.println(e.getMessage());
        }
}


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

Thanks.

new_2_java
Junior Poster
127 posts since Apr 2007
Reputation Points: 7
Solved Threads: 6
 

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

import java.io.*;
import java.util.*;
public class CreateTestTxt
{
	public static void main (String[] arg)
	{
	        try
	        {
	                String file_name = "test.txt";
	                FileWriter file = new FileWriter(file_name);
	                BufferedWriter out = new BufferedWriter (file);
	                String text = "This is goint to be written to the file";
	                out.write(text);
	                out.close();
	        }
	        catch (IOException e)
	        {
	                System.out.println(e.getMessage());
	        }
	}
}


Difficult to comment on realy beginners stuff which you should know

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

kindly, I test your code, and I find there are no errors
and the test.txt file was created
:)

almasalkhi
Newbie Poster
4 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 
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

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.Difficult to comment on realy beginners stuff which you should know
very difficult to comment.

But I should tell you, good observation.

new_2_java
Junior Poster
127 posts since Apr 2007
Reputation Points: 7
Solved Threads: 6
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You