Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cat files

Status
Not open for further replies.

stefanwagner

Programmer
Oct 19, 2003
2,373
DE
I have to append some files together.
The easy description looks like that:

On linux I may say:

cat f.1 f.2 > f.3

On dos i may say

copy -b f.1 + f.2 f.3

Is there an easy way to do something like that in java?

The real description is, that I have to put a String between,
so in pseudocode it is:
file.c = file.a + " some text " + file.b

Of course I can write 'some text' to file.d and use method one if there is one.
And of course I could read file.a, write it to file.c, append text 'some text', read file.b and append it to file.c.

This looks a little bit ugly to me and might be inefficient.
I looked at File, FileChannel, FileWriter and FileOutputStream, but there seems to be nothing close to my Problem.

I remember that this things where easy with c/ c++, but platformdependend of course.

 
Try this ....

String filesIn = "file1.txt file2.txt";
String fileOut ="file3.txt";

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(fileOut)));

Code:
StringTokenizer files = new StringTokenizer(filesIn, " ");
while (files.hasMoreTokens()) {
	File fileIn = new File(files.nextToken());
	BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileIn));
	byte[] buffer = new byte[(int)fileIn.length()];
	bis.read(buffer, 0, buffer.length);
	bos.write(buffer, 0, buffer.length);
	bos.write("\n".getBytes());
	bos.write("this is some text I put in the middle of a file".getBytes());
	bos.write("\n".getBytes());
	bis.close();
}

bos.flush();
bos.close();
 
I'm afraid, that I did lead you in the wrong direction with my Pseudocode.

I know how to write into a Stream/ File.
I meant to write the context of 'file.a' to 'file.c', append some text, and then append 'file.b'.

I know how to do this by reading file.a and file.b in and writing them bytewise, or Line-by-Line, or bufferwise.

I'm looking for something like (again pseudocode, but more javalike:)
File file_a, file_c, file_b;
file_a = new File ("f.1");
// ...
file_a.copy_to (file_c);
file_c.append ("some text");
file_c.append (file.b);
 
>>>> file_a.copy_to (file_c);
Read file_a in (using code as above or whatever) and write out the data to file_c.

>>>>> file_c.append ("some text");
Well - the example above does this ... but you can also do it by calling the constructor of FileOutputStream
Code:
FileOutputStream(File file, boolean append)

>>> file_c.append (file.b);
The example above does this.

I think the example does do all of the things you require - you just need to modify it a bit - have you tried running it ? file3.txt has the contents of file1.txt, with some appened text in the middle, and then the contents of file2.txt.

Is this not what you wish ?!!
 
No, sorry - it's not.

I have been on the search for a more elegant solution than mine (I already have a working solution).

I thought there might be an solution in the libs somewhere, which I didn't find.

In my first posting I wrote:
'And of course I could read file.a, write it to file.c, append text 'some text', read file.b and append it to file.c.'
And your answer is an Implementation of that.

I hope it was just cut'n paste.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top