英文:
how to Ignore certain files from main folder when reading .txt file using arraylist
问题
public class filetext {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("/主文件夹路径/Configs");
ArrayList<File> fileList = new ArrayList<File>(Arrays.asList(file.listFiles()));
System.out.println(fileList);
File ignorefile = new File("/主文件夹路径/ignore.txt");
Scanner sc = new Scanner(ignorefile);
ArrayList<String> ignorelist = new ArrayList<String>();
System.out.println(ignorefile);
while(sc.hasNextLine()) {
ignorelist.add(sc.nextLine());
}
fileList.removeIf(f -> ignorelist.contains(f.getName()));
System.out.println(fileList);
}
}
英文:
i have a main folder(config) and a text file called ignore.txt. i have mentioned few files to ignore from main folder in ignore.txt file. i need some help how do i call ignore.txt file to execute and ignore files from the main folder(config).
public class filetext {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("/path to /Configs");
ArrayList<File> fileList = new ArrayList<File>(Arrays.asList(file.listFiles()));
System.out.println(fileList);
File ignorefile = new File("/path to /ignore.txt");
Scanner sc = new Scanner(ignorefile);
ArrayList<List> ignorelist = new ArrayList<List>();
System.out.println(ignorefile);
if(ignorelist.contains(file));
fileList.remove(file);
System.out.println(ignorelist);
}
答案1
得分: 0
以下是翻译好的代码部分:
根据我理解,您想要从文件列表中删除“忽略的文件”。
要从您的fileList中删除加载的“忽略的名称”,您可以编辑您的代码,类似于以下方式,尽管这不是一种高效的方法:
public static void main(String[] args) throws FileNotFoundException {
File file = new File("主文件夹的路径");
ArrayList<File> fileList = new ArrayList<File>(Arrays.asList(file.listFiles()));
File ignoreFile = new File("忽略文件的路径");
Scanner sc = new Scanner(ignoreFile);
ArrayList<String> listOfLines = new ArrayList<>();
while (sc.hasNextLine())
listOfLines.add(sc.nextLine());
for(File f : fileList){
if(listOfLines.contains(f.getName()))
fileList.remove(f);
}
// 再次打印fileList
}
英文:
As far as I am understanding, you want to remove the "ignored files" from your files list.
To remove the loaded "ignored names" from your fileList, you can edit your code to something like this although it is not an efficient approach:
public static void main(String[] args) throws FileNotFoundException {
File file = new File("path of my main folder");
ArrayList<File> fileList = new ArrayList<File(Arrays.asList(file.listFiles()));
File ignoreFile = new File("path to /ignore.txt");
Scanner sc = new Scanner(ignorefile);
ArrayList<String> listOfLines = new ArrayList<>();
while (sc.hasNextLine())
listOfLines.add(scanner.nextLine());
for(File f : fileList){
if(listOfLines.contains(f.getFileName())
fileList.remove(f);
}
// print the fileList again
}
答案2
得分: 0
这个版本使用NIO和流。
public static void main(String[] args) throws IOException {
Path dir = Path.of(args[0]);
Path ignoreFile = Path.of(args[1]);
HashSet<Path> ignore = new HashSet<Path>();
ArrayList<Path> fileList = new ArrayList<Path>();
try (Stream<String> stream = Files.lines(ignoreFile)) {
stream.map(Path::of).forEach(ignore::add);
}
try (Stream<Path> stream = Files.list(dir)) {
stream.filter(p -> !ignore.contains(p) && !ignore.contains(p.getFileName()))
.forEach(fileList::add);
}
System.out.println("Files#" + fileList.size() + ' ' + fileList);
}
如果需要更多帮助,请随时告诉我。
英文:
This version uses NIO and streams.
public static void main(String[] args) throws IOException {
Path dir = Path.of(args[0]);
Path ignoreFile = Path.of(args[1]);
HashSet<Path> ignore = new HashSet<Path>();
ArrayList<Path> fileList = new ArrayList<Path>();
try(Stream<String> stream = Files.lines(ignoreFile)) {
stream.map(Path::of).forEach(ignore::add);
}
try(Stream<Path> stream = Files.list(dir)) {
stream.filter(p -> !ignore.contains(p) && !ignore.contains(p.getFileName()))
.forEach(fileList::add);
}
System.out.println("Files#"+fileList.size()+' '+fileList);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论