日期:2014-05-18  浏览次数:21003 次

怎么使用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个空格,然后写其他内容