Im trying to list all the files in a particular folder of my android emulator and i keep getting null answer.
Heres my code:
File sdCardRoot = Environment.getExternalStorageDirectory();
File[] file= new File(sdCardRoot+"path");
for (File f : file.listFiles()) {
if (f.isFile())
String name = f.getName();
}
This doesnt seem to work dont know why.
I've split the function in two parts, first function gets all the files in the given path and the second function gets the filenames from the file array.
public File[] GetFiles(String DirectoryPath) {
File f = new File(DirectoryPath);
f.mkdirs();
File[] file = f.listFiles();
return file;
}
public ArrayList<String> getFileNames(File[] file){
ArrayList<String> arrayFiles = new ArrayList<String>();
if (file.length == 0)
return null;
else {
for (int i=0; i<file.length; i++)
arrayFiles.add(file[i].getName());
}
return arrayFiles;
}
change
File[] file= new File(sdCardRoot+"path");
with
File[] file= new File(sdCardRoot, "path");
and make sure the directory path exits
Just Check this:
List<File> files = getListFiles(new File("YOUR ROOT"));
private List<File> getListFiles(File parentDir) {
ArrayList<File> inFiles = new ArrayList<File>();
File[] files = parentDir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
inFiles.addAll(getListFiles(file));
} else {
if(file.getName().endsWith(".csv")) {
inFiles.add(file);
}
}
}
return inFiles;
Since sdCardRoot is instance of File, sdCardRoot+"path" will return the same thing as sdCardRoot.toString() + "path".
However, calling file.toString() returns file name, but not absolute path. You need to call sdCardRoot.getAbsolutePath() + "path".
Also, make sure that you have allowed the emulator to use a certain amount of memory for external storage.
Related
i have a folder with two files: one pdf and one xml.
When i click on folder i want get path of xml file only.
With my code(that i post below) i get paths of both files.
Who can help me?
THANKS!
private ArrayList<String> GetFiles2(File f) {
ArrayList<String> MyFiles = new ArrayList<String>();
//File f = new File(DirectoryPath);
f.mkdirs();
File[] files = f.listFiles();
if (files.length == 0)
return null;
else {
for (int i=0; i<files.length; i++)
MyFiles.add(files[i].getPath());
}
System.out.println("MYFILE:"+MyFiles);
return MyFiles;
}
my result : MYFILE:
`[/storage/emulated/0/ordinazioni/2/23_Agosto_2017_09_44_51_AM.xml,` /storage/emulated/0/ordinazioni/2/23_Agosto_2017_09_44_51_AM.pdf]
You can use the overriden version of File#listFiles(FileFilter) to get specific files from a directory.
File[] files = f.listFiles(new FileFilter() {
#Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(".xml");
}
});
Full implementation :
private ArrayList<String> getXmlFiles(File directory) {
ArrayList<String> names = new ArrayList<>();
directory.mkdirs();
File[] files = directory.listFiles(new FileFilter() {
#Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(".xml");
}
});
for(File f : files)
names.add(f.getPath());
System.out.println("MYFILE:" + names);
return files.length > 0 ? names : null;
}
Anyway I recommend you to return an empty list insteand of null to avoid NPE issues (Just return names)
You can change your for-loop like this for example:
for (int i=0; i<files.length; i++) {
if(files[i].getPath().endsWith(".xml")) {
MyFiles.add(files[i].getPath());
}
}
this will add to the MyFiles list only the paths of the xml files in the praticular folder
For getting a specific file location use this code,
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext");
eg ;
"/AAlist/"+serialno.get(position).trim()+".jpg"
I am writing a method to get specific file type such as pdf or txt from folders and subfolders but I am lacking to solve this problem. here is my code
// .............list file
File directory = new File(directoryName);
// get all the files from a directory
File[] fList = directory.listFiles();
for (File file : fList) {
if (file.isFile()) {
System.out.println(file.getAbsolutePath());
} else if (file.isDirectory()) {
listf(file.getAbsolutePath());
}
}
My current method list all files but I need specific files
For a filtered list without needing recursion through sub directories you can just do:
directory.listFiles(new FilenameFilter() {
boolean accept(File dir, String name) {
return name.endsWith(".pdf");
}});
For efficiency you could create the FilenameFilter ahead of time rather than for each call.
In this case because you want to scan sub folders too there is no point filtering the files as you still need to check for sub folders. In fact you were very nearly there:
File directory = new File(directoryName);
// get all the files from a directory
File[] fList = directory.listFiles();
for (File file : fList) {
if (file.isFile()) {
if (file.getName().endsWith(".pdf")) {
System.out.println(file.getAbsolutePath());
}
} else if (file.isDirectory()) {
listf(file.getAbsolutePath());
}
}
if(file.getName().endsWith(".pdf")) {
//it is a .pdf file!
}
/***/
Try using the FilenameFilter interface in you function
http://docs.oracle.com/javase/6/docs/api/java/io/FilenameFilter.html
http://www.mkyong.com/java/how-to-find-files-with-certain-extension-only/ - for a code that has extention filter
Use File.listFiles(FileFilter).
Example:
File[] fList = directory.listFiles(new FileFilter() {
#Override
public boolean accept(File file) {
return file.getName().endSwith(".pdf");
}
});
You can use apache fileUtils class
String[] exte= {"xml","properties"};
Collection<File> files = FileUtils.listFiles(new File("d:\\workspace"), exte, true);
for(File file: files){
System.out.println(file.getAbsolutePath());
}
My advice is to use FileUtils or NIO.2.
NIO.2 allows Stream with Depth-First search, for example you can print all files with a specified extension in one line of code:
Path path = Path.get("/folder");
try{
Files.walk(path).filter(n -> n.toString().endsWith(".extension")).forEach(System.out::println)
}catch(IOException e){
//Manage exception
}
I followed this question:
Now in my case i have 720 files named in this way: "dom 24 mar 2013_00.50.35_128.txt", every file has a different date and time. In testing phase i used Scanner, with a specific txt file, to do some operations on it:
Scanner s = new Scanner(new File("stuff.txt"));
My question is:
How can i reuse scanner and read all 720 files without having to set the precise name on scanner?
Thanks
Assuming you have all the files in one place:
File dir = new File("path/to/files/");
for (File file : dir.listFiles()) {
Scanner s = new Scanner(file);
...
s.close();
}
Note that if you have any files that you don't want to include, you can give listFiles() a FileFilter argument to filter them out.
Yes, create your file object by pointing it to a directory and then list the files of that directory.
File dir = new File("Dir/ToYour/Files");
if(dir.isDir()) {
for(File file : dir.listFiles()) {
if(file.isFile()) {
//do stuff on a file
}
}
} else {
//do stuff on a file
}
You can try this in this way
File folder = new File("D:\\DestFile");
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
if (file.isFile()&&(file.getName().substring(file.getName().lastIndexOf('.')+1).equals("txt"))) {
// Scanner
}
}
File file = new File(folderNameFromWhereToRead);
if(file!=null && file.exists()){
File[] listOfFiles = file.listFiles();
if(listOfFiles!=null){
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
// DO work
}
}
}
}
Using the code below populates my JComboBox with the complete path. is there an easy way to filter it to only show the file name itself in the JComboBox.
String path = "\\\\intdatserver1\\NY_files";
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
final JComboBox jList1 = new JComboBox(listOfFiles);
I think you could figure it out by yourself but if you insist...
List<String> fileNames = new ArrayList<String>();
for (File file : listOfFiles) {
if (file.isFile()) {
fileNames.add(file.getName());
} else if (file.isDirectory()) {
// handle directory
}
}
You can create a new String array or List<String> wiht the file names only, using the getName() method from the File class.
I am using the following recursive method to list all files and folders within a given directory, however it seems to be listing some files that aren't visible in Windows Explorer - even when I display hidden and system protected files. I have set the method to scan the C:\\ directory, and it hangs after outputting files in the Boot directory and BOOTSECT.BAK. Well, actually, I don't think it hangs - it looks like it returns the final array but there are still more Files and no exceptions are thrown!
private static ArrayList<File> recursiveSearch(File dir){
File[] files = dir.listFiles();
ArrayList<File> result = new ArrayList<File>();
for(File file : files)
if(file.isDirectory()){
result.add(file);
ArrayList<File >tempList = recursiveSearch(file);
for(File temp : tempList)
result.add(temp);
}else{
result.add(file);
System.out.println(file.getPath());
}
return result;
}
I know about FileSystemView but in this occassion I can't use it because I need to apply a custom Filename Filter (which I have excluded from the above, but I have tested and it doesn't affect the methods output). Any help would be appreciated - thanks in advance
This file (bootsect.bak) is detected as a directory, yet returns a null File array. A workaround is to check that it is instantiated:
private static List<File> recursiveSearch(File dir) {
File[] files = dir.listFiles();
List<File> result = new ArrayList<File>();
if (files != null) {
for (File file : files)
if (file.isDirectory()) {
result.add(file);
List<File> tempList = recursiveSearch(file);
for (File temp : tempList)
result.add(temp);
} else {
result.add(file);
System.out.println(file.getPath());
}
}
return result;
}