Using file I / O to navigate directories in windows

I am writing a program that uses File I / O to go through a directory specified by the user and then adds directories to the general list of links. The program I wrote works fine on Ubuntu, but does not work when I try to use it on Windows. This is a pretty long program, but this is the part that I think has problems:

private Node<Item> currentNode = new Node<Item>(); public void traverse(File fileObject) { File allFiles[] = fileObject.listFiles(); for(File aFile: allFiles){ System.out.println(aFile.getName()); /* debugging */ recursiveTraversal(aFile); /* Line 34 */ } } public void recursiveTraversal(File fileObject){ Node<Item> newNode = new Node<Item>(); currentNode.addChild(newNode); currentNode = newNode; if (fileObject.isDirectory()){ newNode.setData(new Item()); File allFiles[] = fileObject.listFiles(); for(File aFile : allFiles){ /* This is line 48 */ recursiveTraversal(aFile); } }else if (fileObject.isFile()){ newNode.setData(new Item()); } currentNode = newNode.getParent(); } 

When I use it on Linux, I can give it something like /home/matt/Documents , and it works, but when I try to use Windows with G:\\Users\\Matt\\Documents , this leads to errors . The print statement I selected actually prints the files in the folder, but something with the rest of the program gets confused:

 java.lang.NullPointerException at FileTraverse.recursiveTraversal(FileTraverse.java:48) at FileTraverse.traverse(FileTraverse.java:34) at DirectoryMain$ClickAction.actionPerformed(DirectoryMain.java:103) ... 

After that, there are many errors associated with the Swing GUI, from which this program ends, but I do not think that this is related to anything.

EDIT: Added line numbers that match the trace.

+4
source share
5 answers

I assume that you are in a directory that for some reason windows will not allow you to browse. listFiles () most likely returns null in this instance.

+1
source

From the Javadoc File.listFile () (highlighted by me):

Returns: an array of abstract paths denoting files and directories in the directory denoted by this abstract empty name. The array will be empty if the directory is empty. Returns null if this abstract path name does not indicate a directory or an I / O error occurs.

You will need to handle the zero return from listFiles() , unfortunately, there is no way with the java.io.File API to find out exactly which error occurred.

If you are using Java 7, you can use DirectoryStream instead:

 private void recursiveTraversal(Path path) throws IOException { try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) { for (Path entry : stream) { //Do something with entry doSomething(entry); if (Files.isDirectory(entry)) recursiveTraversal(entry); } } } 

The difference is that newDirectoryStream() can raise an IOException (or a subclass like AccessDeniedException ) that gives information about why the call failed.

+3
source

If you select NullPointerException on line 48, it means that the allFiles array you received is null . According to the JavaDoc for this method, this should only happen if the file you are calling to is not a directory or some input / output error occurs. This is a bit weird as you are checking if this is a directory in if . There may be some OS level access issues regarding permissions.

You might want to study FileFilter or maybe material from the java.nio.file package. I believe the latter simplifies directory directories.

+1
source

The difference between traverse() and recursiveTraversal() bit strange for me; when do you use traverse() ? What role does he play? what does recursiveTraversal() not do yet? Of course, traverse() assumes it works with a directory, but recursiveTraversal() already handles directories well.

Everything is messing around with Item in recursiveTraversal() , but since they don't do anything here, it's hard to find utility. :)

 public void traverse(File fileObject) { File allFiles[] = fileObject.listFiles(); for(File aFile: allFiles){ System.out.println(aFile.getName()); /* debugging */ recursiveTraversal(aFile); } } public void recursiveTraversal(File fileObject){ Node<Item> newNode = new Node<Item>(); currentNode.addChild(newNode); currentNode = newNode; if (fileObject.isDirectory()){ newNode.setData(new Item()); File allFiles[] = fileObject.listFiles(); for(File aFile : allFiles){ recursiveTraversal(aFile); } }else if (fileObject.isFile()){ newNode.setData(new Item()); } currentNode = newNode.getParent(); } 

I would probably move newNode.setData(new Item()); above isDirectory() to make sure newNode has a new Item added to it regardless of whether the current fileObject file or directory:

 public void recursiveTraversal(File fileObject){ Node<Item> newNode = new Node<Item>(); currentNode.addChild(newNode); currentNode = newNode; newNode.setData(new Item()); if (fileObject.isDirectory()){ File allFiles[] = fileObject.listFiles(); for(File aFile : allFiles){ recursiveTraversal(aFile); } }else if (fileObject.isFile()){ /* must do _something_ with files */ } currentNode = newNode.getParent(); } 
0
source

I tried something like this on Windows, and the program would work and throw a nullPointerException as soon as the program tried to cross the directory where security permissions restricted access.

If you do not need a program to add protected / hidden / system folders, just handle the error using the try catch clause.

 public static void dive(File file, int level){ try{ for(File x : file.listFiles()) if(x.isDirectory() && !x.isHidden()) dive(x); }catch(NullPointerException e){ // Do nothing.. } } 

This should go through all user directories.

0
source

Source: https://habr.com/ru/post/1379055/


All Articles