The Artima Developer Community
Sponsored Link

Java Answers Forum
Writing files in Java

1 reply on 1 page. Most recent reply: Apr 14, 2003 3:07 PM by Rich Burgis

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Bruce Jacobs

Posts: 4
Nickname: comet304
Registered: Apr, 2003

Writing files in Java Posted: Apr 14, 2003 1:50 PM
Reply to this message Reply
Advertisement
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);


Rich Burgis

Posts: 17
Nickname: songbird
Registered: Mar, 2003

Re: Writing files in Java Posted: Apr 14, 2003 3:07 PM
Reply to this message Reply
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);

Flat View: This topic has 1 reply on 1 page
Topic: 2 really easy questions Previous Topic   Next Topic Topic: Java Editor...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use