how do we know that a file is read protected? - java

In Java on Windows, how do we know that a file or folder is read-protected?
I try with canRead() et setReadable() :
String pathFile = "D:/Folder";
File f = new File(pathFile);
System.out.println("Is Protected => " + f.setReadable(true)==true);
but it not solve my problem
Thank you

canWrite method is true if and only if the file system actually contains a file denoted by this abstract pathname and the application is allowed to write to the file; false otherwise.
File lFile = new File("Sample.txt")
lFile.canWrite();

Related

Access File from Removeable Storage in Android

I am trying to access a file placed in my remove able memory card.
What I am using is
File f=new File("/mnt/sdcard/CAPP/"); // f.exists() returns True
File f=new File("/mnt/sdcard/CAPP/myfile.apk"); // f.exists() returns False
Any idea why its happening so ? Although myfile.apk is placed in CAPP folder.
Thanks in advance.
Please try this...
String getFile = Environment.getExternalStorageDirectory().toString() + "folder name" + fileName;
File file = new File(getFile );
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "path/to/the/file/inside/the/myfile.apk");

How to create a text file inside a specific directory?

I am trying to cretae a file SYS_CONFIG_FILE_NAME inside a specific directory SYS_CONFIG_DIR_NAME. using the below posted code, when i run the java program it creates two directories instead of one directory and one text file inside that directory.
The out put of the below code is
SYS_CONFIG/config.txt. But `config.txt` is not a text file it is just a directory named `config.txt`
i referred also to some question in stackoverflow but i could not find a solution. Please let me know what I am missing?
code:
private final static String SYS_CONFIG_DIR_NAME = "SYS_CONFIG";
private final static String SYS_CONFIG_FILE_NAME = "config.txt";
private static File newSysConfigInstance() throws IOException {
// TODO Auto-generated method stub
File f = new File(SYS_CONFIG_FILE_PATH + "/" + SYS_CONFIG_DIR_NAME + "/" + SYS_CONFIG_FILE_NAME);
f.mkdirs();
f.createNewFile();
return f;
}
I would do it that way, you have always to call createNewFile() to create a new instance of the file if it is not created.
File dir = new File(SYS_CONFIG_FILE_PATH, SYS_CONFIG_DIR_NAME);
f.mkdirs(); // this to create the directories need for your path.
File file = new File(dir, SYS_CONFIG_FILE_NAME);
if (file.createNewFile()) {
system.out.prinln("file first created");
}else {
// print a message here
}
return file;
You are telling it to make a directory of the form a/b/c if you want a directory of the form a/b then you should give it the directory you want it to create.
File dir = new File(SYS_CONFIG_FILE_PATH, SYS_CONFIG_DIR_NAME);
f.mkdirs();
return new File(dir, SYS_CONFIG_FILE_NAME);
You don't have to pre-create files before you use them.

Java I/O issue when dealing with files

I'm currently learning Java I/O , when I compile this code :
import java.io.File;
public class Main {public static void main(String[] args){
//Creation of the File object
File f = new File("test.txt");
System.out.println("File absolute path : " + f.getAbsolutePath());
System.out.println("File name : " + f.getName());
System.out.println("Does it exist ? " + f.exists());
System.out.println("Is it a directory? " + f.isDirectory());
System.out.println("Is it a file ? " + f.isFile());
}
The problem is f.exists() and f.isFile()return false
How is that even possible ?
File f = new File("test.txt");
The above line doesn't create an physical file on the disk. it only creates a file object, with the name 'test.txt', thus File#exits() returns false.
You need to create an actual physical file in number of ways.
Using File
file.createNewFile()
using FileWriter
FileWriter writer = new FileWriter(f);
PS: same applies for File#isFile() returning false as well.
File is not a fileā€”it is just a descriptor of a native filesystem resource that may or may not exist. For example, you can do new File(path).createNewFile().
new File("test.txt") It creates a new File instance by converting the given pathname string into an abstract pathname not physical file.
you can call File#createNewFile(). It atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.
there is nothing wrong with the program except the file location
there are two solutions
1 : you can store the file in the project directory , parallel to src folder
2 you can create the file with full path specified
File f = new File("D:/folder1/folder2/applicationname/src/test.txt");

Replace if a File name exists in a directory

This is the code I tried. But this returns false even if the file exists. The variables FilePath and FileName is obtained from the UI.
File exportFile = new File("\""+FilePath + "\\"+ FileName+"\"");
boolean exists = exportFile.exists();
if (!exists) {
System.out.println("File does not exists");
}
else{
System.out.println( "File exists.");
}
What is the proper way to do this? And BTW, How can I prompt the user to replace or rename the FileName?
replace
File exportFile = new File("\""+FilePath + "\\"+ FileName+"\"");
with
File exportFile = new File(FilePath + "\\" + FileName);
There is no need for quoting the file name. Even if it contains spaces.
I think the problem might be caused by the way you get the file path, since you are getting it from UI, i should point out that you don't have to construct the path, you can either use getAbsolutePath() or getPath() methods provided in the java.io.File class.

can file.renameTo replace a existing file?

// File (or directory) to be moved
File file = new File("filename");
// Destination directory
File dir = new File("directoryname");
// Move file to new directory
boolean success = file.renameTo(new File(dir, file.getName()));
if (!success) {
// File was not successfully moved
//can it be because file with file name already exists in destination?
}
If the file with name 'filename' already exists in the destination will it be replaced with a new one?
According to Javadoc:
Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.
From Javadoc:
The rename operation might not be able
to move a file from one filesystem to
another, it might not be atomic, and
it might not succeed if a file with
the destination abstract pathname
already exists.
I tested the following code:
It works the first time, second time it fails as expected.
To move a file you should delete or rename the destination if required.
public class Test {
public static void main(String[] args) throws IOException {
File file = new File( "c:\\filename" );
file.createNewFile();
File dir = new File( "c:\\temp" );
boolean success = file.renameTo( new File( dir, file.getName() ) );
if ( !success ) {
System.err.println( "succ:" + success );
}
}
}
As it is system dependent you should not expect it to behave this or other way. Check it and implement your own logic.

Categories