我想通过一个程序复制一个java文件,也就是将 A.java 复制到 B.java 中去,但是再复制的时候,出现了重复出现的情况,也就是,程序好像讲缓冲区前面的部分重新写了一遍大家说说应该怎么解决呢,嗯,上代码
//复制的代码,感觉在缓冲区的时候有点问题
class FileCopy
{
FileInputStream FIS;
FileOutputStream FOS;
public boolean copyFile(File src, File des) {
try {
FIS = new FileInputStream(src);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FOS = new FileOutputStream(des);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] bt = new byte[1024];
int readNum = 0;
try {
while( (readNum = FIS.read(bt)) != -1 ) {
FOS.write(bt, 0,bt.length);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FIS.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FOS.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
}
只选取了文件的结尾部分public class register_inquiry extends Domain
{
public register_inquiry()
{
TermVariable.initialize(2); constants = new String[3];
constants[0] = "in";
constants[1] = "information";
constants[2] = "tool"; compoundTasks = new String[1];
compoundTasks[0] = "register_inquiry"; primitiveTasks = new String[1];
primitiveTasks[0] = "!register"; methods = new Method[1][]; methods[0] = new Method[1];
methods[0][0] = new Method0();
ops = new Operator[1][]; ops[0] = new Operator[1];
ops[0][0] = new Operator0(); axioms = new Axiom[3][]; axioms[0] = new Axiom[0]; axioms[1] = new Axiom[0]; axioms[2] = new Axiom[0]; }
}
只选取了文件的结尾部分public class register_inquiry extends Domain
{
public register_inquiry()
{
TermVariable.initialize(2); constants = new String[3];
constants[0] = "in";
constants[1] = "information";
constants[2] = "tool"; compoundTasks = new String[1];
compoundTasks[0] = "register_inquiry"; primitiveTasks = new String[1];
primitiveTasks[0] = "!register"; methods = new Method[1][]; methods[0] = new Method[1];
methods[0][0] = new Method0();
ops = new Operator[1][]; ops[0] = new Operator[1];
ops[0][0] = new Operator0(); axioms = new Axiom[3][]; axioms[0] = new Axiom[0]; axioms[1] = new Axiom[0]; axioms[2] = new Axiom[0]; }
}
//重复出现的部分
switch (which)
{
case 0:
p = (new Precondition0(unifier)).setComparator(null);
break;
default:
return null;
} p.reset(); return p;
} public String getLabel(int which)
{
switch (which)
{
case 0: return "Method0Branch0";
default: return null;
}
}
}public class reg
//重复出现的情况结束
//复制的代码,感觉在缓冲区的时候有点问题
class FileCopy
{
FileInputStream FIS;
FileOutputStream FOS;
public boolean copyFile(File src, File des) {
try {
FIS = new FileInputStream(src);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FOS = new FileOutputStream(des);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] bt = new byte[1024];
int readNum = 0;
try {
while( (readNum = FIS.read(bt)) != -1 ) {
FOS.write(bt, 0,bt.length);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FIS.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FOS.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
}
只选取了文件的结尾部分public class register_inquiry extends Domain
{
public register_inquiry()
{
TermVariable.initialize(2); constants = new String[3];
constants[0] = "in";
constants[1] = "information";
constants[2] = "tool"; compoundTasks = new String[1];
compoundTasks[0] = "register_inquiry"; primitiveTasks = new String[1];
primitiveTasks[0] = "!register"; methods = new Method[1][]; methods[0] = new Method[1];
methods[0][0] = new Method0();
ops = new Operator[1][]; ops[0] = new Operator[1];
ops[0][0] = new Operator0(); axioms = new Axiom[3][]; axioms[0] = new Axiom[0]; axioms[1] = new Axiom[0]; axioms[2] = new Axiom[0]; }
}
只选取了文件的结尾部分public class register_inquiry extends Domain
{
public register_inquiry()
{
TermVariable.initialize(2); constants = new String[3];
constants[0] = "in";
constants[1] = "information";
constants[2] = "tool"; compoundTasks = new String[1];
compoundTasks[0] = "register_inquiry"; primitiveTasks = new String[1];
primitiveTasks[0] = "!register"; methods = new Method[1][]; methods[0] = new Method[1];
methods[0][0] = new Method0();
ops = new Operator[1][]; ops[0] = new Operator[1];
ops[0][0] = new Operator0(); axioms = new Axiom[3][]; axioms[0] = new Axiom[0]; axioms[1] = new Axiom[0]; axioms[2] = new Axiom[0]; }
}
//重复出现的部分
switch (which)
{
case 0:
p = (new Precondition0(unifier)).setComparator(null);
break;
default:
return null;
} p.reset(); return p;
} public String getLabel(int which)
{
switch (which)
{
case 0: return "Method0Branch0";
default: return null;
}
}
}public class reg
//重复出现的情况结束
{
FileInputStream FIS;
FileOutputStream FOS;
public boolean copyFile(File src, File des) {
try {
FIS = new FileInputStream(src);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FOS = new FileOutputStream(des);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] bt = new byte[1024];
int readNum = 0;
try {
while( (readNum = FIS.read(bt)) != -1 ) {
FOS.write(bt, 0,bt.length);
bt = new byte[1024];
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FIS.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FOS.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
}
这样比较好package File.IO;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;public class FileCopy
{
FileInputStream FIS;
FileOutputStream FOS;
public boolean copyFile(File src, File des) {
try {
FIS = new FileInputStream(src);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FOS = new FileOutputStream(des);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// byte[] bt = new byte[1024];
ByteBuffer buffer = ByteBuffer.allocate(1024);
FileChannel in = FIS.getChannel(),
out = FOS.getChannel();
int readNum = 0;
try {
while( (readNum = in.read(buffer)) != -1 ) {
buffer.flip();
out.write(buffer);
buffer.clear();
// FOS.write(bt, 0,bt.length);
// bt = new byte[1024];
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
public static void main(String []args){
File file1 = new File("demo.txt");
File file2 = new File("demodemo.txt");
System.out.println(new FileCopy().copyFile(file1,file2));
}
}
直接连接2个FileChannel 通道
public class FileCopy
{
FileInputStream FIS;
FileOutputStream FOS;
public boolean copyFile(File src, File des) {
try {
FIS = new FileInputStream(src);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FOS = new FileOutputStream(des);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// byte[] bt = new byte[1024];
ByteBuffer buffer = ByteBuffer.allocate(1024);
FileChannel in = FIS.getChannel(),
out = FOS.getChannel();
try {
in.transferTo(0, in.size(), out);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// int readNum = 0;
// try {
// while( (readNum = in.read(buffer)) != -1 ) {
// buffer.flip();
// out.write(buffer);
// buffer.clear();
//// FOS.write(bt, 0,bt.length);
//// bt = new byte[1024];
// }
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
public static void main(String []args){
File file1 = new File("demo.txt");
File file2 = new File("demodemo.txt");
System.out.println(new FileCopy().copyFile(file1,file2));
}
}
public boolean copyFile(File src, File des) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(src);
fos = new FileOutputStream(des); byte[] bt = new byte[1024];
int readNum = 0;
while ((readNum = fis.read(bt)) != -1) {
fos.write(bt, 0, readNum);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
if(fis!=null)
fis.close();
if(fos!=null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}