javafilewriter

Java FileWriter skip files


I have a package with files : "100.txt" ,"1000.txt", "10000.txt","100000.txt", "1000000.txt". But when i launch my program, it skips 100 and 1000 txt files. Can you tell me why?

public void generateData(Path path) throws IOException {
        for (int i = 1; i <= 5; i++){
            for (int j = 100; j <= 1000000; j*=10){
                FileWriter fileWriter = new FileWriter(path.resolve(Integer.toString(i)).resolve(j + ".txt").toFile());
                Random random = new Random();
                for (int k = 0; k < j; k++){
                    int num = random.nextInt(10);
                    fileWriter.write(num);
                    fileWriter.write("\n");
                }
            }
        }
    }

Solution

  • I ran your code and everything seemed to run pretty well.

    All files were printed:

    One thing I noticed is that you don't close your FileWriter after you're done with it. Perhaps that is the problem.

    Here is the full code I ran. I did remove the writing of the files into the numbered directories since those directories didn't exist on my machine.

    package forloop;
    
    import java.io.FileWriter;
    import java.io.IOException;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.Random;
    
    public class GenerateData {
    
        public static void main(String[] args) throws IOException {
    
            Path path = Paths.get("");
    
            generateData(path);
        }
    
        public static void generateData(Path path) throws IOException {
            for (int i = 1; i <= 5; i++){
                for (int j = 100; j <= 1000000; j*=10){
                    FileWriter fileWriter = new FileWriter(path.resolve(j + ".txt").toFile());
                    Random random = new Random();
                    for (int k = 0; k < j; k++){
                        int num = random.nextInt(10);
                        fileWriter.write(num);
                        fileWriter.write("\n");
                    }
                    fileWriter.close();
                }
            }
        }
    }
    

    Based on Andy's suggestion a solution that uses a try-with-resources.

    package forloop;
    
    import java.io.FileWriter;
    import java.io.IOException;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.Random;
    
    public class GenerateData {
    
        public static void main(String[] args) throws IOException {
    
            Path path = Paths.get("");
    
            generateData(path);
        }
    
        public static void generateData(Path path) throws IOException {
            for (int i = 1; i <= 5; i++) {
                for (int j = 100; j <= 1000000; j*=10) {
                    try (FileWriter fileWriter = new FileWriter(path.resolve(j + ".txt").toFile())) {
                        Random random = new Random();
                        for (int k = 0; k < j; k++){
                            int num = random.nextInt(10);
                            fileWriter.write(num);
                            fileWriter.write("\n");
                        }
                    }
                }
            }
        }
    }