issue with multiple files merging into 1
843789Jul 13 2009 — edited Jul 13 2009I'm trying to write some code that will read the contents of multiple files in a directory then output it into a single file. However when I try to open the files via a for loop it does not recognise the variable I have assigned for the filename:
FileReader fr = new FileReader(fileName);
please see the full program so far, it isn't finished and this currently has me beat, any help would be greately appreciated.
Thanks
Chris
import java.io.*;
public class OutputCCFile
{
public static void main(String []args)
{
File absolutePath = new File("C:\\java\\Credit Cards");
String[] ccFiles = absolutePath.list();
try
{
boolean newFile = false;
CCHeaderLine cch = new CCHeaderLine();
String strDate = cch.getDate();
String headerLine = cch.getHeader();
File file = new File("TUHCC_"+strDate);
FileWriter fw = new FileWriter(file);
fw.write(headerLine);
fw.flush();
fw.close();
}catch(IOException e) {}
for (int i = 0; i<ccFiles.length;i++)
{
String filename = ccFiles;
filename.toUpperCase();
if (filename.startsWith("C")) //only want to open files beginning with C
{
FileReader fr = new FileReader(fileName);
/*Exception in thread "main" java.lang.Error: Unresolved compilation problem:
fileName cannot be resolved */
BufferedReader br = new BufferedReader(fr);
String dataLine = br.readLine();
}
}
}
}