473,386 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

InputStream help

Ok I'm pretty much copying files in one directory to another the problem I have is that it can only run once, I'm not sure why, possibly I'm forgetting to close a file or something but I can't seem to spot it any advice?

Expand|Select|Wrap|Line Numbers
  1.     public void copyDirectory() throws Exception
  2.     {
  3.         File listOfFiles[] = directoryA.listFiles();
  4.  
  5.         while(filePosition < listOfFiles.length)
  6.         {
  7.             int len;
  8.             filea = listOfFiles[filePosition];
  9.             InputStream in = new FileInputStream(filea);
  10.             String targetFilePath = directoryB.getAbsolutePath() + "\\" + listOfFiles[filePosition].getName();
  11.             File targetFile = new File(targetFilePath);
  12.             if(targetFile.exists())
  13.             {
  14.                 if(targetFile.lastModified() < filea.lastModified())
  15.                 {
  16.                     OutputStream out = new FileOutputStream(targetFilePath);    
  17.                     byte[] buf = new byte[1024];
  18.                     while ((len = in.read(buf)) > 0)
  19.                     {
  20.                         out.write(buf, 0, len);
  21.                     }
  22.                     in.close();
  23.                     out.close();
  24.                     len = 0;
  25.                 }
  26.             }
  27.             if(!targetFile.exists())
  28.             {
  29.                 OutputStream out = new FileOutputStream(targetFilePath);    
  30.                 byte[] buf = new byte[1024];
  31.                 while ((len = in.read(buf)) > 0)
  32.                 {
  33.                     out.write(buf, 0, len);
  34.                 }
  35.                 in.close();
  36.                 out.close();
  37.                 len = 0;
  38.             }
  39.             filePosition ++;
  40.         }  
  41.     }
Nov 22 '08 #1
2 1630
It looks to me like you haven't initialized filePosition. Try setting it to 0 at the beginning of the method.
Nov 22 '08 #2
JosAH
11,448 Expert 8TB
This isn't an error but what I personally find very ugly is this:

Expand|Select|Wrap|Line Numbers
  1. if(targetFile.exists())
  2.             {
  3.                 if(targetFile.lastModified() < filea.lastModified())
  4.                 {
  5.                    // copy the file
  6.                 }
  7.             }
  8. if(!targetFile.exists())
  9.    // copy the file
  10.  
Why not make it like this:

Expand|Select|Wrap|Line Numbers
  1. if(!targetFile.exists() || targetFile.lastModified() < filea.lastModified())
  2.    // copy the file
  3.  
kind regards,

Jos

ps. the previous poster pointed you to the mistake.
Nov 23 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Seong Jin, Cho | last post by:
Hi. Is there a way to get an unbuffered InputStream from Process? I found out that java.lang.Win32Process and java.lang.UNIXProcess both wraps the stdout with java.io.BufferedInputStream, but...
3
by: Rakesh Sinha | last post by:
I have a very trivial question. But I searched in google / archives of this group to get the answer, checked the C++ FAQ - but did not precisely what I was looking for. The problem is with...
1
by: Charles.Deisler | last post by:
XmlTextReader requestdata = new XmlTextReader(Request.InputStream); XmlTextWriter xmltextwriter = new XmlTextWriter(somefile,someencoding); XmlTextWriter xmltextwriter2 = new...
0
by: bvasanth123 | last post by:
Hi, I would like to know how can I read an InputStream into Excel object. In my web app, user uploads MS-Excel file though asp.net html file control. I get UploadedFile.PostedFile.InputStream()...
0
by: bvasanth123 | last post by:
Hi, I would like to know how can I read an InputStream into Excel object. In my web app, user uploads MS-Excel file though asp.net html file control. I get UploadedFile.PostedFile.InputStream()...
1
by: samtilden | last post by:
I want to print out some tracing messages from Global.asax.cs/Application_BeginRequest(). I easily got: System.Web.HttpContext.Current.Request.RawUrl, and...
1
by: O.B. | last post by:
I have the need to read a file (version.txt) that is embedded within an WAR file that is within an EAR file: foo.ear contains bar.war contains resources/version.txt I am able to get the bar.ear...
0
by: raheel javed | last post by:
i am using mobile for communication with live server when mobile send its information to server the input stream is encoded in utf-8 that is converted into string but it is still not...
1
by: madumm | last post by:
Hi all; Can anybody tell me how to convert a InputStream into a Char array?? InputStream foData; foData into a Char array?? Thanx madumm
11
by: John Krukoff | last post by:
-----Original Message----- First off, ignore castironpi, it's a turing test failure. Second, I'm curious as to how Java manages this. I'd think their streams would have to be pretty magic to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.