aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes FileWriter, File Created but no content 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "FileWriter, File Created but no content" Watch "FileWriter, File Created but no content" New topic
Author

FileWriter, File Created but no content

Balaguru Janarthanan
Ranch Hand

Joined: Nov 01, 2000
Posts: 37
import java.io.*;
class FileWriterDemo{
public static void main(String arg[])throws IOException{
String source="i still love software";
char buffer[]=new char[source.length()];
source.getChars(0,source.length(),buffer,0);
FileWriter fw =new FileWriter("flie1.txt");
for(int i=0; i<buffer.length;i++){>
System.out.println(buffer[i]);
fw.write(buffer[i]);
}
}
}
I can see new file and empty, no contenet.
Any one pls. help me.
Seema Das
Ranch Hand

Joined: Mar 05, 2001
Posts: 78
Balguru,
The for loop is not contructed properly so the write statement is actually outside the loop.
But the truth is you don't need a for loop since Writer class has write method that takes a character array as its argument. So just use fw.write(buffer). and then you need to close the object (fw.close()) only then the array will be written to the file.
hth
Seema
sai challa
Ranch Hand

Joined: Feb 06, 2001
Posts: 54
Originally posted by Balaguru Janarthanan:
import java.io.*;
class FileWriterDemo{
public static void main(String arg[])throws IOException{
String source="i still love software";
char buffer[]=new char[source.length()];
source.getChars(0,source.length(),buffer,0);
FileWriter fw =new FileWriter("flie1.txt");
for(int i=0; i<buffer.length;i++){>
System.out.println(buffer[i]);
fw.write(buffer[i]);
}
}
}
I can see new file and empty, no contenet.
Any one pls. help me.

You can modify your for loop:
for(int i=0;i<buffer.length;i++)>
{
System.out.println(buffer[i]);
fw.write(buffer[i]);
fw.flush();
}
The flush() method ensures that any character present in the FileWriter fw is written to the file "flie1.txt".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FileWriter, File Created but no content