• Post Reply Bookmark Topic Watch Topic
  • New Topic

Making a Read-Only File Writeable  RSS feed

 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!