读书人

如何使用FileOutputStream在指定位置写

发布时间: 2013-06-26 14:29:32 作者: rapoo

怎么使用FileOutputStream在指定位置写入文件?
本人java新手,请大神指导。。。。 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();
}
}

}

一个简单的拷贝,
[解决办法]
while((byteread = in.read(buffer)) != -1){

bytesum += byteread;
System.out.println(bytesum);
out.write(buffer, 0, byteread);
}
这个读写不都在吗?你要读,还是写 在哪个字节,buffer这个变量,自己定就是了,
[解决办法]
写1024个空格,然后写其他内容

读书人网 >J2EE开发

热点推荐