粗糙版
package 學習資料.IO流;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class _5_1_文件拷貝 {
public static void main(String[] args) {
byte[] path = FiletoByte("dext.txt");
BytetoFile("a.txt",path);
}
public static byte[] FiletoByte(String filepath) {
File src = new File(filepath);
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
is = new FileInputStream(src);
baos = new ByteArrayOutputStream();
byte[] flush = new byte[1024*10];
int len = -1;
while((len = is.read(flush)) != -1)
baos.write(flush,0,len);
baos.flush();
return baos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(is != null) is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static void BytetoFile(String filePath,byte[] src) {
File dest = new File(filePath);
InputStream is = null;
OutputStream os = null;
try {
is = new ByteArrayInputStream(src);
os = new FileOutputStream(dest);
byte[] flush = new byte[5];
int len = -1;
while((len = is.read(flush)) != -1)
os.write(flush,0,len);
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if( os != null) os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
改進版
package 學習資料.IO流;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class _5_1_FileUtiles_文件拷貝 {
public static void main(String[] args) {
try {
InputStream iStream = new FileInputStream("a.txt");
OutputStream oStream = new FileOutputStream("a_copy.txt");
BytetoFile(iStream,oStream);
} catch (IOException e) {
e.printStackTrace();
}
byte[] information = null;
try {
InputStream iStream = new FileInputStream("a_copy.png");
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
BytetoFile(iStream,oStream);
information = oStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
try {
InputStream iStream = new ByteArrayInputStream(information);
OutputStream oStream = new FileOutputStream("copy.png");
BytetoFile(iStream,oStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void BytetoFile(InputStream is,OutputStream os) {
try(InputStream is1 =
new BufferedInputStream(is);
OutputStream os1 =
new BufferedOutputStream(os)) {
byte[] flush = new byte[1024];
int len = -1;
while((len = is1.read(flush)) != -1)
os1.write(flush,0,len);
os1.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void close(InputStream is,OutputStream os) {
try {
if( os != null) os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(is !=null)is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void close(Closeable...ios) {
for(Closeable io:ios) {
try {
if( io != null) io.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}