Solved

to make read-only

Posted on 1998-08-06
6
260 Views
Last Modified: 2010-04-16
How do write a Java program to  change the attrib of any text file to Read-Only. I tried using File Permission and File class, but i am not able to do that. Could any body suggest.. please... I use jdk1.2beta3 on Win95.
0
Comment
Question by:hvhemant
  • 3
  • 3
6 Comments
 

Author Comment

by:hvhemant
ID: 1231476
Edited text of question
0
 

Accepted Solution

by:
awilkins earned 10 total points
ID: 1231477
Create a new File object representing the text file.  Then call setReadOnly().  For example,

File myFile = new File ("myFile.txt");
myFile.setReadOnly();

If you wish to do this for multiple files, create a new FilenameFilter that only accepts text files and call
  myDir.list(myFilenameFilter);
where myDir is the directory in which your text files reside and myFilenameFilter is your FileName Filter that only accepts text files.  Code for this filter could be as follows:

import java.io.FilenameFilter;
public class myFilenameFilter implements FilenameFilter
{
  public boolean accept(File dir, String name)
  {
    return (name.endsWith(".txt"));
  }
}

This file will accept only files with the extension ".txt", rejecting all others.
Hope this helps somewhat --
              Alexander Wilkins
0
 

Author Comment

by:hvhemant
ID: 1231478
awilkins,

thanks for the reply.
setReadOnly() method is available in Permissions class of java.security and not in File class. I tried your sample, it comes out with an error saying method not found in java.io. Even I tried using Permissions  and FilePermission classes, but I am not able to set.....

here is a sample code what i tried for your ref....

File ff = new File("test.txt");
Permissions p = new Permissions();
FilePermission fp = new FilePermission("test.txt", "read");
p.add(fp);
p.setReadOnly();
boolean rd = p.isReadOnly();
System.out.println(rd);

in above sample rd(boolean) comes out with true, but it is not setting  the file to readonly.
please suggest to solve this.
I need also how to set  archive bit of a file to on/off.

bye
      
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Expert Comment

by:awilkins
ID: 1231479
Sorry -- I just checked and the java.io.File.setReadOnly() method was first implemented in jdk 1.2.  What compiler and version are you using? An IDE?
  -- Alexander Wilkins
0
 

Author Comment

by:hvhemant
ID: 1231480
Alexander,
thanks for the reply.
I use windows simple text editor for coding and compile  on command prompt. OS is Windows 95. JVM (javac.exe) is jdk1.2beta3.

thank you and bye.
0
 

Expert Comment

by:awilkins
ID: 1231481
I'm using 1.2beta4, WinNT, also with a text editor & the command prompt.  I would think that java.io.File.setReadOnly() would also be present in 1.2beta3, but maybe not.  If it isn't, you might consider upgrading to the next beta version.
  A workaround might be to set the readOnly and archive attributes from a batch file.  The command to set these attributes from the command line is
                   attrib [+r|-r] [+a|-a] [+s|-s] [+h|-h][[drive:][path] filename]
where something like
                  attrib +r -a myFile.txt
would make myFile.txt read-only but remove its archive attribute.  Also, java does not seem to have any way of reading/setting the archive setting of a file, probably because it is windows-specific.

  Hope this is helps some --
                   Alexander Wilkins
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 36
check java version using powershell 13 93
hibernate jars 4 31
spring jars download 1 27
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question