File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

append method

 
jake mullet
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 6571
24
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
jake mullet
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic