晕死
我敲了
第三题有点问题!
//利用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());
}
}