Removing file locks in Windows and Java - java

I have a Java program that opens a file using the RandomAccessFile class.
I'd like to be able to rename that file while it is opened by Java. In Unix, this isn't a problem. Does anyone know how I can do this in Windows?
Should I set Java to open it a certain way?
Thanks in advance.
Edit, clarification: I'd like to be able to set this file sharing access in my Java code.

You need to close any handles on the file at any way. E.g. a FileOutputStream on that File must be closed before you can rename it. That's just the way how the Windows file system works.

in_stream.close();
out_stream.getChannel().close();
out_stream.close();
Anyhow in Windows it stay locked :*(

Related

How can I open an application for .txt in java (regardless of operating system )?

I should open a .txt file on my java application, so i've searched a way to do this but what i've found is only java calls to operating system, writing console commands. There is a way to open a .txt file independently of operating system of the user?
Note that I want to open an application that reads .txt, not only its content! I'm wondering if there is a way to call default application for reading .txt, without write for example "notepad myTxt.txt" on console from java :)
You can use Desktop.open()
From the Javadocs:
Launches the associated application to open the file.
http://docs.oracle.com/javase/6/docs/api/java/awt/Desktop.html#open(java.io.File)
An alternative (because you mentioned "editing") might also be Desktop.edit()
You'll most likely want to use:
FileReader and BufferedReader classes that work on any platform that Java supports. In fact, because Java runs on a virtual machine, simple IO is inherently platorm, OS independent. See this reference for ideas.

opening file with java application automatically

I want to create my own file format for a particular kind of file. When someone downloads this file I want their system to know it should be opened with my application.
For example when I download a .doc file, my computer asks me whether I want to save the file or open it with Open Office. Similarly, If that .doc file is sitting on my desktop, and I double click it, it automatically opens with the correct application.
I believe this has to do with associating the file extension with the application in the context of the underlying OS.
Can any one point me to some good resources about how to do this in java?
Thanks.
Edit:
Sorry I want to clarify. Is there a way I can have my application associate the file type with itself when it is installed?
Edit:
found this...
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/jdic_assoc/
platform independent solution
This source shows how to make a file association in windows:
http://www.rgagnon.com/javadetails/java-0592.html
You will probably have to do it per installer that you make in each OS.
found this... http://java.sun.com/developer/technicalArticles/J2SE/Desktop/jdic_assoc/ platform independent solution

A follow-up question for the unzip/zip in Java

I asked a question earlier about unzip/zip using windows cmd command in Java. That turn out to be not good for a program and one would suggest using the java.util.zip to unzip/zip. After looking through some tutorial, I found out usually they just loop through the content of the zip file. If I know the name of the file I want, can I extract it explicitly without looping through the whole zip file?? Here in my case, my zip file have 2300+ entries, and I would only want one of them. Do you guys happen to know some tricks? Thank you very much!
You will find hot to decompress files at: http://java.sun.com/developer/technicalArticles/Programming/compression/
Instead of looping and extracting all files just locate file you are interested in with ZipFile.getEntry() method.
You can get a ZipEntry -object for the specific file from ZipFile-object via the getEntry-method. To access the file contents, use ZipFile's getInputStream with the ZipEntry-object.
If this is for yourself and not your program, have you considered using the jar command in the JDK for this?

How to check whether the file is using/reading or not in JAVA?

I am using Java to develop an application, it needs to manage the file on the computer. The application have the ability/function to delete the file on the system. But I want to check whether the selected file is using/reading by another application or not first. Because I don't want to delete the file which is reading/using. How can I do so?
Maybe you could use tryLock()?
On Windows, you can't delete files which are in use ("locked"). Java itself doesn't offer an API to check.
If another application is using the file or actively reading it, then provided that application has done its job correctly (opening the file with a read lock), you won't be able to delete the file -- you'll get an IOException (specifically, a sharing violation). Catch the exception to know whether there was a problem.

Opening file from Java

What I need is to instruct OS to open the file with the default program used for that file type. Exactly as if, i.e., that file was double-clicked by user under Windows.
The purpose is, i.e., "your PDF file was generated. Click here to open it".
In platform-independent way, if possible...
I dont know exact terms for what I want, so if someone could update tags, I'd most appreciate that ;)
You need the Desktop class, and the open() method in particular.
Launches the associated application to
open the file. If the specified file
is a directory, the file manager of
the current platform is launched to
open it.
Since Java 6, we have Desktop.open() for exactly that purpose.

Categories