Get files inside folder [duplicate] - java

This question already has answers here:
How to read all files in a folder from Java?
(33 answers)
Get a list of resources from classpath directory
(15 answers)
Closed 6 years ago.
I am trying to read the content of the folder of my Java EE Spring application, but it always return me null. The folder I want is under src/main/resources folder and it`s called context. I try doing it this way :
File file = new File("src/main/resources/context")
But it always return me null for file.

You can use one of the following:
File file = new File("src/main/resources/context");
String[] list = file.list(); // returns an array of all file names in the context folder.
OR
File file = new File("src/main/resources/context");
File[] listFiles = file.listFiles(); // returns an array of all "file objects" for all files in the context folder.
Hope this helps!

Related

How to check if file exists knowing only file name? [duplicate]

This question already has answers here:
How do I check if a file exists in Java?
(19 answers)
Closed 3 years ago.
In Java using Maven project we may read file's content as a stream by knowing only file's name, for example:
InputStream in = getClass().getResourceAsStream("/" + fileName);
But is there way to check if the file exists without indicating the whole path, just passing file name?
file.exists() can be used to check whether such a file exists or not.Like following:
File file = new File("filepath");
if(file.exists()){
// Do your stuff
}

Java 8 : Get files from folder / subfolder [duplicate]

This question already has answers here:
Recursively list all files within a directory using nio.file.DirectoryStream;
(9 answers)
Closed 5 years ago.
I have this folders inside the resources folder of a SpringBoot app.
resources/files/a.txt
resources/files/b/b1.txt
resources/files/b/b2.txt
resources/files/c/c1.txt
resources/files/c/c2.txt
I want to get all the txt file, so this is my code:
ClassLoader classLoader = this.getClass().getClassLoader();
Path configFilePath = Paths.get(classLoader.getResource("files").toURI());
List<Path> atrackFileNames = Files.list(configFilePath)
.filter(s -> s.toString().endsWith(".txt"))
.map(Path::getFileName)
.sorted()
.collect(toList());
But I only get the file a.txt
Path configFilePath = FileSystems.getDefault()
.getPath("C:\\Users\\sharmaat\\Desktop\\issue\\stores");
List<Path> fileWithName = Files.walk(configFilePath)
.filter(s -> s.toString().endsWith(".java"))
.map(Path::getFileName).sorted().collect(Collectors.toList());
for (Path name : fileWithName) {
// printing the name of file in every sub folder
System.out.println(name);
}
Files.list(path) method returns only stream of files in directory. And the method listing is not recursive.
Instead of that you should use Files.walk(path). This method walks through all file tree rooted at a given starting directory.
More about it:
https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#walk-java.nio.file.Path-java.nio.file.FileVisitOption...-

How to create a file & folder in Java? [duplicate]

This question already has answers here:
create a text file in a folder
(4 answers)
Closed 9 years ago.
Okay, updated this right now
As of now I have this:
File saveGame = new File(Config.saveDir,Config.saveName);
Now how would I create that into a text file? My saveDir has already been created (mkDir), and my saveName is defined as "xyz.txt", so what method do I use to create this file (and later add text into it)?
new File(...) doesn't crete a file on disk, it only creates a path for a Java program to use to refer to a file that may or may not exist on disk (hence the File.exists() method).
Try userFile.createNewFile(); to actually create the file on disk.
To make the directory you would need to use the File.mkdirs() method, but don't call it on userFile or it will make a directory with the Savegame.txt in it.
Edit:
File dir = new File(Config.userpath + "/test/");
File file = new File(dir, "," + Config.name + " Savegame.txt");
dir.mkdir(); // should check to see if it succeeds (if(dir.mkdir())...)
file.createNewFile(); // should also check that this succeeds.

How to scan a folder for files and their locations in java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to scan a folder in Java?
I want to scan a given folder for all of the files within the folder and add the locations/paths (ex: "c:/users/peter/desktop/image.jpg") to an arraylist of strings. How could i do this? Thanks for the help
Peek around in the java.io.File API. There are at least three methods which may be of use:
listFiles()
isDirectory()
isFile()
You could try
File directory = new File("<Path to directory>");
String [] directoryContents = directory.list();
List<String> fileLocations = new ArrayList<String>();
for(String fileName: directoryContents) {
File temp = new File(String.valueOf(directory),fileName);
fileLocations.add(String.valueOf(temp));
}
Check out File.list()

How can I iterate entries in a URL resource pointing to a folder in a JAR [duplicate]

This question already has answers here:
How to list the files inside a JAR file?
(17 answers)
Closed 7 years ago.
I have a situation where an app I'm writing works fine from the IDE but fails when deployed into a jar. What I see is NULL pointer exception. What I'm trying to do is get a URL resource of a directory and then iterate through the files in that directory. The URL seems to work but I can't find a way to get the files from it.
So I can't seem to get a file list (because this is really a resouce list inside the jar).
Any ideas?
TIA
URL scriptFolder = getClass().getResource("/scripts/");
log.debug(scriptFolder);
if (scriptFolder != null) {
File folder = new File(scriptFolder.getFile());
File[] files = folder.listFiles();
// files is NULL here.
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
log.debug("File " + files[i].getName());
}
}
}
See "List files inside a JAR".

Categories

Resources