Solved

to make read-only

Posted on 1998-08-06
6
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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

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!

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

628 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