0

Im trying to read 100 text files from a folder and save them to a matrix, and it works perfectly for the first 99 text files. This could be a trivial mistake, but I can not find it. Thank you in advance!

package mba_prob;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ConverTo {
    public static void main(String[] args) throws NumberFormatException, IOException{
        String target_dir = "/directory";
        File dir = new File(target_dir);
        File[] files = dir.listFiles();

        for (File f : files) {
            if(f.isFile()) {
                double [][]thematrix = readMatrix(f);
            }
        }
    } 
}
erda
  • 55
  • 9
  • Try to reduce the posted code to the parts that are relevant to the question. In this form it is hard to work through all of it, discouraging people from helping – LionC Jan 29 '15 at 15:18
  • So what *does* happen to the last file? Are you sure it's not just because you're missing a call to `System.println` afterwards? – Jon Skeet Jan 29 '15 at 15:19
  • The last file is not read maybe it does not iterate 100 times. No reason to not be printed out, well I'm not sure.. – erda Jan 29 '15 at 15:23
  • Ok, it is probably not printed out.. but i still can't understand why.. aware that this problem is not in the code. thankaaa – erda Jan 29 '15 at 15:55
  • Hello, sorry for writing again, but I can't solve this problem. I wrote all the text files in another folder and there are only 99 files written. Any suggestions? – erda Jan 29 '15 at 17:42

1 Answers1

0

Is it possible that the last file is in use?

Try using java.io.File.exists()

for (File f : files) {
            if(f.exists()) {
                double [][]thematrix = readMatrix(f);
            }
        }

Java File.exists() versus File.isFile()

Community
  • 1
  • 1
drop27
  • 143
  • 2
  • 11
  • This works the same, but I just realised that the number of files is the same, can it be that it duplicates or lists them not in the same order as from the directory they are read from? – erda Jan 30 '15 at 10:54
  • Is it possible that the file that isn't being found has different read/write permission from the other 99? – drop27 Jan 30 '15 at 17:10
  • No I generated the files with R, they have all the same read/write permission. – erda Jan 31 '15 at 13:39
  • Ok I found out the problem, there was a hidden file in the directory. Thanks for your answers – erda Jan 31 '15 at 16:32