BlogJava 联系 聚合 管理  

Blog Stats

随笔档案

文章档案

Infernu的Google site


Infernus-JXH

建立一般的流来拷贝文件:
public static void main(String[] args) throws Exception {
        FileInputStream in 
= new FileInputStream("c:\\PC.xls");
        FileOutputStream out 
= new FileOutputStream("d:\\PC1.xls");
                
        
int b;
        
while((b = in.read()) != -1{//使用b读取输入流,在不为空的情况下使用输出流写入
            out.write(b);
        }

        
        in.close();
//最后请注意关闭不使用的流
        out.close();
    }
带缓冲的流:(更有效率.)
public static void main(String[] args) throws Exception {
        FileInputStream in 
= new FileInputStream("c:\\card20.pdf");
        FileOutputStream out 
= new FileOutputStream("d:\\abc.pdf");
        FileChannel fc1 
= in.getChannel();//得到流
        FileChannel fc2 = out.getChannel();
        
        ByteBuffer buffer 
= ByteBuffer.allocate(512);//以块为单位操作
        
        
while(fc1.read(buffer) != -1{
            buffer.rewind();
            fc2.write(buffer);
            buffer.clear();
        }

        
        in.close();
        out.close();
    }
posted on 2009-10-29 22:23 Infernus 阅读(204) 评论(0)  编辑  收藏

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


网站导航: