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.

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

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

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.

This article has been dead for over six months. Start a new discussion instead.