473,698 Members | 2,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

InputStream help

67 New Member
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 1649
Aardsquid
4 New Member
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 Recognized Expert MVP
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
8531
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 I need an unbuffered one.
3
4314
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 respect to reading 'long double's from a stream separated by comma. #include <iostream>
1
2116
by: Charles.Deisler | last post by:
XmlTextReader requestdata = new XmlTextReader(Request.InputStream); XmlTextWriter xmltextwriter = new XmlTextWriter(somefile,someencoding); XmlTextWriter xmltextwriter2 = new XmlTextWriter(somefile2,someencoding); xmltextwriter.WriteNode(requestdata, false); // i want to write to another writer here by resetting the reader xmltextwriter2.WriteNode(requestdata, false);
0
2197
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() where UploadedFile is the name of file control. Not I have InputStream. How can I read into Excel object for parsing as excel file. Regards, Vasanth
0
1618
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() where UploadedFile is the name of file control. Not I have InputStream. How can I read into Excel object for parsing as excel file. (Language VB.NET) Regards, Vasanth
1
8108
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 System.Web.HttpContext.Current.Request.Params. I would now like to get the entire:
1
4582
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 file as an inputstream as follows: import java.util.zip.ZipEntry; import java.util.zip.ZipFile; ZipFile zipFile = new ZipFile("foo.ear");
0
1240
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 understandable so chk it what should i do to convert such type of data Stream str; string strmContents= ""; int counter, strLen, strRead;
1
3927
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
1623
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 pull this off after the HTTP connection has already been built. Anyway, as I see it, this is more of a HTTP protocol question. I think what you need to do is set the HTTP Range header to bytes=-128, see the urllib2
0
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.