Link to home
Start Free TrialLog in
Avatar of sigma19
sigma19Flag for United States of America

asked on

java file operations

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(..);
Avatar of for_yan
for_yan
Flag of United States of America image

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

Thius will printout your line
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
Avatar of Am P
vkchaitu82,

Post the entire code here. What issues you are facing is not clear
Of course your BufferedWriter should also work.
I'm sure you used it youself many times.
The issue is somewhere else. Post your code.
>>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?
Avatar of sigma19

ASKER

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 .
SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sigma19

ASKER

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?
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?
So you want to be left with empty files?
Avatar of sigma19

ASKER

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

CEHJ, Yes I want to be left with empty files.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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


@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


:)