File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes append method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "append method" Watch "append method" New topic
Author

append method

jake mullet
Greenhorn

Joined: May 09, 2001
Posts: 15
first of thanks to people who help you guys make this stuff so much funner and easier to understand, now here is what i have come up with so far. I am trying to read these two files and then put them together and call them builtfile.txt right now it writes the first file fine, but instead of appending file2 to the end of the new file it overwrites it, how can i make this into a String buffer instead so I can use the appends() thanks again JM
import java.io.*;
import java.util.*;
public class testFile {
public static void main(String[] args){
File f1 = new File("C:\\file1.txt");
File f2 = new File(C:\\file2.txt");
File outFile = new File("C:\\builtfile.txt");

if(f1.exists() && f2.exists()) {
System.out.println("yes there are two files");
try {
FileReader inFlRd = new FileReader(f1);
BufferedReader bufInput = new BufferedReader(inFlRd);
FileWriter outFlWr= new FileWriter(outFile);
BufferedWriter bufOutput = new BufferedWriter(outFlWr);
String line;

line = bufInput.readLine();

while (line != null) {
bufOutput.write(line, 0, line.length());
bufOutput.newLine();
line = bufInput.readLine();

}//end while

bufInput.close();
bufOutput.close();
}//end try
catch (IOException e) {
e.printStackTrace();
}
try {
FileReader inFlRd2 = new FileReader(f2);
BufferedReader bufInput = new BufferedReader(inFlRd2);
FileWriter outFlWr= new FileWriter(outFile);
BufferedWriter bufOutput = new BufferedWriter(outFlWr);
String line;

line = bufInput.readLine();

while (line != null) {
bufOutput.write(line, 0, line.length());<----this overwrites!
bufOutput.newLine();
line = bufInput.readLine();

}//end while

bufInput.close();
bufOutput.close();
}//end try
catch (IOException e) {
e.printStackTrace();
}






}//end if
else {
System.out.println("Process ran ran normal, only one File exists");
}
}//end main
}//end class
[This message has been edited by jake mullet (edited June 06, 2001).]
[This message has been edited by jake mullet (edited June 06, 2001).]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

First problem: your main() is way too busy. Try to make your methods more cohesive. That is, make each method do one and only one thing. This helps you break down the problem into manageable chunks.
Main problem: cut and paste got you. You have two options:
Don't create a new BufferedWriter for the second input file, just continue adding lines to the one you used for the first input file.
Or, use the FileWriter(String fileName, boolean append) constructor for the second file.
Junilu


Junilu - [How to Ask Questions] [How to Answer Questions]
jake mullet
Greenhorn

Joined: May 09, 2001
Posts: 15
Thanks alot for your response but I am confused as to how to use the same BufferedWriting, i dont use an append method? So i should go :
bufOutput = null;
FileReader inFlRd2 = new FileReader(f2);
BufferedReader bufInput2 = new BufferedReader(inFlRd2);
FileWriter outFlWr2 = new FileWriter(line);
thanks JM
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Jake, here's an excerpt from the middle of your code

I believe if you eliminate lines 1, 2, and 3, you will get the output you are looking for.
jake mullet
Greenhorn

Joined: May 09, 2001
Posts: 15
Thanks for the response scott, but I tried exactly what you said but the second file overwrites the first not appends to the end. any thoughts. Im sstill trying stuff in the meantime JM
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
A try-catch block has its own scope, correct? If so, you might try declaring the BufferedWriter outside of the original try-catch block. It may be going out of scope between the 2 try-catch blocks.
jake mullet
Greenhorn

Joined: May 09, 2001
Posts: 15
Thanks scott, you were exactly right and i appreciate your help, it does write the first file out now and then append the second to it!!! Wonderful, I only have one more issue to resolve and I will tryi to work it out myself, but if i get stumped ...ill be back
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: append method
 
Similar Threads
Question about File, FileWriter + BufferedWriter
tricky question
comparing the file before i append it to the other
RandomAccessFile()
scope problem