Two Laptop Bag*
The moose likes I/O and Streams and the fly likes problem with writing file , please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "problem with writing file , please help" Watch "problem with writing file , please help" New topic
Author

problem with writing file , please help

isabella swan
Greenhorn

Joined: Nov 19, 2010
Posts: 12
[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

Joined: Jan 08, 2009
Posts: 1449
    
    6

\n should not create any problem. It will only append a new line.


Swastik
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
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

Joined: Nov 19, 2010
Posts: 12
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

Joined: Jan 08, 2009
Posts: 1449
    
    6

In this case you are probably being suggested to use PrintWriter.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
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

Joined: Nov 19, 2010
Posts: 12


i have tried this, its writing nothing into file.

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

As suggested you need to flush and close the streams properly
isabella swan
Greenhorn

Joined: Nov 19, 2010
Posts: 12
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

Joined: Nov 19, 2010
Posts: 12


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

Joined: Nov 19, 2010
Posts: 12
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

Joined: Jan 08, 2009
Posts: 1449
    
    6

It should have worked, just trying using one flush() at last, and see what happens

Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

I think this question is more related to IO. Let's slide this over to the IO and Streams forum.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with writing file , please help
 
Similar Threads
IO package
problem while dispalying from file
writing data to CSV file
Reading Reg exp from text file give error
How to transform a OutputStream in a InputStream?