public class File_Reader
{
public static void main(String[] args) throws FileNotFoundException,IOException
{
int count_files=0;
int count_lines=0;
ArrayList<String> list_row = new ArrayList<String>();
File dir = new File("/home/sumit/Desktop/split_20");//folder is loaded
if(dir.exists())
{
int i=0;
for (File file : dir.listFiles())
{
Scanner s = new Scanner(file);
System.out.println(file.getAbsoluteFile());
fl=file.getAbsoluteFile();
while (s.hasNext())
{
list_row.add(s.next());//adding all elements
count_lines++;
}
String str[];
str=new String[count_lines];
for(int p=0 ; p<count_lines ; p++)
{
str[p]=list_row.get(p);
}
count_files++;
s.close();
}
}
System.out.println("Count files = "+count_files);
}
}
我的文件分类如1.csv,2.csv,3.csv ....等等。 所以我希望我的程序读出这些文件,因为它们出现在文件夹中。但程序正在随机读取它们,如11.csv,8.csv,20.csv ......等等。 我在文件夹中有24个文件。
答案 0 :(得分:2)
在Javadoc中,您会看到File.listFiles()
返回文件的顺序无法保证您需要对返回的列表进行排序:请参阅Collections.sort()
如果文件的默认顺序不符合您的需要,您可以定义自己的比较器。
File.compareTo()的默认排序:
按字典顺序比较两个抽象路径名。订购 由此方法定义的取决于底层系统。在UNIX上 系统,字母表案例在比较路径名时很重要;上 它不是Microsoft Windows系统。
答案 1 :(得分:0)
你不能直接这样做。只需对文件列表进行排序。然后使用该排序列表。
List<File> sortedDirs = Arrays.asList(dir.listFiles());
Collections.sort(sortedDirs, new Comparator<File>() {
@Override
public int compare(File f1, File f2) {
return f1.getName().compareTo(f2.getName());
}
});
for (File file : sortedDirs){
...
}
Collections.sort(列表)无效。因为File.compareTo()
并不保证在所有操作系统上返回相同的值。因此,您必须提供我自己的Comparator
,如上所示。并且必须使用 Collections.sort(List,Comparator)版本
答案 2 :(得分:0)
您应该在for
之前对文件名进行排序,例如
File[] files = dir.listFiles();
List<String> filePaths = new ArrayList<>();
for(File f : files) {
filePaths.add(f.getName());
}
Collections.sort(files);
for(String fName : files) {
File sortedFile = new File(fName);
// and so on
答案 3 :(得分:0)
如其他答案中所述,您必须对文件列表进行排序。 Java NIO.2中有一些方便的函数可以将整个文件读入List<String>
并遍历文件树。
在我的解决方案中,我使用TreeMap
对文件路径进行排序,并在遍历文件树时读取文件。
此外,您可以通过调用List<String>
将List::toArray(T[])
转换为数组。所以你可以写:
public static String[] readAllFiles(String path) throws IOException {
Map<Path, List<String>> readFiles = new TreeMap<>();
Files.walkFileTree(Paths.get(path), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Objects.requireNonNull(file);
readFiles.put(file, Files.readAllLines(file, StandardCharsets.UTF_8));
return FileVisitResult.CONTINUE;
}
});
List<String> lines = new ArrayList<>();
for (List<String> read : readFiles.values()) {
lines.addAll(read);
}
return lines.toArray(new String[lines.size()]);
}
<强> 修改 强>
如果您的文件名只包含数字,那么您可以通过正则表达式/拆分/等提取数字。文件名。
然后你必须将它添加到TreeMap
Map<Path, List<String>> readFiles = new TreeMap<>(new Comparator<Path>() {
@Override
public int compare(Path o1, Path o2) {
Matcher o1Matcher = NUMBER_PATTERN.matcher(o1.getFileName().toString());
Matcher o2Matcher = NUMBER_PATTERN.matcher(o2.getFileName().toString());
if (o1Matcher.find() && o2Matcher.find()) {
return Integer.compare(Integer.parseInt(o1Matcher.group()), Integer.parseInt(o2Matcher.group()));
} else {
return o1.compareTo(o2);
}
}
});
并将此模式用作类中的静态字段
private static final Pattern NUMBER_PATTERN = Pattern.compile("(\\d+)");