Solved

to make read-only

Posted on 1998-08-06
6
240 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now