Java – resource leakage, input is never closed, how to solve it?
Generally, you need to write *.close() when the stream is used up, but System.in is the input stream of the console and does not need to be closed. After closing, an error will be reported when used elsewhere.
java socket Input/OutputStream closing problem
I don’t know why you turn off the stream back and also turn off the socket, but since you need to communicate and write back, why should you turn off the stream? Generally, one socket is used and one stream is used. Once all communication operations are completed, it is enough to close both the socket and the stream. You said that if you disable it, it will block. Have you used multi-threading? For network communication and other tasks such as client/server, multi-threading is generally used. That is, one thread has one task, and the blocking of one thread does not affect other threads. continue working.
How to close the window in java
1 package applicationGraphicsInOut; 2 import java.awt.*; 3 import java.awt.event.*; 4 public class ApplicationGraphicsInOut { 5 public static void main(String args[]) 6 { 7 new FrameInOut(); 8 } 9 10 }11 class FrameInOut extends Frame implements ActionListener12 {13 Label prompt;14 TextField input,output;15 FrameInOut()16 {17 super(“Graphic Interface Java Application program”);18 prompt=new Label(“Please enter your name”);19 input=new TextField(6);20 output=new TextField(20);21 setLayout(new FlowLayout()); 22 add(prompt);23 add(input);24 add(output);25 input.addActionListener(this);26 setSize(300,200);27 setVisible(true);28 }29 public void actionPerformed(ActionEvent e)30 { 31 output.setText(input.getText()+”,Welcome”); 32 } 33 } The program runs successfully under Eclipse, but it cannot be closed! What’s the reason? There are several solutions: 1: Change the main function to the following 1 public static void main(String args[]) 2 { 3 Frame fr=new FrameInOut(); 4 fr.addWindowListener(new java.awt.event. WindowAdapter() 5 { 6 public void windowClosing(java.awt.event.WindowEvent e) 7 { 8 System.exit(0); 9 }10 });11 12 }I am a little confused about this method, addWindowListener() brackets What’s going on here, I’m a little confused, I haven’t seen such a form, but I guess it may be an abstract method windowClosing() in the java.awt.event.WindowAdapter() class, but I really haven’t seen it. Please explain this format. 2: Respond to the WINDOWS_CLOSING event. Each window has 3 control icons. The minimizing and maximizing operations of the Frame can be completed automatically. However, the operation of closing the window cannot be achieved by clicking the close icon. The program needs to specifically write the relevant information. Code, in fact, this method is the same as the previous one, but it has a different format, a format that is more understandable for beginners.
Add a listening function in the FrameInOut() constructor. After adding the listening function, the constructor is as follows: FrameInOut() { super(“Java Application program with graphical interface”); prompt=new Label(“Please enter Your name”); input=new TextField(6); output=new TextField(20); setLayout(new FlowLayout()); addWindowListener(new HandleWin()); //Add a listening function and trigger the WindowEvent event add(prompt ); add(input); add(output); input.addActionListener(this); setSize(300,200); setVisible(true); } Among them, HandleWin() is an internal class, which mainly implements the WindowListener interface. After adding the listener, the WindowEvent class will be triggered Represents all seven events, the details are as follows: (1) WINDOW_ACTIVATED: represents the window being activated (standby at the front of the screen).
(2) WINDOW_DEACTIVATED: represents the window deactivation (the original active window is deactivated after other windows are activated). (3) WINDOW_OPENED: Indicates that the window is opened. (4) WINDOW_CLOSED: Indicates that the window is closed (occurs after closing the window). (5) WINDOW_CLOSING: Indicates that the window is being closed (referring to before closing.
For example, when clicking the close button on the window title bar). (6) WINDOW_ICONIFIED: means minimizing the window into an icon. (7) WINDOW_DEICONIFIED: The main methods in the WindowEvent class that represent restoring the window from the icon are: public window getWindow(); this method returns the specific window object that triggered the current WindowEvent event, and the getSource() method returns the same event reference.
HandleWin is defined as follows: class HandleWin extends WindowAdapter { public void windowClosing(WindowEvent e) { (e.getWindow()).dispose(); System.exit(0); } }HandleWin is a window event A subclass of the cropping class WindowAdapter, which overloads the WindowClosing() method.
How to handle the close of java’s InputStream
Debug: 3 to 4, when the code is executed to 3, Figure 1 notices the fd of in and the in of buffered, indicating that buffered and in Both are open, and then execute 3. When reaching 4, Figure 2 notices that the fd of in is larger than the in of buffered, indicating that both buffered and in are closed, so in.close is redundant. If you do not execute 3, execute 4. and 5. When executing to 4, it is consistent with Figure 1. When executing to 5, fd is -1 and buffered has changed, so at this timeIn is closed, but buffered is not closed, and then it is Figure 2 again. So, we just need to close the buffer stream and it will be ok. Unless you are a suspicious person and are seriously dissatisfied with the jvm, or you like it very much. Typing does not care about the execution efficiency of the code.
Java closes the input stream close() problem
Sca