Article View: borland.public.jbuilder.java.api
Article #2266Re: File locked after stream closed
From: Torsten Welches
Date: Sun, 15 Apr 2001 17:47
Date: Sun, 15 Apr 2001 17:47
47 lines
2302 bytes
2302 bytes
Al,
the file should not be locked. Are you sure that the close() lines actually
execute? How do you handle the IOExceptions? If, say, the read or write
operations throw an exception your close will never get called.
It is better to ensure the close in a finally block. Something like this:
isURLStream = uDownloader.openStream();
try {
..
} finally {
osFileStream.close(); // closig file stream
isURLStream.close(); // closing connection to the URL content
}
Torsten
Al Mal schrieb in Nachricht <3ad7c343$1_2@dnews>...
>This nearly drove me crazy. I have a piece of code which runs under Win2K
>where I read from a URL and save to a file.
>
> URL uDownloader;
> InputStream isURLStream;
> FileOutputStream osFileStream;
>
> uDownloader = new URL(upParser.GetProtocol(),
> upParser.GetHost(),
>
>upParser.GetDir()+upParser.GetFileName());
> isURLStream = uDownloader.openStream();
> osFileStream = new FileOutputStream(upParser.GetFileName());
>
> while (iChunk > 0)
> {
> iChunk = isURLStream.read(bBuffer, 0, 102400);
> osFileStream.write(bBuffer, 0, iChunk);
> }
>
> osFileStream.close(); // closig file stream
> isURLStream.close(); // closing connection to the URL content
>
>The problem is that the resulting file is kept locked until I exit from the
>application. I need it unlocked - how?
>
>Alex
>
>
Technical Details
Message-ID:
<3ad9de16_1@dnews>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!logbridge.uoregon.edu!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnewsReferences:
<3ad7c343$1_2@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,ea1112c189c5d22f X-Google-Attributes: gid1089d0,public X-Google-ArrivalTime: 2001-04-15 11:14:07 PST From: "Torsten Welches" <torsten.welches@who.cares.netcologne.de> Newsgroups: borland.public.jbuilder.java.api References: <3ad7c343$1_2@dnews> Subject: Re: File locked after stream closed Date: Sun, 15 Apr 2001 19:47:52 +0200 Lines: 49 X-Newsreader: Microsoft Outlook Express 4.72.3110.5 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 NNTP-Posting-Host: 195.14.226.194 Message-ID: <3ad9de16_1@dnews> X-Trace: dnews 987356694 195.14.226.194 (15 Apr 2001 10:44:54 -0700) Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!logbridge.uoregon.edu!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews