This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes writing string into file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "writing string into file" Watch "writing string into file" New topic
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
    
  19

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
    
    4
You should find your answer in the Java Tutorials.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: writing string into file
 
Similar Threads
HELP!!! Output stream closing prematurely
Not able to write the output to the file.
Creating Directories with FileWriter
File Directory
Split a File According to a String in the File