import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
public class TestConcatenateFiles {
public static void main(String[] args) {
String relativeSourceFileName = "Test1.txt";
String sourceDirPath = "C:\\Source";
String destDirPath = "C:\\Destination";
processResultInSourceDirIfExistsInDestDir(relativeSourceFileName, sourceDirPath, destDirPath);
}
// See if file 'relativeSourceFileName' exists with earlier file date in 'destDirPath' location
// if it does, than read the file from 'destDirPath' and concatenate to this file the same filename
// in 'sourceDirPath'
private static void processResultInSourceDirIfExistsInDestDir(String relativeSourceFileName, String sourceDirPath, String destDirPath){
// See if file exists in the destination folder:
File fileOnServer = new File(destDirPath + "\\" + relativeSourceFileName);
String line = "";
ArrayList<String> linesListResultFile = new ArrayList<String>();
if(fileOnServer.exists() && !fileOnServer.isDirectory()) { // If file exists and it is not directory,
// then concatenate current test results to the file on server and save it at local
// folder to be backed up later on server.
try {
// FileReader reads text files in the default encoding:
FileReader fileReader = new FileReader(sourceDirPath + "\\" + relativeSourceFileName);
// wrap FileReader in BufferedReader:
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
linesListResultFile.add(line);
}
// Close file
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"File not found, unable to open file '" +
sourceDirPath + "\\" + relativeSourceFileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ sourceDirPath + "\\" + relativeSourceFileName + "'");
}
}
System.out.println();
// Append to end of file on server, in a try-catch:
try{
// Open file on server, and with append = true setting concatenate to file:
FileWriter fileWriter = new FileWriter(fileOnServer.getName(),true);
BufferedWriter bufferWriter = new BufferedWriter(fileWriter);
Iterator it = linesListResultFile.iterator();
while(it.hasNext()){
bufferWriter.write((String) it.next());
}
bufferWriter.flush();
bufferWriter.close();
System.out.println("Done writing file");
}catch(IOException e){
System.out.println("Error writing file on server");
// e.printStackTrace();
}
} // End of processResultInSourceDirIfExistsInDestDir() method
}
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE