Win a copy of Learning Regular Expressions this week in the General Computing 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

read one file to write multiple files  RSS feed

 
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:

 
Marshal
Posts: 60199
188
  • 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: 60199
188
  • 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()?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!