| Author |
writing string into file
|
Slaxmi Raj
Ranch Hand
Joined: Apr 20, 2012
Posts: 40
|
|
Here I am trying to write a string into "example.txt",this file compiled and did run successfully, but there is no content in the "example.txt".
i am unable to understand the reason behind. anyone please tell how to write a string and int values into a file in java.
import java.io.*;
public class Write_String_And_IntDemo {
public static void main(String[] args)throws IOException {
BufferedWriter out =new BufferedWriter(new FileWriter("example.txt"));
out.write("abcd");
}
}
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16692
|
|
Slaxmi Raj wrote:Here I am trying to write a string into "example.txt",this file compiled and did run successfully, but there is no content in the "example.txt".
i am unable to understand the reason behind. anyone please tell how to write a string and int values into a file in java.
import java.io.*;
public class Write_String_And_IntDemo {
public static void main(String[] args)throws IOException {
BufferedWriter out =new BufferedWriter(new FileWriter("example.txt"));
out.write("abcd");
}
}
You forgot to close the file. When a program exits, the operating system will close any open files -- and unfortunately, the OS doesn't know about any data that hasn't been flushed in the terminating program.
Henry
|
 |
Slaxmi Raj
Ranch Hand
Joined: Apr 20, 2012
Posts: 40
|
|
Henry Wong wrote:
Slaxmi Raj wrote:Here I am trying to write a string into "example.txt",this file compiled and did run successfully, but there is no content in the "example.txt".
i am unable to understand the reason behind. anyone please tell how to write a string and int values into a file in java.
import java.io.*;
public class Write_String_And_IntDemo {
public static void main(String[] args)throws IOException {
BufferedWriter out =new BufferedWriter(new FileWriter("example.txt"));
out.write("abcd");
}
}
You forgot to close the file. When a program exits, the operating system will close any open files -- and of course, the OS doesn't know about any data that hasn't been flushed in the terminating program.
Henry
Yes, got it, thank you.I need one more , here i would like write int values too. please tell me how to write?
thank you.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32675
|
|
|
You should find your answer in the Java Tutorials.
|
 |
 |
|
|
subject: writing string into file
|
|
|