This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Program to check whehter a file is read-only / write Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Program to check whehter a file is read-only / write " Watch "Program to check whehter a file is read-only / write " New topic
Author

Program to check whehter a file is read-only / write

pavang kumar
Greenhorn

Joined: May 27, 2010
Posts: 11
Hi

I need a program where to check the files present in directory are read-only/write and return read only list .Is there a way we can do this in java .If so can you provide .. me .
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

java.io.File


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
pavang kumar
Greenhorn

Joined: May 27, 2010
Posts: 11
Yup can you provide me some approach,how to know whether a file is read-only/write in java. and can we open multiple files in java using a single stream.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

pavang kumar wrote:Yup can you provide me some approach,how to know whether a file is read-only/write in java.

Have you checked the methods in that class? Hint: check the ones that start with "can" and "is".

and can we open multiple files in java using a single stream.

No you can't. You can use list() and listFiles() to list the children of a folder, but always check if these methods return null - that means you can't read the contents.
pavang kumar
Greenhorn

Joined: May 27, 2010
Posts: 11
Then whats the alternative for that.. can you suggest me , then you said uisng i/o we can do , them how is it possible.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

List all methods that start with "can" or "is" and tell me if you think that they can help you or not. Tell me why (not).
Jitendra K. Patel
Greenhorn

Joined: Aug 24, 2009
Posts: 2
import java.io.*;

public class FileTest {

public static void main(String[] args) {
File f = new File("file-name");
if(f.canRead() == true)
System.out.println("Read Only");
if(f.canWrite() == true)
System.out.println("\nWrite Only");
}

}

Using this program you can able to check whether file is read-only or write-only.

File class has many properties for that you can check it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Those were the methods I was thinking about, but your print-outs are incorrect. canRead() doesn't indicate that the file is read-only, and canWrite() doesn't indicate that the file is write-only.
Jitendra K. Patel
Greenhorn

Joined: Aug 24, 2009
Posts: 2
Rob Prime wrote:Those were the methods I was thinking about, but your print-outs are incorrect. canRead() doesn't indicate that the file is read-only, and canWrite() doesn't indicate that the file is write-only.


i have checked it and it works properly...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

canRead() does not return true if the file is read-only; a file is read-only if it can be read but not written, i.e. canRead() returns true and canWrite() returns false.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Program to check whehter a file is read-only / write
 
Similar Threads
What does a "one to many" relationship mean
File.canWrite
Reading and Writing using a Large Random Access File
File permission
problem in canRead and canWrite