File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Making a Read-Only File Writeable Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Making a Read-Only File Writeable" Watch "Making a Read-Only File Writeable" New topic
Author

Making a Read-Only File Writeable

Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
I'm sort of looking for some collaboration here. I know that java.io.File is cannot be made writeable after it's been setReadOnly(); So I made a class RWFile (Read-Write-File) that extends java.io.File and adds one method: boolean setWriteable(). I have already implemented this for windows (using JNI) but wondered if other people out there wanted to contribute code for making it work on Linux/Unix/MacOSX. I'm posting all my code below including instructions on how to compile, etc. But can someone out there on those OS's contribute that code? I'll even put it all together and maybe we could set up a sourceforge thing or something.
The cool thing is, now you can use the feature of making files read-only, because now you can set them back to writeable! So have fun with this, and anyone who solves it for the other OS's please post it here!


INSTRUCTIONS
--------------------------
1. Compile the RWFile.java with:

javac RWFile.java
2. You need to create the C/C++ Header which is basically the interface (with some precompiling stuff put in). Don't worry, just do this:
javah -classpath . -jni RWFile
(NOTE!! Even though Sun's tutorial says you can do it without the classpath, forget it, I've never gotten that to work. )
This outputs a file called "RWFile.h". Your C code needs to be able to find this at compile time.
3. Compile the C code:
cl -I<Java_SDK_Home>\include -I<Java_SDK_Home>\include\win32 -LD RWFile.c -FeRWFile.dll
So if you have your SDK at the folder C:\Java2\, then you'd have this:
cl -IC:\Java2\include -IC:\Java2\include\win32 -LD RWFile.c -FeRWFile.dll
4. Run the test program:

java RWFile "testFile.txt"
It should print out:
CAN WRITE: true
Setting read only...
true
CAN WRITE: false
Setting writeable...
true
CAN WRITE: true
That's it!
Robert
[ February 06, 2003: Message edited by: Robert Paris ]
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
OK, I created a sourceforge project! Anyone who wants to contribute let me know.
http://sourceforge.net/projects/rwfile-java/
Also, I added a jar file with the source code and a test (currently only works on windows files). All you have to do is download the file, save it and double-click! I set it up to load the library from the jar file so you have only one file to download.
NOTE: Don't run it straight from a download window, it won't work since at that point it's a temp file or something. Save it and run it from the file system. java -jar RWFile.jar will also work.
The SourceForge Page
 
 
subject: Making a Read-Only File Writeable
 
Threads others viewed
HTTP 405 Resource not allowed
File Not Found Problem
How to get upload an image on server and get it on next jsp?
How to download csv file
Is there any free libraries avaialble for file splitting usiong java??
MyEclipse, The Clear Choice

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture