忽略主文件夹中的特定文件,在读取 .txt 文件时使用 ArrayList。

huangapple go评论66阅读模式
英文:

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(&quot;/path to /Configs&quot;);
        ArrayList&lt;File&gt; fileList = new ArrayList&lt;File&gt;(Arrays.asList(file.listFiles()));
        
        System.out.println(fileList);
        
        File ignorefile = new File(&quot;/path to /ignore.txt&quot;);
		Scanner sc = new Scanner(ignorefile); 
        ArrayList&lt;List&gt; ignorelist = new ArrayList&lt;List&gt;();

	    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(&quot;path of my main folder&quot;);
        ArrayList&lt;File&gt; fileList = new ArrayList&lt;File(Arrays.asList(file.listFiles()));
        
        File ignoreFile = new File(&quot;path to /ignore.txt&quot;);
        Scanner sc = new Scanner(ignorefile); 
        ArrayList&lt;String&gt; listOfLines = new ArrayList&lt;&gt;();
        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&lt;Path&gt; ignore = new HashSet&lt;Path&gt;();
    ArrayList&lt;Path&gt; fileList = new ArrayList&lt;Path&gt;();

    try(Stream&lt;String&gt; stream = Files.lines(ignoreFile)) {
        stream.map(Path::of).forEach(ignore::add);
    }
    try(Stream&lt;Path&gt; stream = Files.list(dir)) {
        stream.filter(p -&gt; !ignore.contains(p) &amp;&amp; !ignore.contains(p.getFileName()))
              .forEach(fileList::add);
    }
    System.out.println(&quot;Files#&quot;+fileList.size()+&#39; &#39;+fileList);
  }

huangapple
  • 本文由 发表于 2020年8月12日 02:23:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63364217.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定