Program should check if the directory is exist?? And if not, tell the user that there is no such folder.
I found many examples in which is explained how to check whether there is a file, but I need to know whether there is a directory? All methods
boolean x = context.getExternalFilesDir("/nicknameOfUser/").exists();
Toast.makeText(context, "ExternalFilesDir : " + x, Toast.LENGTH_SHORT).show();
isAbsolute(), isDirectory(), isFile(), create a new path to the files folder - nicknameOfUser I do not want to they were created, I just need to receive there is a directory or not ... I don't need create new folders...
How to do it? I think it is a question from regular, but I can't understand ...
When i launch app first time - in my filemanager no any file! But after i check .exists(); it create a path to the folder that i need a check... I DON'T NEED IT
To check if there is directory you have to use two conditions
File file = new File(filePath);
boolean isPresent = file.exists() && file.isDirectory();
returns true only if file exists and is directory.
You can ask for isDirectory() as follows :
File f = new File(Environment.getExternalStorageDirectory()+"/nicknameOfUser/");
if(f.isDirectory()) {
}
File file = new File("your path");
file.exists();
But after i check .exists(); it create a path to the folder that i need a check... I DON'T NEED IT
The problem is not the File.exists() call. It is the getExternalFilesDir call that is creating the directory. As the documentation states:
Unlike Environment.getExternalStoragePublicDirectory(), the directory returned here will be automatically created for you.
Eventually i found a solution. I begin to think it is impossible but all is ok
String fileDir = Environment.getExternalStorageDirectory().getAbsoluteFile() + "/nicknameOfUser/";
File file = new File(fileDir);
boolean x = file.exists();
This code work properly
Related
How does Java know:
new File("C:\Directory")
is a directory?
and
new File("C:\Directory\file.txt")
is a file?
I'm asking because I use this:
File f = new File(directory_path)
f.mkdirs()
and later on, I check if the file is a directory and it returns false.
if(f.isDirectory())
Do I have to set the file to be a directory or does Java figure it out based on the lack of .extension?
It consults the underlying file system which has an attribute that indicates whether a file is a directory or not.
From the code of File.isDirectory
return ((fs.getBooleanAttributes(this) & FileSystem.BA_DIRECTORY)
!= 0);
I have a path i.e. "K:\user\abc.xml". I want to validate that path only that is valid or not, no need to create file at this location. I used file writer but it's create file. So please guide me what should I do?
Don't go with file please help me for path validation.
Create a File object:
File f = new File("K:\user\abc.xml");
and test if it exists:
if (f.exists()) {}
else {}
File f = new File("c:/temp");
if(f.exists()){
System.out.println("yes");
}else{
System.out.println("no");
}
This code will validate the given directory (or Path) is exists or not.
Here are the possible results:
if i have temp folder in c drive : yes
if i don't have temp folder in c drive : no
if you are writing to root directory like "C" path should "C:" (you should handle if user enter only "C" just append ":"
But, it purely dependent code for windows platform, you should think through platform independent.
Happy Codding.
I'm making a game that saves information into a binary file so that I can start at the point I left the game on the next use.
My problem is that it works fine on my PC because I chose a path that already existed to save the file, but once I run the game on another PC, I got an error saying the path of the file is invalid (because i doesn't exist yet, obviously).
Basically I'm using the File class to create the file and then the ObjectOutputStream and ObjectInputStream to read/write info.
Sorry for the noob question, I'm still pretty new to using files.
You must first check if the directory exists and if it does not exist then you must create it.
String folderPath = System.getProperty("user.home") + System.getProperty("file.separator") + "MyFolder";
File folder = new File(folderPath);
if(!folder.exists())
{
folder.mkdirs();
}
File saveFile = new File(folderPath, "fileName.ext");
Please note that the mkdirs() method is more useful in this case instead of the mkdir() method as it will create all non existing parent folders.
Hope this helps. Good luck and have fun programming!
Cheers,
Lofty
You are looking for File mkdirs()
Which will create all the directories necessary that are named in your path.
For example:
File dirs= new File("/this/path/does/not/exist/yet");
dirs.mkdir();
File file = new File(dirs, "myFile.txt");
Take in consideration that it may fail, due to proper file permissions.
My solution has been create a subdirectory within the user's home directory (System.getProperty("user.home")), like
File f = new File(System.getProperty("user.home") + "/CtrlAltDelData");
f.mkdir();
File mySaveFile = new File (f, "save1.txt");
I trying to check whether a file exists at given directory location.
File seacrhFile = new File("D:/input", contract.conf);
if (seacrhFile.exists()) {
returnFile = seacrhFile;
} else {
System.out.println("No such file exists");
}
reutrn returnFile;
This is working in D:/input directory scenario, but if I Change the directory location to src/test/resources/input folder then I am getting No such file exists, eventhough the file exists.
If you want to have access to
src/test/resources/input
you probably should use
System.getProperty("user.dir") + File.separator + "src/test/resources/input"
because the system-property "user.dir" points to the projectlocation.
If you just use "src/test/resources/input" you will get your mentioned exception, because the File Object don't "start" at the project location. So you have to specify it manually.
Nevertheless it's better to use the getResource-Method to retrieve different resources within your project, because if you run your project with the jar-File you need to tweak around to get "user.dir" to work correctly.
Just a basic example for the Classloader:
ClassLoader.getSystemClassLoader().getResource("test/resources/input");
This returns an URL-Object, with this object you can get the File-Object using ...
URL filePath = ClassLoader.getSystemClassLoader().getResource("test/resources/input");
File file = new File( filePath.toURI() );
Remove the src and try it again .
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
fos.write(string.getBytes());
fos.close();
When trying to delete one of those files, this is what I use, but it's returning false.
String tag = v.getTag().toString();
File file = new File(System.getProperty("user.dir")+"/"+tag);
String s = new Boolean (file.exists()).toString();
Toast.makeText(getApplicationContext(), s, 1500).show();
file.delete();
How can I overcome this problem?
Use getFileStreamPath(FILENAME) to find your file. From the docs:
Returns the absolute path on the filesystem where a file created with openFileOutput(String, int) is stored.
Your current working directory.
To help diagnose the problem, use file.getAbsolutePath() to see the full path.
It could also be a permissions problem, if you're trying to delete from another application. If so, you may need to change to MODE_WORLD_WRITEABLE (insecure), or restructure your code so the create and delete are called by the same app.
EDIT: That was mostly incorrect. I didn't realize that openFileOutput didn't use the current working directory.
Use same contents as 'FILENAME' variable in your first snippet in the second snippet while trying to delete.
String RootDir = Environment.getExternalStorageDirectory()
+ File.separator + "Video";
File RootFile = new File(RootDir);
RootFile.mkdir();
FileOutputStream f = new FileOutputStream(new File(RootFile, "Sample.mp4"));
i used this code to save the video files to non-default location. Hope this will be useful to you.By default it is storing in sd card
For each application the Android system creates a "data/data/package of the application" directory.
Files are saved in the "files" folder under this directory
to change the default directory the above code will be used
the default working directory can be displayed using fileobject.getAbsolutePath()