We help IT Professionals succeed at work.
Get Started

Java: Unable to Concatenate to file using  BufferedWriter and FileWriter

AttilaB
AttilaB asked
on
364 Views
Last Modified: 2015-03-03
I am trying to use BufferedWriter and FileWriter to concatenate the text contents of a file located in "C:\\Source"
to a file of a same name in the "C:\\Destination" directory. I am reading the contents of the source file into an ArrayList<String> , and trying to write it to attach it to the end of the file in the "C:\\Destination" directory.
I set the append parameter of FileWriter to true.

The problem is that the destination file never gets written.  What am I doing wrong here?

The code:
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
            
}

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Distinguished Expert 2020
Commented:
This problem has been solved!
Unlock 1 Answer and 11 Comments.
See Answer
Why Experts Exchange?

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.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE