文件拷贝,重现重复内容怎么办

Eniak 2010-02-22 10:37:56
我想通过一个程序复制一个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
//重复出现的情况结束
...全文
285 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
BearKin 2010-02-22
  • 打赏
  • 举报
回复
LZ可换字符流来读
恩 内容太短了
ScAREcrOw_ss 2010-02-22
  • 打赏
  • 举报
回复

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;
}
zhaining522 2010-02-22
  • 打赏
  • 举报
回复
不过 一个理想方式是
直接连接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));
}
}
zhaining522 2010-02-22
  • 打赏
  • 举报
回复
或者 用 FileChannel 加上 字节缓存区 ByteBuffer
这样比较好



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));
}
}



zhaining522 2010-02-22
  • 打赏
  • 举报
回复


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);
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;
}
}


BearKin 2010-02-22
  • 打赏
  • 举报
回复
引用 7 楼 zhaining522 的回复:
在问问提就另开个贴
这个贴 可以结贴了

引用 6 楼 eniak 的回复:谢谢各位,再问个问题,如果我想在一个特定的位置加入一个字符串呢,比如 在文件出现 "abcde" 的时候加入这个字符,但是不知道何时出现,如果出出现了,就在 "abcde" 的前面加上 "AAA_" 后面加上 "_BBB", 最后的结果就是 “AAA_abcde_BBB”, 文件只是这么改动一下,AAA_abcde_BBB” 后面的部分继续复制 这个应该则么改呢?


用字符流 先将所有内容读进来 然后查找需要更换的地方 替换完毕后再写入

分给我吧 表给LS的
zhaining522 2010-02-22
  • 打赏
  • 举报
回复
在问问提就另开个贴
这个贴 可以结贴了

引用 6 楼 eniak 的回复:
谢谢各位,再问个问题,如果我想在一个特定的位置加入一个字符串呢,比如

在文件出现 "abcde" 的时候加入这个字符,但是不知道何时出现,如果出出现了,就在 "abcde" 的前面加上 "AAA_" 后面加上 "_BBB", 最后的结果就是 “AAA_abcde_BBB”, 文件只是这么改动一下,AAA_abcde_BBB” 后面的部分继续复制

这个应该则么改呢?
Eniak 2010-02-22
  • 打赏
  • 举报
回复
谢谢各位,再问个问题,如果我想在一个特定的位置加入一个字符串呢,比如

在文件出现 "abcde" 的时候加入这个字符,但是不知道何时出现,如果出出现了,就在 "abcde" 的前面加上 "AAA_" 后面加上 "_BBB", 最后的结果就是 “AAA_abcde_BBB”, 文件只是这么改动一下,AAA_abcde_BBB” 后面的部分继续复制

这个应该则么改呢?

62,624

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