I am trying to perform a scan of the project containing the word stub for searching files in it. Files in UTF-16 encoding. However, when the parser comes to 1017 files, all with the error:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space.
How I can fix it?
import java.io.*;
import java.util.Scanner;
public class FolderScanerV2 {
public static void FolderScaner(File file) throws Exception {
if (file.isDirectory()) {
File[] directory = file.listFiles();
for (File enter : directory) {
if ((enter.isFile() & enter.getCanonicalPath().contains("txt"))) {
FileScaner(enter);
} else {
FolderScaner(enter);
}
}
}
}
public static void FileScaner(File file) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file.getAbsolutePath()), "UTF-16"));
Scanner text = new Scanner(in);
while (text.hasNext()) {
if(text.next().contains("Cap")) {
System.out.println("Cap" + " " + file.getCanonicalPath());
}
}
text.close();
in.close();
}
public static void main(String[] args) {
String path = new String();
Scanner inpunt = new Scanner(System.in);
System.out.println("Input path");
path = inpunt.nextLine(); //
File dir = new File(path);
path.close();
try {
FolderScaner(dir);
} catch (Exception e) {
e.printStackTrace();
}
}
}