We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

java file operations

vkchaitu82
vkchaitu82 asked
on
Medium Priority
294 Views
Last Modified: 2012-05-11
I am working on basic java functions programs.
Basically I have 10 files with some text.
1)I want to Read from 10 files(example: Read file1, Read file2...) and write the out put in a single file(write file).
2) the program should read a line from : Read file1 and than writeto the file and Remove that file from that file.
3) once the program is executed all the ReadFiles should have 0 lines ( all the lines must be deleted)
and all the data should be in one file.

=
I tried using these but having issues in writing to the file.
BufferedReader in = new BufferedReader(new FileReader("test.txt"));
BufferedWriter out = new BufferedWriter(new FileWriter("output.txt"));
str = in.readLine()
out.write(..);
Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:
PrintStream psout = new PrintStream(new FileOutputStream("output.txt"));
psout.println(str);

Thius will printout your line
Awarded 2011
Awarded 2011

Commented:
I am not sur how you'll remove lines from files. You'd probably rather in the end delete all input files and create new empty ones with the same names
Amitkumar PSr. Consultant
CERTIFIED EXPERT

Commented:
vkchaitu82,

Post the entire code here. What issues you are facing is not clear
Awarded 2011
Awarded 2011

Commented:
Of course your BufferedWriter should also work.
I'm sure you used it youself many times.
The issue is somewhere else. Post your code.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>2) the program should read a line from : Read file1 and than writeto the file and Remove that file from that file.

I assume you mean that it should remove the LINE from the file..?

Your purpose is unclear. Let's assume for the purposes of illustration that you have THREE file. Can you post 3 example files, together with what you want to see in the result file?

Author

Commented:
Thanks for response amit, yan, Cehj.

input:
File 1: Contents are
--------
This is file 1 line 1:
This is file 1 line 2
This is file 1 line 3

File 2: Contents are
--------
This is file 2 line 1:
This is file 2 line 2
This is file 2 line 3

out should be merged file with: (result)

This is file 1 line 1:
This is file 1 line 2
This is file 1 line 3
This is file 2 line 1:
This is file 2 line 2
This is file 2 line 3

content of: File 1: noting.
File 2: noting.

===
I am looking for a program which does the above .
Awarded 2011
Awarded 2011
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Thanks an, you are deleting the file and creating one....
But I am looking at not deleting the file..want to delete the link that is read?
Awarded 2011
Awarded 2011

Commented:
What's the point of dioing that on each step?
Once you read everything from the file you can
delete it.
I don't think you can delete one line from the file, without
re-writing the whole file.

Is it again about this feed application?
CERTIFIED EXPERT
Top Expert 2016

Commented:
So you want to be left with empty files?

Author

Commented:
yan, no this is for my file operations exercise  that I am looking into.

CEHJ, Yes I want to be left with empty files.
CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Awarded 2011
Awarded 2011

Commented:
No with normal text files in Java you cannot remove the line, some talks here:
http://www.artima.com/forums/flat.jsp?forum=1&thread=101966

You of course can create a method which will do it for you after each line
in fact rewriting the whole file and replacing
the old file with a new one without the line just read - but
this would be very funny way  in the end to achieve the same thing
with much more time and effort.

You can delete explicvitly or overwrite the file
without deleteing, but you cannot edit your files.

You can edit only RandomAccessFiles - but even in those
you can replace bytes, but not change number of bytes in the middle of
the file
Awarded 2011
Awarded 2011

Commented:


@vkchaitu82,
I hope you understand that what is happening in both ID:35454068 and ID:35454412
is absolutely identical things - in both cases files are closed
and overwritten with empty string, only
in one case it is done  in the same program, in another case it is wrapped
up in additional class


CERTIFIED EXPERT
Top Expert 2016

Commented:
:)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.