Win a copy of Functional Design and Architecture this week in the Functional programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

problem with writing file , please help

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
\n should not create any problem. It will only append a new line.
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i have tried this, its writing nothing into file.

 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should have worked, just trying using one flush() at last, and see what happens

 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic