How to read all mp3 files in my computer in java? - java

My aim is to get list of all mp3 files in my computer(below code in c: directory). But when I run this code, I am getting NullPointerException. But works well for other directory like(e:).
public class music {
public static void main(String args[]){
extract("c:\\");
}
public static void extract(String p){
File f=new File(p);
File l[]=f.listFiles();
for(File x:l)
{
//System.out.println(x.getName());
if(x.isHidden()||!x.canRead())
continue;
if(x.isDirectory())
extract(x.getPath());
else if(x.getName().endsWith(".mp3"))
System.out.println(x.getPath()+"\\"+x.getName());
}
}
}

I got NPE with your code when it tried to access some not real directories like c:\Documents and Settings.
To solve this problem you can skip iterating over directories that returns null from listFiles() like in this code:
public static void main(String args[]) {
extract(new File("c:\\"));
}
public static void extract(File dir) {
File l[] = dir.listFiles();
if (l == null) {
System.out.println("[skipped] " + dir);
return;
}
for (File x : l) {
if (x.isDirectory())
extract(x);
if (x.isHidden() || !x.canRead())
continue;
else if (x.getName().endsWith(".mp3"))
System.out.println(x.getPath());//name should be included in path
}
}

In Windows operating system. C Drive (Windows drive) have system file that used by windows while running and some file that locked by windows. When your code try to access that files its through exception.
Try to run this code with other then C:// drive..
Add Try catch or null check for this files:
import java.io.*;
public class Music {
public static void main(String args[]){
extract("c:\\");
}
public static void extract(String p){
File f=new File(p);
File l[]=f.listFiles();
for(File x:l){
if(x==null) return;
if(x.isHidden()||!x.canRead()) continue;
if(x.isDirectory()) extract(x.getPath());
else if(x.getName().endsWith(".mp3"))
System.out.println(x.getPath()+"\\"+x.getName());
}
}
}

Related

Java - Read Multiple Excel files in folder [duplicate]

I need to get a list of all the files in a directory, including files in all the sub-directories. What is the standard way to accomplish directory iteration with Java?
You can use File#isDirectory() to test if the given file (path) is a directory. If this is true, then you just call the same method again with its File#listFiles() outcome. This is called recursion.
Here's a basic kickoff example:
package com.stackoverflow.q3154488;
import java.io.File;
public class Demo {
public static void main(String... args) {
File dir = new File("/path/to/dir");
showFiles(dir.listFiles());
}
public static void showFiles(File[] files) {
for (File file : files) {
if (file.isDirectory()) {
System.out.println("Directory: " + file.getAbsolutePath());
showFiles(file.listFiles()); // Calls same method again.
} else {
System.out.println("File: " + file.getAbsolutePath());
}
}
}
}
Note that this is sensitive to StackOverflowError when the tree is deeper than the JVM's stack can hold. If you're already on Java 8 or newer, then you'd better use Files#walk() instead which utilizes tail recursion:
package com.stackoverflow.q3154488;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class DemoWithJava8 {
public static void main(String... args) throws Exception {
Path dir = Paths.get("/path/to/dir");
Files.walk(dir).forEach(path -> showFile(path.toFile()));
}
public static void showFile(File file) {
if (file.isDirectory()) {
System.out.println("Directory: " + file.getAbsolutePath());
} else {
System.out.println("File: " + file.getAbsolutePath());
}
}
}
If you are using Java 1.7, you can use java.nio.file.Files.walkFileTree(...).
For example:
public class WalkFileTreeExample {
public static void main(String[] args) {
Path p = Paths.get("/usr");
FileVisitor<Path> fv = new SimpleFileVisitor<Path>() {
#Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
System.out.println(file);
return FileVisitResult.CONTINUE;
}
};
try {
Files.walkFileTree(p, fv);
} catch (IOException e) {
e.printStackTrace();
}
}
}
If you are using Java 8, you can use the stream interface with java.nio.file.Files.walk(...):
public class WalkFileTreeExample {
public static void main(String[] args) {
try (Stream<Path> paths = Files.walk(Paths.get("/usr"))) {
paths.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Check out the FileUtils class in Apache Commons - specifically iterateFiles:
Allows iteration over the files in given directory (and optionally its subdirectories).
Using org.apache.commons.io.FileUtils
File file = new File("F:/Lines");
Collection<File> files = FileUtils.listFiles(file, null, true);
for(File file2 : files){
System.out.println(file2.getName());
}
Use false if you do not want files from sub directories.
For Java 7+, there is also https://docs.oracle.com/javase/7/docs/api/java/nio/file/DirectoryStream.html
Example taken from the Javadoc:
List<Path> listSourceFiles(Path dir) throws IOException {
List<Path> result = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.{c,h,cpp,hpp,java}")) {
for (Path entry: stream) {
result.add(entry);
}
} catch (DirectoryIteratorException ex) {
// I/O error encounted during the iteration, the cause is an IOException
throw ex.getCause();
}
return result;
}
It's a tree, so recursion is your friend: start with the parent directory and call the method to get an array of child Files. Iterate through the child array. If the current value is a directory, pass it to a recursive call of your method. If not, process the leaf file appropriately.
As noted, this is a recursion problem. In particular, you may want to look at
listFiles()
In the java File API here. It returns an array of all the files in a directory. Using this along with
isDirectory()
to see if you need to recurse further is a good start.
You can also misuse File.list(FilenameFilter) (and variants) for file traversal. Short code and works in early java versions, e.g:
// list files in dir
new File(dir).list(new FilenameFilter() {
public boolean accept(File dir, String name) {
String file = dir.getAbsolutePath() + File.separator + name;
System.out.println(file);
return false;
}
});
To add with #msandiford answer, as most of the times when a file tree is walked u may want to execute a function as a directory or any particular file is visited. If u are reluctant to using streams. The following methods overridden can be implemented
Files.walkFileTree(Paths.get(Krawl.INDEXPATH), EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
new SimpleFileVisitor<Path>() {
#Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException {
// Do someting before directory visit
return FileVisitResult.CONTINUE;
}
#Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
// Do something when a file is visited
return FileVisitResult.CONTINUE;
}
#Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
throws IOException {
// Do Something after directory visit
return FileVisitResult.CONTINUE;
}
});
I like to use Optional and streams to have a net and clear solution,
i use the below code to iterate over a directory. the below cases are handled by the code:
handle the case of empty directory
Laziness
but as mentioned by others, you still have to pay attention for outOfMemory in case you have huge folders
File directoryFile = new File("put your path here");
Stream<File> files = Optional.ofNullable(directoryFile// directoryFile
.listFiles(File::isDirectory)) // filter only directories(change with null if you don't need to filter)
.stream()
.flatMap(Arrays::stream);// flatmap from Stream<File[]> to Stream<File>

Why is this not calculating total size of the folder correctly?

Here are the two java classes:
package je3.io;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* Created by IDEA on 31/01/15.
*/
public class DirWalker {
private List<File> recursiveList = new ArrayList<File>();
public void walkDir(String pathname) {
File d = new File(pathname);
recursiveList.add(d);
if(d.isDirectory()) {
for(String f : d.list()) {
walkDir(f);
}
}
}
public void reset() {
recursiveList.clear();
}
public List<File> getRecursiveList() {
return recursiveList;
}
public static void main(String[] args) {
DirWalker dirWalker = new DirWalker();
dirWalker.walkDir("/tmp");
dirWalker.getRecursiveList().forEach(System.out::println);
}
}
package je3.io;
import java.io.File;
/**
* Created by IDEA on 31/01/15.
*/
public class DirSummariser {
private DirWalker dirWalker = new DirWalker();
private long dirSize = 0;
public DirSummariser(String pathname) {
dirWalker.reset();
dirWalker.walkDir(pathname);
}
public DirSummariser(File file) {
this(file.getAbsolutePath());
}
public long calculateDirSize() {
for(File f : dirWalker.getRecursiveList()) {
dirSize += f.length();
}
return dirSize;
}
public static void main(String[] args) {
DirSummariser dirSummariser = new DirSummariser("/Users/hualin/Downloads/pdf");
System.out.println(dirSummariser.calculateDirSize());
}
}
In the main method of the second class, I was trying to calculate the total size of the pdf folder, which should be around 30MB. The java program compiles without error, but says the size of the folder is only 1600 bytes.
The problem is in DirWalker:
public void walkDir(String pathname) {
File d = new File(pathname);
recursiveList.add(d);
if(d.isDirectory()) {
for(String f : d.list()) { // <-- here
walkDir(f); // <--
}
}
}
The strings returned by d.list() are just the file names, without a path attached to them. If you find, for example, a file some_directory/foo.txt, the string you'll pull out of the list is foo.txt, and since foo.txt is not in the current working directory, the File object you construct from it will be invalid (or describe a different file).
You'll have to make the path you're trying to inspect part of the recursion to make this work properly, for example like this:
walkDir(pathname + File.separator + f);
Or, as #Adam mentions in the comments, by passing the File object that describes the parent directory into the recursion and using the File(parent, child) constructor, as in
// new parameter here: parent directory
public void walkDir(String pathname, File parent) {
System.out.println(pathname);
File d = new File(parent, pathname); // <-- File constructor with parent
recursiveList.add(d);
if(d.isDirectory()) {
for(String f : d.list()) {
walkDir(f, d); // passing parent here
}
}
}
// entry point, to keep old interface.
public void walkDir(String pathname) {
walkDir(pathname, null);
}
Note: This answer, I think this should be mentioned, is rather tailored to OP's use case of an exercise, so I mainly tried to explain why his code didn't work and suggested ways to make it work. If you're a stray visitor in the future and looking for ways to walk through a directory with Java, look at #fge's answer for a better way.
Use the java.nio.file API, it's much better at doing things like this.
Here is an example, also using throwing-lambdas, calculating the total size of all files in a directory, recursively:
final Path theDirectory = Paths.get("path/to/your/directory");
final long totalSize = Files.walk(theDirectory)
.filter(Files::isRegularFile)
.mapToLong(Functions.rethrow(Files::size))
.sum();
If you don't have Java 8, use Files.walkFileTree().

How to create a file using java in a webapp?

I am trying to make a webapps which creates a file in a specific folder. This app should work both on windows and ubuntu but they have different file structures. So how do I mention path in creation of file and again in Ubuntu I also need to use permissions. How can I give permissions to the folder in which I am trying to create a file. I am using java for this and this is my code:
//bfr=new BufferedReader(new InputStreamReader(System.in));
String fileName="/home/hemant/"+credentials[3];
String content=String.valueOf(n)+"\n"+messages.length;
File file=new File(fileName);
if(!file.exists()){
System.out.println("filecreated");
file.createNewFile();
}
my app is a tomcat based app. What should I do? I am new to this and don't have any idea.
You can get the information of Operating System using this code :
public class OSValidator {
private static String OS = System.getProperty("os.name").toLowerCase();
public static void main(String[] args) {
System.out.println(OS);
if (isWindows()) {
System.out.println("This is Windows");
} else if (isMac()) {
System.out.println("This is Mac");
} else if (isUnix()) {
System.out.println("This is Unix or Linux");
} else if (isSolaris()) {
System.out.println("This is Solaris");
} else {
System.out.println("Your OS is not support!!");
}
}
public static boolean isWindows() {
return (OS.indexOf("win") >= 0);
}
public static boolean isMac() {
return (OS.indexOf("mac") >= 0);
}
public static boolean isUnix() {
return (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0 );
}
public static boolean isSolaris() {
return (OS.indexOf("sunos") >= 0);
}
}
If you are using servlet,you can find path using this -
String path = servletConfig.getServletContext().getRealPath("/WEB-INF")
It will work on Ubantu and WIndows.
You can check operating system, on which your webapp is deployed by:
System.getProperty("os.name");
You can create file accordingly , For giving permissions to file , full control over file attributes is available in Java 7, as part of the "new" New IO facility (NIO.2)
Or you can checkout this link for more information : http://www.mkyong.com/java/how-to-set-the-file-permission-in-java/

isDirectory() method returning 'false' when invoked on java package 'com'

I did this simple experiment to list all files/directory in a parent directory.
Did this by making a java project in eclipse by name 'JavaProject' and a class 'Temp.java' under src/com. Code is as below:
public class Temp {
public static void main(String[] args) {
search("../JavaProject");
}
public static void search(String dName) {
String[] files = new String[100];
File search = new File(dName); // make file object
if (!search.isDirectory()) {
return;
}
files = search.list(); // create the list
for (String fn : files) {// iterate through it
System.out.print(" " + fn);
File temp = new File(fn);
if (temp.isDirectory()) {
search(fn);
}
System.out.println();
}
}
}
The file structure is as below :
JavaProject(dir)
.classpath(file)
.project(file)
.settings(dir)
org.eclipse.jdt.core.prefs(file)
bin(dir)
com(file)
Temp.class(file)
src(dir)
com(dir)
Temp.java(file)
When I run the above program, it gives the following output:
.classpath
.project
.settings org.eclipse.jdt.core.prefs
bin com
src com
I cant understand why it does not print the .java file and .class file inside the com folders.
When I try debugging then the file object on 'com' returns 'false' for both isDirectory() and isFile() methods.
When it gets to the 'com' directory your code is doing:
File temp = new File("com");
Since you have not specified any path this will be taken to be relative to the current directory which is not the directory containing 'com'.
You should use something like:
File temp = new File(parent, fn);
where parent is the File object for the parent directory.
You can use listFiles() instead of list(). See below example:
public class Program {
public static void main(String args[]) throws IOException {
search(new File("."), 0);
}
public static void search(File file, int level) {
if (!file.isDirectory()) {
return;
}
for (File f : file.listFiles()) {
for (int i = 0; i < level; i++) {
System.out.print(" ");
}
System.out.println(f.getName());
if (f.isDirectory()) {
search(f, ++level);
}
}
}
}

Traversing a directory for a specific folder

enter code hereI wrote the following code which searches a folder directory recursively to find a specific folder.
The program is supposed to do check the folder name and if the folder name is "src", then it should go into that folder to get all the files. Currently the program is getting all the files from all the directories.
public class Main {
public static void main(String[] args) {
File fileObject = new File("C:\\Users\\lizzie\\Documents\\");
recursiveTraversal(fileObject);
}
public static void recursiveTraversal(File fileObject)
{
if (fileObject.isDirectory())
{
File allFiles[] = fileObject.listFiles();
for(File aFile : allFiles){
recursiveTraversal(aFile);
}
}
else if (fileObject.isFile())
{
System.out.println(fileObject.getAbsolutePath());
}
}
}
when I check if a certain folder is a directory, I added the following constraint but that didn't help.
if (fileObject.isDirectory() && fileObject.getName().equals("src"))`
Please let me know what I can do to improve my code. Anything will be appreciated.
Thanks
If you look at your if-else inside recursiveTraversal, you'll see that you're printing anything that isn't a directory, regardless of where it is. Here's a fix:
public class Main {
public static void main(String[] args) {
File fileObject = new File("C:\\Users\\lizzie\\Documents\\");
recursiveSearch(fileObject);
}
public static void recursiveSearch(File fileObject) {
if (fileObject.isDirectory()) {
if (fileObject.getName().equals("src")) {
recursivePrint(fileObject);
} else {
File allFiles[] = fileObject.listFiles();
for(File aFile : allFiles){
recursiveSearch(aFile);
}
}
}
// ignore non-directory objects during search
}
public static void recursivePrint(File fileObject)
{
if (fileObject.isDirectory())
{
File allFiles[] = fileObject.listFiles();
for(File aFile : allFiles){
recursivePrint(aFile);
}
}
else if (fileObject.isFile())
{
System.out.println(fileObject.getAbsolutePath());
}
}
}
This will print all the files recursively of any directory named src.
What you need to do is put the constraint on what's being printed, not what's being traversed. As you've noticed, the traversal is working fine, since it gets all files in all subfolders.
If you want to print only the filenames inside of the "src" directory (not in subdirectories), then you can do...
...
else if (fileObject.isFile() && fileObject.getParent().getName().equals("src")
{
System.out.println(fileObject.getAbsolutePath());
}
...
If you want to print what's in the "src" directory, and all subdirectories, then you'll need to break your algorithm into two parts
find the "src" folder, then
use your current algorithm to print everything in all directories from there and lower
Instead of checking for .equals() on the name, check if the name contains "src" using either fileObject.getName().contains(StringBuffer) or fileObject.getName().indexOf("src") != -1

Categories

Resources