I am writing a program that reads files in a directory and then based upon the files last modofied date I write the file out to another directory. My problem is that when I read for ex. 7910 files, I bypass 7000 and write 910 to a new file. When I check the directory all 7910 are there and the ones I bypassed have 0 length (no data). The 910 are correct. This is part of the code I am using. Why are all the records being written?
try {
FileInputStream fis = new FileInputStream(fCurrent); FileReader fr = new FileReader(fCurrent);
int fileLength = (int) fCurrent.length(); byte[] buffer = new byte[fileLength]; int bytes_read;
String output; StringBuffer workArea = new StringBuffer();
String sCurrent = fCurrent.toString();
FileOutputStream fos = new FileOutputStream(subDirName + "\\" + fCurrent.getName()); FileWriter fw = new FileWriter(subDirName + "\\" + fCurrent.getName());
String holdString = fCurrent.toString(); File nfd = new File(holdString);
SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMdd"); Date date = new Date(nfd.lastModified()); String fileDate = df1.format(date);
int workDate1 = PreProcessorUtil.stringToInt(fileDate); int workDate2 = PreProcessorUtil.stringToInt(pfr.getPurgeFromDate()); int workDate3 = PreProcessorUtil.stringToInt(pfr.getPurgeToDate()); String returnFlag = "N";
if (workDate1 == workDate2) { returnFlag = "Y"; } if (workDate1 == workDate3) { returnFlag = "Y"; } if (workDate1 < workDate3) { if (workDate1 > (workDate2)) { returnFlag = "Y"; } }
if (returnFlag.equals("Y")) { while ((bytes_read = fis.read(buffer)) != -1) fos.write(buffer, 0, bytes_read); fis.close(); fos.close(); count++; } pfr.setPurgeCount(count);
You get the zero length files because you open the output file before you decide if you need it. When you do that and then close it you get the empty files. Move the open after
if (returnFlag.equals("Y"))
And then you'll only create those files you care about.
> FileInputStream fis = new > s = new FileInputStream(fCurrent); > FileReader fr = new FileReader(fCurrent); > > int fileLength = (int) fCurrent.length(); > byte[] buffer = new byte[fileLength]; > int bytes_read; > > String output; > StringBuffer workArea = new StringBuffer(); > > String sCurrent = fCurrent.toString(); > >FileOutputStream fos = new > s = new FileOutputStream(subDirName + "\\" + > fCurrent.getName()); > FileWriter fw = new FileWriter(subDirName + "\\" + > "\\" + fCurrent.getName()); > > String holdString = fCurrent.toString(); > File nfd = new File(holdString); > > SimpleDateFormat df1 = new > 1 = new SimpleDateFormat("yyyyMMdd"); > Date date = new Date(nfd.lastModified()); > String fileDate = df1.format(date); > > int workDate1 = > Date1 = PreProcessorUtil.stringToInt(fileDate); > int workDate2 = > Date2 = > PreProcessorUtil.stringToInt(pfr.getPurgeFromDate()); > int workDate3 = > Date3 = > PreProcessorUtil.stringToInt(pfr.getPurgeToDate()); > String returnFlag = "N"; > > > if (workDate1 == workDate2) > { > returnFlag = "Y"; > } > if (workDate1 == workDate3) > { > returnFlag = "Y"; > } > if (workDate1 < workDate3) > { > if (workDate1 > (workDate2)) > { > returnFlag = "Y"; > } > } > > if (returnFlag.equals("Y")) > { > while ((bytes_read = fis.read(buffer)) != -1) > fos.write(buffer, 0, bytes_read); > fis.close(); > fos.close(); > count++; > } > pfr.setPurgeCount(count);