Home > OS >  Why won't my lambda write to the .txt files it creates?
Why won't my lambda write to the .txt files it creates?

Time:11-18

I have an AWS lambda program written in Java that is supposed to create a bunch of txt files then write URLs to them and move them to a bucket. It appears to be creating the .txt files in the /tmp/ folder of the lambda but not writing anything to them, because I keep getting FL2: null. The bucket gets .txt files with the right names but they're empty.

The FileNames are all of the format "/tmp/[name].txt". The map has txt filenames followed by a list of URLs. The buffered reader was simply my own code to see if it could read the .txts that were created.

for (Map.Entry<String,  ArrayList<String>> entry: files.entrySet()) {
        String fileName = entry.getKey();
        ArrayList<String> list = entry.getValue();
        File file = new File(fileName);
        FileWriter writer = null;
        BufferedWriter bw = null;
        BufferedReader br = null;
        try {
            if (!file.exists()){
                log.info("It doesn't exist as a temp.");
                if( !file.createNewFile()){
                    log.error(fileName " not created. Skipping");
                    continue;
                }
            }
            writer = new FileWriter(file, StandardCharsets.UTF_8);
            bw = new BufferedWriter(writer);
            for (int i=0; i< list.size(); i  ) {
                String url = list.get(i);
                log.info("Inserting "   url   " into file "   fileName);
                if (i !=0){
                    bw.write("\n");
                }
                bw.write(url);
            }
            br = new BufferedReader(new FileReader(file, StandardCharsets.UTF_8));
            log.info("FL2: " br.readLine());
            String key = fileName.substring(5);
            amazonS3.putObject("[bucketname]", key, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            try {
                if (writer != null) {
                    writer.close();
                }
                if (bw != null) {
                    bw.close();
                }
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    log.info("End of the sitemap generator");
}

I tried the above code, and I tried printWriter turning into a bufferedWriter.

CodePudding user response:

Your code is writing to the text file and later reading from the same text file. But ... your code doesn't close the writer until the finally section of code so the read happens before the writer closes and consequently the written data, which is buffered, has not been flushed to disk.

The fix is to close the buffered writer before reading from the same file.

Also, you can reduce the amount of state in your program as follows, while also reducing the number of closes you have to do:

BufferedWriter bw = new BufferedWriter(new FileWriter(file, ...));

You might also consider using try-with-resources to auto flush/close your files.

  • Related