当前位置: 代码迷 >> Java相关 >> 文件拷贝问题(代码)
  详细解决方案

文件拷贝问题(代码)

热度:167   发布时间:2007-06-30 14:52:00.0
文件拷贝问题(代码)
//利用FileInputStream和FileOutputSteam编写一个文件拷贝的程序 运行后结果不是我想要的结果!
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.*;
import java.nio.channels.FileChannel;
class Copy
{
String filename = null;
String path = "456";
File file =null;
String objFilename = null;
File objfile = null;
FileOutputStream output = null;
FileInputStream input = null;
FileChannel inputchannel = null;
FileChannel outputchannel = null;
File pathfile=null;
File yuanfile = null;
File bojectfile = null;
Copy(String yuan,String obj)
{
pathfile = new File(path);
yuanfile = new File(yuan);
bojectfile = new File(obj);
filename = yuan;
objFilename = obj;
file = new File(path,filename);
objfile = new File(path,objFilename);
judge(pathfile,file);
judge(pathfile,objfile);
read();
write();
}
public void judge(File pathfile,File file) //判确定文件存在
{
if(pathfile.isDirectory()==false)
{
pathfile.mkdirs();
}
if(file.isFile()==false)
{
try
{
file.createNewFile();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public void read() //读文件
{
try
{
input = new FileInputStream(file);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
ByteBuffer buf = ByteBuffer.allocate(200);
inputchannel = input.getChannel();
try
{
inputchannel.read(buf);
}
catch (IOException e)
{
System.out.println(e.toString());
}
buf.flip();
System.out.println("文件内容:");
//System.out.println(buf.get());
try //关闭流
{
input.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void write() //写文件
{
try
{
output = new FileOutputStream(objfile);
}
catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
ByteBuffer buf = ByteBuffer.allocate(200);
outputchannel = output.getChannel();
try
{
inputchannel.write(buf);
}
catch (IOException e)
{
System.out.println(e.toString());
}
buf.flip();
System.out.println("文件写入完成");
try
{
output.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public String toString()
{
return "完成";
}
public static void main(String args[]) //主方法
{
String yuan="123.txt";
String obj="456.txt";
Copy copy = new Copy(yuan,obj);
//System.out.println(copy.toString());
}
}
搜索更多相关的解决方案: 文件  代码  拷贝  

----------------解决方案--------------------------------------------------------
NIO我不会,IO我可以指点一下
----------------解决方案--------------------------------------------------------
你想要什么结果?
----------------解决方案--------------------------------------------------------

呵呵。你没有实现复制,只是简单的写入了0而已。

public void write() //写文件
{
try
{
output = new FileOutputStream(objfile);
}
catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
ByteBuffer buf = ByteBuffer.allocate(200);
outputchannel = output.getChannel();
try
{
inputchannel.write(buf);//应该是outputchannel 不然的话,会认为通道已经关了。。再说你的缓冲器里没有任何东西。所以写进去全是0,你要想把内容写如到缓冲器中,然后在用通道把内容写到456这个文件里。。
}
catch (IOException e)
{
System.out.println(e.toString());
}
buf.flip();
System.out.println("文件写入完成");
try
{
output.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}


----------------解决方案--------------------------------------------------------
老大,没必要那么罗嗦的

[CODE]
/*
* JCopy.java
*
* Created on 2007年6月30日, 下午4:16
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package jcopy;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
*
* @author vlinux
*/
public class JCopy {

private FileChannel sourceFileChannel;
private FileChannel destFileChannel;
private long bufferSize;

public JCopy( File sourceFile, File destFile, long bufferSize ) throws FileNotFoundException, IOException {
try{
sourceFileChannel = new java.io.FileInputStream( sourceFile ).getChannel();
destFileChannel = new java.io.FileOutputStream( destFile ).getChannel();
this.bufferSize = bufferSize;
for( long index=0; bufferSize == sourceFileChannel.transferTo(index,bufferSize,destFileChannel); index+=bufferSize );
} finally {
try{ sourceFileChannel.close(); } catch( Exception ex ) {}
try{ destFileChannel.close(); } catch( Exception ex ) {}
}
}


public static void main( String... args ) throws FileNotFoundException, IOException {
File sf = new File("xxx.rmvb");
File df = new File("唐伯虎点秋香.rmvb");
new JCopy( sf, df, 1024*1024 );
}

}


[/CODE]
----------------解决方案--------------------------------------------------------
兄弟们辛苦了!
----------------解决方案--------------------------------------------------------
  相关解决方案