You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hudi.apache.org by "c-f-cooper (via GitHub)" <gi...@apache.org> on 2023/04/30 05:15:28 UTC

[GitHub] [hudi] c-f-cooper commented on pull request #8596: [BUG-FIX] use try with resource to close stream

c-f-cooper commented on PR #8596:
URL: https://github.com/apache/hudi/pull/8596#issuecomment-1528941416

   > Hi, can you elaborate a little more what would happen if the inputstream is not closed properly? Can you write a test case to demonstrate the resolution of the issue.
   
   To demonstrate the issue of unclosed IO streams, I can write the following test program:
   `import java.io.*;
   
   public class UnclosedIOTest {
       public static void main(String[] args) throws IOException {
           BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
           BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
           
           System.out.print("Please enter a line of text:");
           String line = reader.readLine();
           writer.write(line);
           System.out.println("Written to output.txt");
       }
   }
   `
   This program uses an unclosed BufferedReader and an unclosed BufferedWriter. When the program runs, the user will be prompted to enter a line of text, and the program will write this line of text to a file called output.txt. However, because the BufferedReader and BufferedWriter are not closed, it can lead to the following issues:
   
   Resource leakage: When the program is run repeatedly, a new BufferedReader and BufferedWriter will be created each time, but the old IO streams are not closed, and they will continue to occupy system resources, which may eventually cause the system or program to crash.
   Data loss: If an exception occurs while writing data and the BufferedWriter is not closed, the written data may be lost because they have not been flushed to the disk yet.
   To solve these issues, you should add the following code at the end of the program to close the IO streams:
   
   `reader.close();
   writer.close();
   `
   This will ensure that the program correctly releases the IO resources.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@hudi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org