• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with writing file , please help

 
isabella swan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[size=12]
in the above code,
assume some data is present in the file in_file,
i am able to write the data from in_file to output_file.
after writing the data from in_file am trying to write some string, but its not writing to the output_file.
if i uncomment the code(out.write("\n".getBytes());) am able to write the string.
can anyone please tell me the reason.

thanks in advance.[/size]
 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
\n should not create any problem. It will only append a new line.
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should always flush and close streams properly.

If you're dealing with character data, then you should wrap some kind of Writer around the OutputStream - then you can use writer.write(str) instead of out.write(str.getBytes(),0,str.getBytes().length).

As an aside, String.getBytes() completely ignores the question of encodings, and is thus not cross-platform compatible. Make sure that's OK with the kind of data you're working with.
 
isabella swan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lester Burnham wrote:You should always flush and close streams properly.

If you're dealing with character data, then you should wrap some kind of Writer around the OutputStream - then you can use writer.write(str) instead of out.write(str.getBytes(),0,str.getBytes().length).

As an aside, String.getBytes() completely ignores the question of encodings, and is thus not cross-platform compatible. Make sure that's OK with the kind of data you're working with.



am not clear about, wat you mean by writer.write(str); can you please give eg.
i dint get wat kind of object writer is.
 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case you are probably being suggested to use PrintWriter.
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, a PrintWriter would work. You could even wrap a BufferedWriter around it if you're concerned about performance (which you seem to be, since you wrap a BufferedReader around the InputStream).

Make sure you understand the difference between stream I/O (using subclasses of InputStream and OutputStream) and character I/O (using subclasses of Reader and Writer); it's where the question of encodings arises.
 
isabella swan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i have tried this, its writing nothing into file.

 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As suggested you need to flush and close the streams properly
 
isabella swan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:As suggested you need to flush and close the streams properly


Now its writing contents of in_file, but strings are not written into ouput file
 
isabella swan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


if i comment while block the string are writte into output file. but i want to write from file as well as some strings. can anyone suggest solution
 
isabella swan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:In this case you are probably being suggested to use PrintWriter.



i have tried with PrintWriter instead of BufferedWriter,problem still the same, am not able write strings.

 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should have worked, just trying using one flush() at last, and see what happens

 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this question is more related to IO. Let's slide this over to the IO and Streams forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic