Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

writing string into file

 
Slaxmi Raj
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should find your answer in the Java Tutorials.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic