怎样用流将一个文件夹中的图片复制到另一个文件夹中
解决方案 »
- 大家帮看看这个空指针错误的show.jsp代码,在我的数据库中存的Blob类型的图片,我想读取它并显示为图片,用以下代码
- Sql中Uid为char类型 值为aaa,Upassword为char类型 值为 aaa
- 一个奇怪的sql异常,大虾们都来帮我看看啊
- 谁能推荐几种好的基于JSP新闻系统、文章管理系统
- 关于EL表达式
- 为什么使用getRequestDispatcher转向新的地址后网页乱码?
- 性别验证
- 新装的myeclipse为什么无法查看javadoc文档?
- 刚学taglib搞了好半天了,问题依然,现在只有用最后一招了。谢谢!
- 几个servlet共享一些数据 如我从数据库里读出一些重要的设置存入vector 让几个servlet共享 怎么做到?先谢
- 从事j2ee多长时间了?
- IBM提出我们的系统有安全隐患,不知如何修改……
{
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) {
InputStream inStream = new FileInputStream(oldPath);
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println( "error ");
e.printStackTrace();
}
}
/**
* 复制单个文件
*
* @param oldPath
* String 原文件路径 如:c:/fqf.txt
* @param newPath
* String 复制后路径 如:f:/fqf.txt
* @return boolean
* @throws Exception
*/
public void copyFile(String oldPath, String newPath) throws IOException {
try {
String newPathFolder = newPath.substring(0, newPath.lastIndexOf(File.separator));
File folder = new File(newPathFolder);
if (!folder.exists()){
folder.mkdirs();
}
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { // 文件存在时
InputStream inStream = new FileInputStream(oldPath); // 读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; // 字节数 文件大小
fs.write(buffer, 0, byteread);
}
inStream.close();
fs.close();
}
} catch (IOException e) {
throw new IOException(e);
}}
import java.nio.*;
import java.nio.channels.*;public class FastCopyFile
{
static public void main( String args[] ) throws Exception {
if (args.length<2) {
System.err.println( "Usage: java FastCopyFile infile outfile" );
System.exit( 1 );
} String infile = args[0];
String outfile = args[1]; FileInputStream fin = new FileInputStream( infile );
FileOutputStream fout = new FileOutputStream( outfile ); FileChannel fcin = fin.getChannel();
FileChannel fcout = fout.getChannel(); ByteBuffer buffer = ByteBuffer.allocateDirect( 1024 ); while (true) {
buffer.clear(); int r = fcin.read( buffer ); if (r==-1) {
break;
} buffer.flip(); fcout.write( buffer );
}
}
}
来个nio的。