Recursion method to display the directory structure of a path (Java) - java

Our teacher gave us question to Write a recursive method to display the directory structure of a path.
He want the output to look like this :
testdir
+--f1
+--d2
+--d22
+f221
+f212
+f211
+--f2
+--d1
+--f12
I used this method :
package Task;
import java.io.File;
import java.io.IOException;
public class Recursive {
public static void main(String[] args) {
File currentDirectory = new File(" . "); // current directory
displayDirectoryContents(currentDirectory);
}
public static void displayDirectoryContents(File dirct) {
File[] myfiles = dirct.listFiles();
for (File file : myfiles) {
if (file.isDirectory()) {
//it worked when i used file.getCanonicalPath()); but file.getName()); does not work
System.out.println("directory : " + file.getName());
displayDirectoryContents(file);
}
else {
//it worked when i used file.getCanonicalPath()); but file.getName()); does not work
System.out.println(" files : " + file.getName());
}
}
}
}
The getName does not work and give me an erorr (
Exception in thread "main" java.lang.NullPointerException
at lab16.displayDirectoryContents(lab16.java:17)
at lab16.main(lab16.java:10)
)

i've rewritten algorithm and it works with .getName() without any problem. I didn't do any indentations tho.
import java.io.File;
public class Recursive {
private static void TraverseDirectory(File[] files) {
if (files.length == 0) return;
for (File file : files) {
if (file.isFile()) System.out.println(file.getName());
else {
// is DIR
System.out.println(file.getName());
TraverseDirectory(file.listFiles());
}
}
}
public static void main(String[] args) {
File cur_dir = new File("/");
TraverseDirectory(cur_dir.listFiles());
}
}
EDIT: after you edited your question, I can now see what was the problem. You weren't returning anything when the content of the directory was empty (it was null). Therefore, it couldn't traverse null value.

Related

Terminal input gives null pointer but IDE input runs fine

