-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathFileUtil.java
More file actions
75 lines (71 loc) · 1.54 KB
/
Copy pathFileUtil.java
File metadata and controls
75 lines (71 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package common;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
public class FileUtil
{
public static void writeToFile( File file, InputStream source,
boolean close )
{
BufferedInputStream bis = null;
BufferedOutputStream fouts = null;
try
{
bis = new BufferedInputStream( source );
if ( !file.exists( ) )
{
if ( !file.getParentFile( ).exists( ) )
{
file.getParentFile( ).mkdirs( );
}
file.createNewFile( );
}
fouts = new BufferedOutputStream( new FileOutputStream( file ) );
byte b[] = new byte[1024];
int i = 0;
while ( ( i = bis.read( b ) ) != -1 )
{
fouts.write( b, 0, i );
}
fouts.flush( );
fouts.close( );
if ( close )
bis.close( );
}
catch ( IOException e )
{
Logger.getLogger( FileUtil.class.getName( ) ).log( Level.WARNING,
"Write file failed.", //$NON-NLS-1$
e );
try
{
if ( fouts != null )
fouts.close( );
}
catch ( IOException f )
{
Logger.getLogger( FileUtil.class.getName( ) )
.log( Level.WARNING, "Close output stream failed.", f ); //$NON-NLS-1$
}
if ( close )
{
try
{
if ( bis != null )
bis.close( );
}
catch ( IOException f )
{
Logger.getLogger( FileUtil.class.getName( ) )
.log( Level.WARNING, "Close input stream failed.", //$NON-NLS-1$
f );
}
}
}
}
}