aspose file tools*
The moose likes Beginning Java and the fly likes writing string into file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
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: 45
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: 18896
    
  40

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: 45
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: 39396
    
  28
You should find your answer in the Java Tutorials.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: writing string into file