Java file read/write input/output stream shutdown problem, Java garbage collection

Source: Internet
Author: User

Remember: Read and write files must be in time to close the input and output stream


Project, the use of the RTF document is modified after uploading an update marked 0 is not updated, another program uploads the document to the LED screen, the LED program detects that the update is marked as not updated when the status is dropped on the LED, and the status update to 1 has been updated.

Phenomenon: the input and output stream is used when uploading a file, the LED screen does not display the update document after uploading successfully, is empty.

Part of the code:

public void Save () {

Upload ();

Save (XXXXX);

}

public void Upload () throws exception{
Create an upload file output stream with the server's file save address and source file name
Xczl.setpath (Getsavepath () + "\ \" +getuploadfilename ());
FileOutputStream fos = new FileOutputStream (Getsavepath () + "\ \" +getuploadfilename ());
Create a file upload stream as an upload file
FileInputStream fis = new FileInputStream (upload);
Writes the contents of the uploaded file to the server
byte[] buffer = new byte[1024*1024];
int len = 0;
while (len = fis.read (buffer)) >0)
{
Fos.write (Buffer,0,len);
}

}

Did you find any specific reasons for this?

Cause: If you upload a file using the above code, open the uploaded file to indicate that the file is being consumed by another application. But after a while, you can open it.


Remember to close the input and output stream when the file is read and write, and if the input and output stream is not closed, the file is being consumed by other applications. But why, for a while,

can also open the file, is the Java automatic garbage collection mechanism problem, long time without the input and output stream of the file will be reclaimed by default. The uploaded file has the same name as the file, so it is overwritten.

In the process of writing the file, the LED program has updated its status to 1, but the input and output stream is not closed, causing the LED program to not access the upload file. Hehe, we should know how to solve the problem.


Workaround Code:


public void Upload () throws exception{
Create an upload file output stream with the server's file save address and source file name
Xczl.setpath (Getsavepath () + "\ \" +getuploadfilename ());
FileOutputStream fos = new FileOutputStream (Getsavepath () + "\ \" +getuploadfilename ());
Create a file upload stream as an upload file
FileInputStream fis = new FileInputStream (upload);
Writes the contents of the uploaded file to the server
byte[] buffer = new byte[1024*1024];
int len = 0;
while (len = fis.read (buffer)) >0)
{
Fos.write (Buffer,0,len);
}
Fos.close ();
Fis.close ();//Key code
}

















Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.