Solved

to make read-only

Posted on 1998-08-06
6
273 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delete Android all or certain applications data programmatically 9 106
jmss example java 2 31
ejb message driven bean mdb creation steps 2 34
Java List 4 32
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

685 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