• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Combining multiple inputstreams into one file always cause

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I got this code below that combines multiple file input streams into one file which works just fine. I close all the streams. Then I want the old fiels moved to an archive folder. ITs here where they always give me an error "
The process cannot access the file because it is being used by another process." What else do I need to close or do here?(I am using java 7 btw).

 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Steve,

A couple of things.

- You're using Java 7, so make use of try-with-resources.
- Vectors are obsolete. Use Lists instead. In this case, you don't need *any* collection if you just open one file at a time.
- Your new file path depends on the path of the last file you opened. Is this *really* what you want? Just make the client responsible for the complete path.
- Please don't use File for paths. Use Path for paths.
- Your method name betrays its implementation. It's really not that interesting for the outside world to know that you're using InputStreams to read the files.
- On the other hand, your method name does not say that it's going to archive the files used. That seems rather important.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A few more things:

- BufferedWriter has a newLine() method.
- Files has newBufferedReader() and newBufferedWriter() methods.
- I would first copy all the lines to a temporary file, and if the operation succeeds, move the file to the final location.
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I would first copy all the lines to a temporary file, and if the operation succeeds, move the file to the final location.


I think you are taking assumption for larger files.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That has nothing to do with file sizes. This measure is to prevent corrupt files from appearing if the operation fails.
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with your point but i am thinking if having large file say several GBs then it will waste lots of memory first to store at temp place.
 
Saloon Keeper
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Hi Steve,

A couple of things.

- Vectors are obsolete. Use Lists instead.



I wouldn't count them as "obsolete". If they were that, then they'd be marked "deprecated". However - as the JavaDocs indicate - they're no longer the preferred list implementation. Vectors carry thread-safety overhead, so an ArrayList is a lighter option when you don't need thread safety. Vector was retor-fitted to better integrate with the Collections (which didn't exist when Vector was first defined). Which is why Vector has 2 ways to traverse - the original Enumeration and the newer (Collection-style) Iterator.

Stephan van Hulsy wrote:
- Please don't use File for paths. Use Path for paths.



What is "Path"? Can you provide the fully-qualified classname? Because I've used java.io.File to hold and manipulate paths for years and I'm not aware of anything having replaced it.

The java.io.File class is perhaps imprecisely-named anyway. You don't really do I/O on a java.io.File, you use it as a basis for opening an I/O channel.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You perform these close operations:


Think about this - how does the stream opened with the first file get closed?

Bill
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote:I agree with your point but i am thinking if having large file say several GBs then it will waste lots of memory first to store at temp place.



You're not wasting any space. There's also hardly any memory involved. The goal is to store one large file. Moving a temporary file to its final location doesn't make a second copy, it simply edits a small entry in the file system index.

Try moving a huge file from one place on the filesystem to another place on the same filesystem. You'll find that it happens instantaneously.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Vectors carry thread-safety overhead, so an ArrayList is a lighter option when you don't need thread safety.


Huh, for some reason I always though java.util.concurrent would have some sort of concurrent list.

What is "Path"? Can you provide the fully-qualified classname? Because I've used java.io.File to hold and manipulate paths for years and I'm not aware of anything having replaced it.


java.nio.file.Path. In particular, the Paths and Files utility classes have a lot of nice helper methods.

Here's an example:
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stephan..
 
reply
    Bookmark Topic Watch Topic
  • New Topic