怎么使用FileOutputStream在指定位置写入文件
发布网友
发布时间:2022-04-25 04:46
我来回答
共1个回答
热心网友
时间:2023-08-03 01:37
* 拷贝文件,
* @param oldPath 旧文件路径
* @param newPath 新文件路径
* @throws Exception
*/
private void copyFile(String oldPath, String newPath) throws Exception{
int bytesum = 0;
int byteread = 0;
File oldFile = new File(oldPath);
InputStream in = null;
OutputStream out = null;
if(oldFile.exists()){
try {
in = new FileInputStream(oldPath);
out = new FileOutputStream(newPath);
byte[] buffer = new byte[1024*5];
while((byteread = in.read(buffer)) != -1){
bytesum += byteread;
System.out.println(bytesum);
out.write(buffer, 0, byteread);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception("输入myeclipse的路径不正确");
}finally{
if(in != null)
in.close();
if(out != null)
out.close();
}
}
}