[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

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(..);
0
vkchaitu82
Asked:
vkchaitu82
  • 7
  • 4
  • 3
  • +1
2 Solutions
 
for_yanCommented:
PrintStream psout = new PrintStream(new FileOutputStream("output.txt"));
psout.println(str);

Thius will printout your line
0
 
for_yanCommented:
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
0
 
Amitkumar PSr. ConsultantCommented:
vkchaitu82,

Post the entire code here. What issues you are facing is not clear
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
for_yanCommented:
Of course your BufferedWriter should also work.
I'm sure you used it youself many times.
The issue is somewhere else. Post your code.
0
 
CEHJCommented:
>>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?
0
 
vkchaitu82Author 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 .
0
 
for_yanCommented:
import java.io.*;

public class CombineFiles {


    public CombineFiles () {

        try {

            PrintStream psout = new PrintStream(new FileOutputStream("C:\\temp\\test\\outtt.txt"));
            DataInputStream in = new DataInputStream(new FileInputStream("C:\\temp\\test\\file1.txt"));
            String buff = null;
            while((buff = in.readLine()) != null){
                       psout.println(buff);

            }
            in.close();
                 in = new DataInputStream(new FileInputStream("C:\\temp\\test\\file2.txt"));

            while((buff = in.readLine()) != null){
                       psout.println(buff);

            }
            in.close();
          
              File f = new File("C:\\temp\\test\\file2.txt");
              f.delete();

            psout.close();

               psout = new PrintStream(new FileOutputStream("C:\\temp\\test\\file2.txt"));

            psout.println("");
              psout.close();

               f = new File("C:\\temp\\test\\file1.txt");
              f.delete();

            psout.close();

               psout = new PrintStream(new FileOutputStream("C:\\temp\\test\\file1.txt"));

            psout.println("");
              psout.close();

            





        } catch(Exception ex){

            System.out.println("error " + ex.toString());
            ex.printStackTrace();
        }

    }

    public static void main(String [] args){
        new CombineFiles();
    }

}

Open in new window

0
 
vkchaitu82Author 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?
0
 
for_yanCommented:
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?
0
 
CEHJCommented:
So you want to be left with empty files?
0
 
vkchaitu82Author 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.
0
 
CEHJCommented:
Try the following - works for me

http://technojeeves.com/joomla/index.php/free/74-string-list
import java.util.*;
import java.io.*;

public class FileProcesser {
    public static void main(String[] args) {
	process(new String[] { "f1", "f2" }, "f3");
    }

    public static void process(String[] files, String targetFile) {
	StringList target = new StringList();
	for(String f : files) {
	    StringList sl = new StringList();
	    sl.read(f);
	    target.addAll(sl);
	    sl.clear();
	    sl.save(f);
	}
	target.save(targetFile);
    }
}

Open in new window

0
 
for_yanCommented:
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
0
 
for_yanCommented:


@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


0
 
CEHJCommented:
:)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 7
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now