I have to read a text file and based on some logic, make some changes to the file. So I am reading the file line by line by using
while ( (temp_string = inputstream.readLine()) != null )
{
/*after employing some other logic*/
outputstream.write(temp_string);
outputstream.flush();
}
Where temp_string is a String, and the declaration for inputstream is :
BufferedReader inputstream = new BufferedReader(new FileReader(pathtosilentfile));
and the declaration of outputstream is :
BufferedWriter outputstream = new BufferedWriter(new FileWriter(pathtooutputfile));
But when i write to the text file (new one) there is a lot of garbage, that shows on notepad++ as NULL. Also, the written data looks ok in notepad or wordpad. So is this some kind of an encoding error ? I dont get it ! Whats going wronng ?
It would really have helped if you’d told us what
output_streamis – but it should be aWriterof some description – probably anOutputStreamWriterwrapped around aFileStream, specifying the appropriate encoding (which is up to you, I guess).If it looks okay in Notepad / Wordpad, but broken in Notepad++, then it probably is an encoding issue, but you ought to decide which encoding to use. UTF-8 is often a good choice (compact for ASCII, covers the whole of Unicode, widely supported) but it depends on what needs to read the file.