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
}