随笔-204  评论-90  文章-8  trackbacks-0
1.使用FileInputStream和FileOutputStream,代码如下:sFlieName和tFileName分别为源文件(带路径)和目标文件(带路径)
try {
    
// Save as File
    FileInputStream input = new FileInputStream(new File(
            sFileName));
    FileOutputStream output 
= new FileOutputStream(
            
new File(tFileName));
    
int b;
    
while (true{
        
if (input.available() < 1024{
            
while ((b = input.read()) != -1{
                output.write(b);
            }

            
break;
        }
 else {
            input.read(buffer);
            output.write(buffer);
        }

    }

    input.close();
    output.close();
    
continue;
}
 catch (FileNotFoundException e) {
    e.printStackTrace();
}
 catch (IOException e) {
    e.printStackTrace();
}
2.使用FileChennal,本人认为这种方法在文件移动,不做别的处理时很好用,又好看,哈哈,特别是它的transferTo(),很棒啊,想了解看文档啊,sFileName和tFileName同上
try {
    
// Save as File
    FileInputStream input = new FileInputStream(sFileName);                    
    FileOutputStream output 
= new FileOutputStream(tFileName);
    FileChannel infileChannel 
= input.getChannel();   
    FileChannel outfileChannel 
= output.getChannel();   
    
long size = infileChannel.size();   
    infileChannel.transferTo(
0, size, (WritableByteChannel)outfileChannel);    
    
continue;
}
 catch (FileNotFoundException e) {
    e.printStackTrace();
}
 catch (IOException e) {
    e.printStackTrace();
}

posted on 2006-07-05 16:21 一凡 阅读(5121) 评论(0)  编辑  收藏 所属分类: JAVA 基础

只有注册用户登录后才能发表评论。


网站导航: