How to Read All Files in a Folder from Java

How to read all files in a folder from Java?

public void listFilesForFolder(final File folder) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
System.out.println(fileEntry.getName());
}
}
}

final File folder = new File("/home/you/Desktop");
listFilesForFolder(folder);

Files.walk API is available from Java 8.

try (Stream<Path> paths = Files.walk(Paths.get("/home/you/Desktop"))) {
paths
.filter(Files::isRegularFile)
.forEach(System.out::println);
}

The example uses try-with-resources pattern recommended in API guide. It ensures that no matter circumstances the stream will be closed.

How to read data from all files one by one in a folder with Java?

You could do it without any external library, just with java.nio.

The procedure is as follows:

The code takes a path as String and creates a java.nio.Path out of it (done with a constant String path in this example). Then it prepares a Map<String, List<Strings> that is to store the file name and its content as one String per line. After that, it reads the directory content using the DirectoryStream<Path> from java.nio into a list of file paths and stores both, the content read by Files.readAllLines(Path path) as a List<String> and the absolute file path in the Map.

Finally, it just prints out everything read, separated by a line of long dashes...

Just try it, maybe it is helpful for your search.

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class FileContentReader {

private static final String FOLDER_PATH = "Y:\\our\\destination\\folder\\";

public static void main(String[] args) {
Path folderPath = Paths.get(FOLDER_PATH);

// prepare a data structure for a file's name and content
Map<String, List<String>> linesOfFiles = new TreeMap<String, List<String>>();

// retrieve a list of the files in the folder
List<String> fileNames = new ArrayList<>();
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(folderPath)) {
for (Path path : directoryStream) {
fileNames.add(path.toString());
}
} catch (IOException ex) {
System.err.println("Error reading files");
ex.printStackTrace();
}

// go through the list of files
for (String file : fileNames) {
try {
// put the file's name and its content into the data structure
List<String> lines = Files.readAllLines(folderPath.resolve(file));
linesOfFiles.put(file, lines);
} catch (IOException e) {
e.printStackTrace();
}
}

// finally, print everything
linesOfFiles.forEach((String fileName, List<String> lines) -> {
System.out.println("Content of " + fileName + " is:");
lines.forEach((String line) -> {
System.out.println(line);
});
System.out.println("————————————————————————————————");
});
}
}

how to open all files in a directory in java, read them, creat new files, write to them

File f = new File("your folder path here");// your folder path

//**Edit** It is array of Strings
String[] fileList = f.list(); // It gives list of all files in the folder.

for(String str : fileList){
if(str.endsWith(".jack")){

// Read the content of file "str" and store it in some variable

FileReader reader = new FileReader("your folder path"+str);
char[] chars = new char[(int) new File("your folder path"+str).length()];
reader.read(chars);
String content = new String(chars);
reader.close();

// now write the content in xml file

BufferedWriter bw = new BufferedWriter(
new FileWriter("you folder path"+str.replace(".jack",".xml")));
bw.write(content); //now you can write that variable in your file.

bw.close();
}
}

Getting the filenames of all files in a folder

You could do it like that:

File folder = new File("your/path");
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
System.out.println("File " + listOfFiles[i].getName());
} else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory " + listOfFiles[i].getName());
}
}

Do you want to only get JPEG files or all files?

Java - Read all .txt files in folder

Something like the following should get you going, note that I use apache commons FileUtils instead of messing with buffers and streams for simplicity...

File folder = new File("/path/to/files");
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {
File file = listOfFiles[i];
if (file.isFile() && file.getName().endsWith(".txt")) {
String content = FileUtils.readFileToString(file);
/* do somthing with content */
}
}

list all files in the folder and also sub folders

Using you current code, make this tweak:

public void listf(String directoryName, List<File> files) {
File directory = new File(directoryName);

// Get all files from a directory.
File[] fList = directory.listFiles();
if(fList != null)
for (File file : fList) {
if (file.isFile()) {
files.add(file);
} else if (file.isDirectory()) {
listf(file.getAbsolutePath(), files);
}
}
}

how to get list of all files from folder inside java resources folder

Found solution to fetch all files inside folder.

if we try to access folder by getResource() method it will return in terms of vfs protocol and we need to convert that to

public List<String> loadFiles() throws IOException, Exception {
List<String> fileNames = new ArrayList<>();
URL resourceUrl = getClass().getResource("/protocol");
VirtualJarInputStream jarInputStream = (VirtualJarInputStream) resourceUrl.openStream();
JarEntry jarEntry = null;
while ((next = jarInputStream.getNextJarEntry()) != null) {
fileNames.add(jarEntry.getName());
}
return fileNames;
}



Related Topics



Leave a reply



Submit