怎么使用FileOutputStream在指定位置写入文件

如题所述

* 拷贝文件,
* @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();
}
}

}
温馨提示:答案为网友推荐,仅供参考
相似回答