Trying to build a program that changes stuff in SRC folder. for (File f : files) { Returns a null pointer despite returning appropriate answer in Intellij IDE. This program searches my SRC file directory and lists all files pertaining to the the directory. It works fine in the IDE but does not do the correct function on the command line.
//This program is from Liang Comprehensive Java 12.18
import java.io.*;
import java.util.ArrayList;
public class AddStatement {
public static void main(String[] args) throws IOException {
// File dir = new File(args[0]);
File dir=new File("src");
System.out.println(dir.getCanonicalPath() + " Was put into console");
writeTofile(getfiles(dir));
}//End of main class
public static ArrayList<String> getfiles(File dirc) throws IOException {
ArrayList<String> paths = new ArrayList<String>(5);
File[] files = dirc.listFiles();
for (File f : files) { //This part is where null is happening
paths.add(f.getCanonicalPath());
}//End of foreach
return paths;
}//End of getFiles method
public static void writeTofile(ArrayList<String> files) throws IOException {
String file;
int count = 1;
for (String f : files) {
file = f;
try (PrintWriter writer = new PrintWriter(new FileWriter(file, true))) {
if (file.contains("Chapter")) {
writer.print("\\\\Chapter" + count);
count++;
}//End of filtering if
}//End of try block
}//End of for loop
System.out.println("Files written");
}//End writeTofile
}// End of Class

how to search subfolders and files to get input form the user using java

how to search subfolders and files to get input form the user using java,
using listRoot method, but how to use with give a File class path
use recursion on the top directory.Some thing as shown below
class Main{
public static void showFiles(File[] files) {
for (File file : files) {
if (file.isDirectory()) {
System.out.println("Folder is " + file.getName());
showFiles(file.listFiles());
} else {
System.out.println("File is " + file.getName());
}
}
public static void main(String[] args) {
File[] files = new File("your path").listFiles();
showFiles(files);
}}
}

Java dir/files list

I am trying to list all directories and files within a certain directory, but while "D:\data" works, "D:\" doesn't. "D" is a secondary disk.
This is the exception:
Exception in thread "main" java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at java.util.Arrays$ArrayList.<init>(Arrays.java:3813)
at java.util.Arrays.asList(Arrays.java:3800)
at project.1.scavenger.listf(scavenger.java:19)
at project.1.scavenger.listf(scavenger.java:30)
at project.1.scavenger.listf(scavenger.java:30)
at project.1.main(Project1.java:28)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Code:
public static List<File> listf(String directoryName) throws IOException {
File directory = new File(directoryName);
List<File> resultList = new ArrayList<File>();
// get all the files from a directory
File[] fList = directory.listFiles();
resultList.addAll(Arrays.asList(fList));
for (File file : fList) {
if (file.isFile()) {
System.out.println(file.getAbsolutePath());
try {
System.out.println(scavenger.checkmime(file.getAbsolutePath()));
} catch (Exception ex) {
}
} else if (file.isDirectory()) {
resultList.addAll(listf(file.getAbsolutePath()));
}
}
// System.out.println(fList);
return resultList;
}
public static String checkmime(String fl) throws MalformedURLException, IOException {
File file = new File(fl);
String mimeType = file.toURL().openConnection().getContentType();
// System.out.println(mimeType);
return mimeType;
}
What's wrong with my code?
Removed error from your version
In your recursion you never ask for null values. Do it and it should run like this:
public static List<File> listf(String directoryName) throws IOException {
File directory = new File(directoryName);
List<File> resultList = new ArrayList<>();
// get all the files from a directory
File[] fList = directory.listFiles();
// this was missing
if (fList == null) {
return null;
}
resultList.addAll(Arrays.asList(fList));
for (File file : fList) {
if (file.isFile()) {
System.out.println(file.getAbsolutePath());
try {
System.out.println(checkmime(file.getAbsolutePath()));
} catch (Exception ex) {
}
} else if (file.isDirectory()) {
// ask here if it was null
List<File> files = listf(file.getAbsolutePath());
if (files != null) {
resultList.addAll(files);
}
}
}
//System.out.println(fList);
return resultList;
}
Why D:\data works and D:\ not
In every root of a drive in a Windows System is a hidden folder structure called $RECYCLE.BIN. In this folder windows stores for each user (sid) an own folder with deleted data (links to it). But a normal user is only allowed to get the first level and not the user folder (with sid value as name).
(German Windows: Papierkorb = Trash)
A maybe much better solution:
A maybe better way of doing such searchings in tree's is to create an Iterator over the directory tree (like a composite iterator). This solution also uses only Java NIO features, so the platform should be changeable (haven't tested!) to for ex. Linux.
DirectoryTreeIterator.java
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
public class DirectoryTreeIterator implements Iterator<Path> {
private final Deque<Iterator<Path>> deque = new ArrayDeque<>();
public DirectoryTreeIterator(Iterator<Path> it) {
deque.push(it);
}
#Override
public boolean hasNext() {
if (deque.isEmpty()) {
return false;
} else {
Iterator<Path> it = deque.peek();
if (!it.hasNext()) {
deque.pop();
return hasNext();
} else {
return true;
}
}
}
#Override
public Path next() {
if (hasNext()) {
Iterator<Path> it = deque.peek();
Path p = it.next();
try {
// here is the magic recursive on only filtered paths
if (Files.isDirectory(p) && Files.isReadable(p) && !Files.isHidden(p)) {
deque.push(Files.newDirectoryStream(p).iterator());
}
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
return p;
} else {
return null;
}
}
}
Then you are able to use it like a charm:
FileLister.java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
public class FileLister {
public static void main(String[] args) throws IOException {
Path p = Paths.get("D:\\");
Iterator<Path> it = Files.newDirectoryStream(p).iterator();
DirectoryTreeIterator dti = new DirectoryTreeIterator(it);
while (dti.hasNext()) {
Path path = dti.next();
if (!Files.isDirectory(path)) {
String mime = Files.probeContentType(path);
System.out.println("Mime of File "
+ path.getFileName() + " is: " + mime);
}
}
}
}
If you add this code right beneath the directory.listFiles() call:
if (fList == null) {
System.out.println("Couldn't read files in directory: " + directoryName);
return resultList;
}
then you will get a message like this:
Couldn't read files in directory: G:\$RECYCLE.BIN\Windows SID
This Windows SIDs is security identifier for a user on your local machine.
So that means your current user has no permission to read this directory (it belongs to a different user) in the "recycle bin" directory, therefore listFiles() returns null instead of a String[].
You could keep the check and the message I've posted, or you can implement a different way to handle "unreadable" directories.
About why "D:\\" failed and "D:\\data" not:
"D:\\" contains the mentioned "$RECYCLE.BIN" directory with restricted access
"D:\\data" has no directory where your current user isn't allowed to access/read it, therefore listFiles() never returned null.

Recursively file search not working

What i wanna do is to recursively search for some files on the external sd-card. The problem is that the code is looking ok, but (assuming .txt files) it only shows me 7 files out of 100+ that are being spread throughout folders.
The code is this:
file = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
void makelist(File file){
if(file.isFile()){
if(SimpleAdapter.getFileType(file)==null)
mis.add(file);
else if(SimpleAdapter.getFileType(file).equalsIgnoreCase("text"))
doc.add(file);
}else if(file.isDirectory()){
for(File f:file.listFiles())
makelist(f);
}
}
Any idea how could i make it run correctly?
Assuming you are building two lists (misc files and doc files), try with below code which all all files other than text files to misc and text files to doc.
if(SimpleAdapter.getFileType(file) == null || !SimpleAdapter.getFileType(file).equalsIgnoreCase("text"))
mis.add(file);
else
doc.add(file);
Not sure why it is not working for you. Tried a test program and it worked perfectly...
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class FileChecker
{
/**
* #param args
*/
public static void main(String[] args) {
SimpleAdapterTest adapter = new SimpleAdapterTest();
adapter.makelist(new File("C:\\MYFolder"));
adapter.showList();
}
}
class SimpleAdapterTest
{
List<File> mis = new ArrayList<File>();
List<File> doc = new ArrayList<File>();
public static String getFileType(File f)
{
String Name = f.getName();
if (f.getName().endsWith(".txt") || Name.endsWith(".TXT")
|| Name.endsWith(".inf") || Name.endsWith(".INF"))
return "text";
return null;
}
public void makelist(File file){
if(file.isFile()){
if(SimpleAdapterTest.getFileType(file)==null)
mis.add(file);
else if(SimpleAdapterTest.getFileType(file).equalsIgnoreCase("text"))
doc.add(file);
}else if(file.isDirectory()){
for(File f:file.listFiles())
makelist(f);
}
}
public void showList()
{
for(File miscFile : mis)
{
System.out.println("Misc files = " + miscFile.getName());
}
for(File docfile : doc)
{
System.out.println("Doc files = " + docfile.getName());
}
}
}

how to delete all files having specific extention from MY Computer by java

HI I want to write a java program by which I can delete all the files of my computer having a specific extension or character pattern in name.I also want to apply wild card character on the name of file.
Thanks in advance
For your program to be really useful you need to do some more thinking, but for a starter;
import java.io.File;
import java.util.regex.Pattern;
private static void walkDir(final File dir, final Pattern pattern) {
final File[] files = dir.listFiles();
if (files != null) {
for (final File file : files) {
if (file.isDirectory()) {
walkDir(file, pattern);
} else if (pattern.matcher(file.getName()).matches()) {
System.out.println("file to delete: " + file.getAbsolutePath());
}
}
}
}
public static void main(String[] args) {
walkDir(new File("/home/user/something"), Pattern.compile(".*\\.mp3"));
}
Sidenote (not an answer, but you didn't ask something): Be aware of recursion.
public void deleteFilesWithExtension(final String directoryName, final String extension) {
final File dir = new File(directoryName);
final String[] allFiles = dir.list();
for (final String file : allFiles) {
if (file.endsWith(extension)) {
new File(aDirectoryName + "/" + file).delete();
}
}
}

Categories