• Post Reply Bookmark Topic Watch Topic
  • New Topic

read one file to write multiple files  RSS feed

 
mahesh waran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai,
i've write source code.but not get correct output.

Input:File1.txt
1.hai to all.
2.this program is about file read and write.
3.this is good concept

Output:File2.txt
1.hai to all.
3.this is good concept

File3.txt
2.this program is about file read and write.

Note:Program is read one file and write the alternate line to each file.

Tell me the error in sourcecode.My program was compile and run but not write any content in each file.
Please find the error.I tried more time but not found the error.I gave the source code below.

thanks advance.
Mahesh

SourceCode:

 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid your code is difficult to read, because it is not indented. Please go back and click the edit button and add code tags. Highligh from the beginning of each class to its end and push the code button.

Why are you using DataOutputStreams? Those are intended for binary files, and you have text files, for which BufferedReader, BufferedWriter, FileReader and FileWriter are much more appropriate. Look in the Java Tutorials and keep going until you see the section about buffered streams. You will find an example of what I mean in there.
It is not at all a good idea to leave a reader or a writer open. You really out to close them, preferably in a finally block. If you don’t close them, there is the risk of those files being locked and becoming unavailable to other applications. Tthere is also the risk that your writing is never terminated, and if the writing is never terminated, you might have all your text in the buffer still and nothing in the file Look up this method, and then look for close(), which tells you that you don’t need to call the previous method!
Remember, always close readers and writers (except for System.out, System,in and System.err) when you have finished with them.
 
mahesh waran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ritche,
thanks your kind information.I'm a beginner of java.So some error come in the code.Now i'm check & edit the code that means
1.close the bufferedwrite reference
2. file append is set true.
3.file streams convert to file reader and writer.

Now i got exact output.

thanks Ritche
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
Did you notice that if you use close(), you don’t need flush()?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!