Adding files to a file[] statement in java - java

Hello members of stackoverflow
I need some help with adding files from a directory to a bunch of files (File[])
So basically i want to add all the files in a directory to the following group of files:
File[] contents = {};
The user of my application will select a directory and i want that directories contents to be added to the above group of files ('contents I'm not sure how this is done because it doesn't have a simple 'add' method like an ArrayList/List does.
Any help with this would be greatly appreciated.
Thanks.

Use File.listFiles():
File dir = new File("/somedir");
File[] files = dir.listFiles();

try
{
File folder = new File(FOLDER_NAME);
File[] contents = folder.listFiles();
}
catch(Exception e)
{
System.out.println("[Error] Folder not Found");
}

Related

Cannot reach files inside the root directory of my Android app

I have the code bellow:
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/data/data/com.example.stavr.mydiary";
File file=new File(path);
File[] files = file.listFiles();
String arr[]=file.list();
List l=new ArrayList<>();
for(String i:arr){
if(i.endsWith(". txt")){
l.add(i);
}
}
which returns 0 files on my array (NullPointerException to be exact), Iknow that there are files in that directory. If i change the directory to just
Environment.getExternalStorageDirectory().getAbsolutePath()
the code work and it returns all the files. I thing that i have done something wrong with the permissions. Could you please help me,
Thank you very much in advance..!!!
It seems you're trying to access your internal app data folder, you can do that by using a context object:
String dir = context.getFilesDir().getAbsolutePath();
If you still want to access external storage then you should make sureyou have WRITE_EXTERNAL_STORAGE permission in your AndroidManifest.xml

How to get folders' name listed in Assets folder? Only folders' names but not any file names

Scenario: My Assets directory - Assets/folder1; Assets/folder2; and so on.
With files in folder1 and other folders, such as Asssets/folder1/file1, and others, Asssets/folder/somefile1, and others.
I want the code to access names of only folder names from Assets folder i.e., as a list.
folder1
folder2
and so on.
new File("file:///android_asset/").listFiles();
new File("file:///android_asset/").list();
Both of the above statements return null. what would be the path for Assets folder?
Thanks!
You can use File.listFiles() to get all files in a given directory (including sub-directories). From these you can get the names.
Try following
public static getDirNames(String sDir){
List<String> listDir;
File[] faFiles = new File(sDir).listFiles();
for(File file: faFiles)
{
if(file.isDirectory())
{
getDirNames(file.getAbsolutePath());
listDir.add(file.getName());
}
}

Copy jpg from folder to folder on Linux

I'm using Files.copy(sourceFile,destFile) from apache's commonsIO lib, in order to copy jpg from one folder to another on Linux machine.
Actually I'm doing it for all pic's in the folder :
File folder = new File(sourcePath);
File[] folderContent = folder.listFiles();
File tmp = null;
File sourceFile = null;
File destFile = null;
//copy all pics to other folder :
for(int i=0;i<folderContent.length;i++){
if(folderContent[i].getName().endsWith("jpg")){
sourceFile = new File(sourcePath);
destFile = new File(destPath);
//copy to main dir:
Files.copy(sourceFile,destFile);
}
}
But all I get in the new folder is empty files (with the correct name).
When I tested it with a simple test with one file ,Like that :
Files.copy(sourceFile,destFile);
then the file copy successfully.
Does anyone have a clue ?? (Is it a java-Linux known issue ?)
Thanks!
This is no Linux issue.
First, you use the source folder as the source file, not the file itself.
Also, possibly, you use use the destination folder as the copy target.
Assuming destPath is the destination folder:
for(File file : folderContent){
if(file.getName().endsWith("jpg")){
Files.copy(file, new File(destPath, file.getName()));
}
}

How to retrieve every file in the Internal Storage?

In Android, assuming that I have files in "/data/data/package.name/", without knowing the names or how many files exist, what is the best way to retrieve/iterate them all?
Also, the name is numerical only.
It looks like you want to list all of the files in the directory, and (possibly) recurse if a file is a directory.
You can find how to do this at the answer to this question.
Copy-pasted:
File f = new File("/data/data/package.name/");
File[] files = f.listFiles();
for (File inFile : files) {
if (inFile.isDirectory()) {
// is directory; recurse?
} else {
doLogicFor(inFile);
}
}

Getting file details and not printing directory help

I am trying to make a program which will print all files in my current directory (but not the directories), and at the moment what I have is printing all files and directories in my home directory. How can I change this ?
My code at the moment :
File[] fileList = new File(System.getProperty("user.home")).listFiles();
Also, how can I print the details of these files as well ? I would like the file size, permissions, anything I can get, so as to make my program the equivalent to an ls -a in unix.
Can someone please help me with this as I cannot find the pertinent Java functions anywhere?
Thanks a lot!
You can use a FileFilter to get rid of dirs
File[] files = new File(System.getProperty("user.home")).listFiles(new FileFilter() {
public boolean accept(File file) {
return !file.isDirectory();
}
});
You can get the size of file with file.length()
You can get the files by using a FileFilter, like so:
File dir = new File("/path/to/directory");
FileFilter fileFilter = new FileFilter() {
public boolean accept(File file) {
return file.isFile();
}
};
File[] files = dir.listFiles(fileFilter);
for (File file : files) {
System.out.println("File: " + file);
}
Your question doesn't make sense.
You ask in your coment to Bozho to print a list of files in the current directory, but specifically asks for a list of files in "user.home", which is your home directory.
If you don't want to list the files in your home directory, change your code to ask for files from somewhere else.
Loop the array and print only those which have isFile() true (or isDirectory() false). The current directory is simpyl new File("")
All details you can obtain by calling the respective methods, like canRead(), canWrite(), length(), etc. Check the documentation for more
Get the current directory by initializing a file object for it and then using the listFiles() method from that object. Then loop over the resulting list and test each file with the File.isFile() method:
File cwd = new File(".");
File[] fileList = cwd.listFiles()
for (File aFile : fileList) {
if (aFile.isFile()) {
System.out.println(aFile);
}
}
You can use the canExecute(), canRead(), and canWrite() methods to find out if your program can do those things. As noted in another answer, length() will give you the size.

Categories

Resources