首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

如何使用FileOutputStream在指定位置写入文件

2013-06-26 
怎么使用FileOutputStream在指定位置写入文件?本人java新手,请大神指导。。。。FileOutputStream指定位置[解决

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

热点排行