'Using a Scanner to read multiple files in a loop
For a school project I am attempting to create a program that uses a Scanner to read each individual line of a text file and store it in a variable. The program does this for every file in a given directory.
public static void init(final File folder) throws FileNotFoundException {
String foo, bar;
int slime, grit, ball, funk;
int count = folder.list().length;
for (final File fileEntry: folder.listFiles()) {
if(fileEntry.isDirectory()) {
init(fileEntry);
} else {
readfile(folder);
}
}
I have succeeded in making the program do this for one file without the loop, however once I added the loop, it wouldn't run. I got the code to compile though. The .txt files only have a certain amount of lines in them, each with similar content.
public static void readfile(final File folder) throws FileNotFoundException {
File file = folder;
String foo, bar;
int slime, grit, ball, funk;
Scanner input = new Scanner(file);
while (input.hasNextLine()) {
foo = input.nextLine();
System.out.println(foo);
bar = input.nextLine();
System.out.println(bar);
slime = input.nextInt();
System.out.println(slime);
grit = input.nextInt();
System.out.println(grit);
ball = input.nextInt();
System.out.println(ball);
funk = input.nextInt();
System.out.println(funk);
break;
}
}
Upon running the file I get this error:
Exception in thread "main" java.io FileNotFoundException: home/work/txt (Is a directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
at java.base/java.io.FileInputStream<init>(FileInputStream.java:157)
at java.base/java.util.Scanner.<init>(Scanner.java:639)
at FileRead.readfile(FileRead.java:24)
at FileRead.init(FileRead.java:75)
at FileRead.main(FileRead.java:12)
The main method is nothing out of the ordinary either.
public class FileRead {
public static void main(String args[]) throws FileNotFoundException {
final File folder = new File("/home/work/txt");
init(folder);
}
The goal of the program is to create an object that stores the string and int values for later use. There will be a few calculations but I don't think that these details will be important for this as they have not been implemented. I am running this on an Ubuntu VM. If anyone can provide any answers I would be very grateful, thanks!
Solution 1:[1]
I think instead of
} else {
readfile(folder);
}
you want to use
} else {
readfile(fileEntry);
}
Otherwise even though you are checking if the current element is a file, you still pass the current folder as the parameter.
Solution 2:[2]
You can't use Scanner to read all files from a directory like this.
You'd be better off using nio and Files.readAllBytes();
public static void init(final File folder) throws FileNotFoundException {
List<String> fileStrings = new ArrayList<String>();
File[] files = folder.listFiles();
for (int i = 0; i < files.length; i++) {
filesStrings.add(new String(Files.readAllBytes(files[i].toPath())));
}
}
Solution 3:[3]
To combine what @ControlAltDel did with another method of directory iteration (in this case for all the .txt directories). You can test this by passing the path to the directory on the command line:
import java.util.List;
import java.util.ArrayList;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.DirectoryStream;
import java.nio.file.DirectoryIteratorException;
import java.io.IOException;
public class AllFilesScanner {
public static void main(String[] args) {
Path dir = Path.of(args[0]);
List<String> fileStrings = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.{txt}")) {
for (Path entry : stream) {
fileStrings.add(new String(Files.readAllBytes(entry)));
}
} catch (DirectoryIteratorException ex) {
ex.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(fileStrings);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | f1sh |
| Solution 2 | ControlAltDel |
| Solution 3 | g00se |
