'Writing in a font file

I need to create a method that given the file name and an integer n writes to a character file with that name, n random integers, one per line. This is my code, I think it is written correctly but the file I pass remains "empty" with size 0 bytes. Can someone help me?

   public static void scriviIntero(String nomeFile, int n) {
    try (PrintWriter scrivi = new PrintWriter(new FileWriter(nomeFile, true))) {
    Random random = new Random();
        for (int i = 0; i < n; i++) {
            int nuovo = random.nextInt(99999);
            scrivi.println(nuovo);
        }
    } catch (IOException e) {
        System.out.println("Errore di I/O nella funzione scriviIntero nel tentativo di scrivere sul file " + nomeFile);
    }
    
}


Solution 1:[1]

Your problem is incorrect initialization of FileWriter. Check this out where I put true:

public static void main(String[] args) throws IOException {
    appendRandomNumbersToFile("e:/foo.txt", 10);
    appendRandomNumbersToFile("e:/foo.txt", 20);
}

public static void appendRandomNumbersToFile(String fileName, int n) throws IOException {
    if (n <= 0)
        throw new RuntimeException("n should be positive");

    try (PrintWriter writer = new PrintWriter(new FileWriter(fileName, true))) {
        Random random = new Random();

        for (int i = 0; i < n; i++)
            writer.println(random.nextInt());
    }
}

P.S. This is from JavaDoc:

public FileWriter(String fileName, boolean append) {}
public PrintWriter(Writer out, boolean autoFlush) {}

Solution 2:[2]

The problem seems to be that you assign frameMerge a new part of the image with every iteration. Instead you need to append/add to the already assigned value of frameMerge

# untested
parameters = pd.read_csv('parameters.csv')
frameMerge = "init whatever datatype frameMerge needs to be"
for ind in parameters.index: 
    x = parameters['x'][ind]
    y = parameters['y'][ind]
    w = parameters['w'][ind]
    h = parameters['h'][ind]
    # add/append to existing frameMerge
    frameMerge += imgScratches[y:y+h,x:x+w]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 oleg.cherednik
Solution 2 st.huber