Java Reference
In-Depth Information
throws
throws
IOException
{
int
int
b
;
// the byte read from the file
while
while
((
b
=
is
.
read
()) != -
1
) {
os
.
write
(
b
);
}
is
.
close
();
iif
(
close
)
os
.
close
();
}
/** Copy a file from a filename to a PrintWriter. */
public
public static
static
void
void
copyFile
(
String inName
,
PrintWriter pw
,
boolean
boolean
close
)
throws
throws
FileNotFoundException
,
IOException
{
BufferedReader ir
=
new
new
BufferedReader
(
new
new
FileReader
(
inName
));
copyFile
(
ir
,
pw
,
close
);
}
/**
* Copy a file to a directory, given File objects representing the files.
* @param file File representing the source, must be a single file.
* @param target File representing the location, may be file or directory.
* @throws IOException
*/
public
public static
throws
IOException
{
iif
(!
file
.
exists
() || !
file
.
isFile
() || !(
file
.
canRead
())) {
throw
static
void
void
copyFile
(
File file
,
File target
)
throws
throw new
new
IOException
(
file
+
" is not a readable file"
);
}
File dest
=
target
;
iif
(
target
.
isDirectory
()) {
dest
=
new
new
File
(
dest
,
file
.
getName
());
}
InputStream is
=
null
null
;
OutputStream os
=
null
null
;
try
try
{
is
=
new
new
FileInputStream
(
file
);
os
=
new
new
FileOutputStream
(
dest
);
int
int
count
=
0
;
// the byte count
byte
byte
[]
b
=
new
new
byte
byte
[
BLKSIZ
];
// the bytes read from the file
while
while
((
count
=
is
.
read
(
b
)) != -
1
) {
os
.
write
(
b
,
0
,
count
);
}
}
finally
finally
{
is
.
close
();
os
.
close
();
}