Iterate over all files in a directory from a given relative path
I want to use a "relative" path like this because I want to be able to include all files in that directory as part of a github project that others can download and run.
So, I just want to jump into the structure once ../
, and then go down to one of the directories there, word_lists_1
and read all the files.
6 th
This code keeps crashing. Why is this?
public static void main(String[] args)
{
ArrayList<File> arrayList = new ArrayList<File>();
final String directoryName = "../word_lists_1";
listf( directoryName, arrayList );
for(File elem : arrayList)
{
System.out.println(elem+" ");
}
}
public static void listf(String directoryName, ArrayList<File> files)
{
File directory = new File(directoryName);
// get all the files from a directory
File[] fList = directory.listFiles();
for (File file : fList)
{
if (file.isFile())
{
files.add(file);
}
else if (file.isDirectory())
{
listf(file.getAbsolutePath(), files);
}
}
}
This error message:
Exception in thread "main" java.lang.NullPointerException
at Main.listf(Main.java:49)
at Main.main(Main.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
so it doesn't look like a line 49
, which is:
for (File file : fList)
source to share
You may need the canonical file path to resolve the parent correctly and avoid any path not being resolved, which may be required for some null
, hence << 21>.
One simple implementation for solving a directory from a realistic path to the current directory and then displaying the load, its tree hierarchy would be as follows:
public class Main
{
public static void main( String[] args ) throws URISyntaxException, IOException
{
File currentDir = new File( "." ); // Read current file location
File targetDir = null;
if (currentDir.isDirectory()) {
File parentDir = currentDir.getCanonicalFile().getParentFile(); // Resolve parent location out fo the real path
targetDir = new File( parentDir, "word_lists_1" ); // Construct the target directory file with the right parent directory
}
if ( targetDir != null && targetDir.exists() )
{
listDirectoryAndFiles( targetDir.toPath() );
}
}
private static void listDirectoryAndFiles( Path path ) throws IOException
{
DirectoryStream<Path> dirStream = Files.newDirectoryStream( path );
for ( Path p : dirStream )
{
System.out.println( p.getFileName() );
if ( p.toFile().isDirectory() )
{
listDirectoryAndFiles( p );
}
}
}
}
You may need to customize the method listDirectoryAndFiles(java.nio.file.Path)
to suit your needs.
Note that the above implementation takes advantage of the new I / O capabilities from Java 7.
source to share
This is what I understand from your requirements
- you are in the current execution directory and then you want to go back to one directory and go to another folder called "Word_lists_1"
If I understood your question correctly, I would implement it as
String str=System.getProperty("user.dir");
String filePath = str+"/../word_lists_1";
if you are in another directory you can do
String str=Paths.get(".").toAbsolutePath().normalize().toString();
String filePath = str+"/../word_lists_1";
hope this helps!
Good luck!
source to share