?
Solved

to make read-only

Posted on 1998-08-06
6
Medium Priority
?
292 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 20 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

719 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