【问题标题】:ListOfFiles, getPath, shuffling when have more than 3ListOfFiles,getPath,超过 3 个时改组
【发布时间】:2013-08-26 21:34:43
【问题描述】:

我用它在某个文件夹和子文件夹中显示图像,一切“工作正常”,文件以这种形式命名,例如:

17000.001 17000.002 18555.001 18542.001 1.001 1.002 1.003 1.004 .....

.xxx 是扩展名(重命名的 TIFF)

程序以这种方式工作:

你输入你想要的数字,例如:我想要 17000,我输入 17000,它在屏幕上返回第一个 .001,其他的 .002、.003 以及它有多少,我想走路通过下一个图像按钮和预先图像...

问题是:当我尝试搜索超过 4 .004 或更多的数字时,它不显示第一个,它显示“随机”、.002、004 或其他我不明白为什么,这是我得到它的“路径”的那段代码!不要因为代码杀了我^^!

....

        public void geraListaArquivos(String subdir, String matricula) {


            String diretorio = "F:\\registro_sql\\Imagens\\Livro02" + "\\"

     + subdir + "\\";
            String novaimagem = null;

            File folder = new File(diretorio);
            listOfFiles = folder.listFiles();
            if (!folder.exists()) { 
                JOptionPane.showMessageDialog(null,"Não existe o diretório em que está tentando fazer a busca");
            } else {
        //  JOptionPane.showMessageDialog(null, diretorio);
            for (int i = 0; i < listOfFiles.length; i++) {  

                String matsonome[] = listOfFiles[i].getName().split("\\.");

                for (int i2 = 0; i2 < matsonome.length; i2 = i2 +2) {

                    if(matsonome[i2].matches(matricula)) {
                        System.out.println(matsonome[i2] = "." + matsonome[i2+1]);
... the rest of the code, if the typed number image exist in the folder

我编辑字符串 matsonome 以检查数组的第一部分是否与键入的数字匹配,.. i2 +2,因为它拆分时例如 17000.001 和 17000.002

会是这样的:

matsonome[0] = 17000
matsonome[1] = 001
matsonome[2] = 17000
matsonome[3] = 002

在这种情况下"System.out.println(matsonome[i2] = "." + matsonome[i2+1]);"

将显示正确,因为它少于 4 个

17000.001 17000.002

但如果输入的数字有4个或更多,它会以这种方式显示(乱序):

xxxx.002 xxxx.001 xxxx.004 xxxx.003

为什么???

对不起英语不好:(

【问题讨论】:

  • 当您有这样的问题时,请学会阅读文档。以下是 File.listFiles() 文档中的引述:“无法保证结果数组中的名称字符串会以任何特定顺序出现;尤其不能保证它们按字母顺序出现...... " 你应该自己对字符串数组进行排序。

标签: java file list


【解决方案1】:

我相信这是因为无法保证订单(如果我正确理解问题的话)。

documentation:

无法保证结果数组中的名称字符串 将以任何特定顺序出现;它们不是,特别是, 保证按字母顺序出现。

这意味着您必须使用 static function. 对数组进行排序

【讨论】:

    猜你喜欢
    • 2019-07-09
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 2018-02-07
    相关资源
    最近更新 更多