java中如何从文件夹中获取单个文件?

发布于 2024-09-11 22:02:27 字数 640 浏览 4 评论 0原文

我的想法是获取一个文件,但我不想列出所有文件。我有指定文件夹的地址,但没有名称。

基本上我想要

findFileInFolder(StringfolderName) --- 这个方法返回一个随机文件名或在文件夹上创建的最旧的文件

有人尝试过这样做吗?有什么想法可以避免列出数组中的所有文件然后获取第一个文件?


补充:

以防万一我不清楚(我真的很抱歉我的英语。如果我听起来很强势或咄咄逼人,请原谅我,这真的不是我的意图。)该文件不是由人类选择的,而是由 字符串的方法之外,

机器而不询问或显示文件,除了返回带有 FileName String findFileInFolder(StringfolderName) 的

就像我的评论是针对 ram 和处理器的使用一样,因为这是一个辅助进程,而不是项目的主要进程,所以如果我必须读取一千多个文件,这将大大降低我的项目的性能:(

谢谢;)


更新:该程序在不同的计算机上运行,​​所以如果我可以只访问该目录而不是“思考”读取哪个文件伟大的。 =D


希望最后一次更新:抱歉打扰你们了:)

从我读到的答案来看,没有办法。我的问题是:您认为除了数组之外还有什么好的替代方案?我的想法是在文本文件中创建索引并仅获取第一行。

The idea is to take one single file, but I don't want to list all of the files. I have the address of the specified folder, but not the name.

Basically I want

findFileInFolder(String folderName) --- this method returns a random filename or the oldest file created on the folder

Has anybody ever tried doing this? Any ideas to avoid listing all of the files in an array and then taking the first one?


Added:

Just in case I wasn't clear (I'm really sorry for my English. Please forgive me if I sound prepotent or aggressive it is really not my intentions.) The file is not selected by a human, it's selected by the machine without asking or showing the file except for the method that returns a string with the FileName

String findFileInFolder(String folderName)

Like I comment is for usage of ram and processor because this is a secondary process and not the primary of the project, so if I have to read over a thousand files it will considerably reduce the performance of my project :(

Thanks ;)


Update: the program is running on different computers so if I could just access the directory not "thinking" about reading which file it would be great. =D


Hopefully last update: sorry for bothering you guys :)

From what I read on the answers there is no way. My question is: what good alternatives instead of doing an array would you think? My idea is to create an index in a textfile and to take only the first line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷心人i 2024-09-18 22:02:27

我决定使用这段代码,这并不完全是我想要的,但它现在有效。

  public static String getFileToCrawl(String directory){
      File dir = new File(directory);

      String[] children = dir.list();
      if (children == null) {
          return "";
      } else {
          int i=0;
          String filename = children[i];
          while (i<children.length && !filename.contains(".txt")){
              i++;
              filename = children[i];
          }
          return filename;
      }

  }

如果有人喜欢它或知道改进此代码的方法,我们非常欢迎;)如果您想使用它,请随意:D

I decidee to use this code, is not exactly what I wanted but it works for now.

  public static String getFileToCrawl(String directory){
      File dir = new File(directory);

      String[] children = dir.list();
      if (children == null) {
          return "";
      } else {
          int i=0;
          String filename = children[i];
          while (i<children.length && !filename.contains(".txt")){
              i++;
              filename = children[i];
          }
          return filename;
      }

  }

if anyone like it or know a way to improve this code it's really welcome ;) if you want to use it feel free :D

野の 2024-09-18 22:02:27

您可以查看 FileFilter 类和 public File[] listFiles(FileFilter filter)

使用此方法,您将能够根据文件的最后修改日期过滤文件,例如。

顺便说一句,出于性能考虑,为什么要避免列出所有文件?

You can have a look on FileFilter class and on the public File[] listFiles(FileFilter filter)

Using this method you will be able to filter files according to their last modification date for example.

On a side note, why do you want to avoid to list all the files, for performances concerns ?

亣腦蒛氧 2024-09-18 22:02:27

在当前版本的 java 中,除了列出所有文件并选择您需要的文件之外,没有其他方法可以做到这一点。如果您可以使用java 7,则有一个 FileVisitor 类可以遍历文件夹树而不列出所有文件。

There is no way of doing this in current versions of java other than listing all files and selecting what you need. If you can use java 7, there is a FileVisitor class that can walk a folder tree without listing all of the files.

此生挚爱伱 2024-09-18 22:02:27

我意识到这是一个旧线程,但这是一种快速而肮脏的方法:

import java.io.File;
import java.util.Arrays;
import java.util.Collections;

public class Shuffle {
   public static void main(String[] argv) 
   throws Exception {

      File dir = new File(".");
      String[] children = dir.list();
      Collections.shuffle(Arrays.asList(children));
      System.out.println(children[0]);
   }   
}

I realize this is an old thread but here is a quick and dirty way to do it:

import java.io.File;
import java.util.Arrays;
import java.util.Collections;

public class Shuffle {
   public static void main(String[] argv) 
   throws Exception {

      File dir = new File(".");
      String[] children = dir.list();
      Collections.shuffle(Arrays.asList(children));
      System.out.println(children[0]);
   }   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文