为什么无法正确复制文件?

gearss00 2012-04-14 07:23:17
我使用下面的程序复制一个txt文件C:/1.txt,里面的内容只是一个数字1,复制到文件2.txt后,在2.txt里,1后面有很多空格,即下面程序无法正确复制一个txt文件,这是我从Java范例大全里面抄写出来的程序段,我总是检查不出哪里错了,请帮忙。


import java.io.*;
import java.io.File;//引入类
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy {
public void abcd () {
File fromFile = new File ("C:/1.txt");
File toFile = new File ("C:/2.txt");
copyFile(fromFile, toFile);
}

// 复制一个文件
public void copyFile(File fromFile, File toFile) {// 复制文件
createFile(toFile, true);// 创建文件
System.out.println("复制文件" + fromFile.getAbsolutePath() + "到" + toFile.getAbsolutePath());
try {
FileOutputStream fop = new FileOutputStream(toFile); // 创建FileOutputStream对象
FileInputStream in = new FileInputStream(fromFile);
int tempbyte;
byte[] buffer = new byte[1024];
while (in.read(buffer) != -1) { // 循环读取文件
fop.write(buffer); // 通过流写数据
}
fop.close(); // 关闭输出流
in.close(); // 关闭输入流
} catch (IOException e) {
e.printStackTrace();
}
}

public void createFile(File file, boolean isFile) {// 创建文件
if (!file.exists()) {// 如果文件不存在
if (!file.getParentFile().exists()) {// 如果文件父目录不存在
createFile(file.getParentFile(), false);
} else {// 存在文件父目录
if (isFile) {// 创建文件
try {
file.createNewFile();// 创建新文件
} catch (IOException e) {
e.printStackTrace();
}
} else {
file.mkdirs();// 创建目录
}
}
}
}

public static void main(String[] args) {
long sttime = System.nanoTime();
Copy sourceToDestiny = new Copy();
sourceToDestiny.abcd();
long endtime = System.nanoTime();
System.out.println("Totaol time is: "+(endtime-sttime)/1000000000+"秒");
}
}
...全文
192 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zkbao 2012-04-16
  • 打赏
  • 举报
回复
前面说的是用了字符流,一次只能读一个字符,因此不能用于文件中出现中文的情况,如果要读写中文,用字节流FileInputStream和FileOutputStream的read()和write()方法,一次读写一个字节
zkbao 2012-04-16
  • 打赏
  • 举报
回复
直接用FileReader和FileWriter就行了,

关键部分的while循环这样写就OK了

如:int c=0;
while ((c=in.read())!=-1){
out.wirte(c);
}
zkbao 2012-04-16
  • 打赏
  • 举报
回复
测试了一下,以下用FileReader能读到中文:

import java.io.*;
public class TestFileReader{
public static void main(String[] args){
FileReader fr=null;
try{
fr = new FileReader("d:/java/testData/1.txt");
}catch(FileNotFoundException e){
e.printStackTrace();
}
int c=0;
try{
while((c=fr.read())!=-1){
System.out.print((char)c);
}

}catch(IOException e){
e.printStackTrace();
}

}
}
dalancon 2012-04-16
  • 打赏
  • 举报
回复
import java.io.*;
public class TestFileInputStream {
public static void main(String[] args) throws IOException {
int b=0;
int num=0;
FileReader f = null;
FileWriter o = null;
try {
f = new FileReader("D:\\JavaWorkspace\\javaio\\src\\TestFileInputStream.java");
o = new FileWriter("D:\\JavaWorkspace\\javaio\\TestFileOutputStream.java");
} catch (FileNotFoundException e) {
System.out.println("系统错误");
System.exit(-1);
}
try{
while((b=f.read())!=-1){
num++;
o.write(b);
}
System.out.println("总计"+num+"个字节");
f.close();
o.close();
}catch(IOException e){
System.out.println("读写错误");
}

}


}

//文件的读写操作的原理就是上面这样的 ,自己的代码要自己调试
//加油 嘿嘿
zkbao 2012-04-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

前面说的是用了字符流,一次只能读一个字符,因此不能用于文件中出现中文的情况,如果要读写中文,用字节流FileInputStream和FileOutputStream的read()和write()方法,一次读写一个字节
[/Quote]

晕,我说错了,一个中文2个字节,不好意思,愚笨呀自己
周靖峰 2012-04-14
  • 打赏
  • 举报
回复
需要把已读的字符数也记下来,然后用write(byte[] b, int off, int len)这个函数


import java.io.*;

public class Copy {
public void abcd () {
File fromFile = new File ("c:\\1.txt");
File toFile = new File ("c:\\2.txt");
copyFile(fromFile, toFile);
}

// 复制一个文件
public void copyFile(File fromFile, File toFile) {// 复制文件
createFile(toFile, true);// 创建文件
System.out.println("复制文件" + fromFile.getAbsolutePath() + "到" + toFile.getAbsolutePath());
try {
FileOutputStream fop = new FileOutputStream(toFile); // 创建FileOutputStream对象
FileInputStream in = new FileInputStream(fromFile);
int tempbyte;
byte[] buffer = new byte[1024];
int readNum; //这里添加一个变量用于记录已读的字符数
while ((readNum = in.read(buffer)) != -1) { // 循环读取文件
fop.write(buffer, 0, readNum); // 通过流写数据
}
fop.close(); // 关闭输出流
in.close(); // 关闭输入流
} catch (IOException e) {
e.printStackTrace();
}
}

public void createFile(File file, boolean isFile) {// 创建文件
if (!file.exists()) {// 如果文件不存在
if (!file.getParentFile().exists()) {// 如果文件父目录不存在
createFile(file.getParentFile(), false);
} else {// 存在文件父目录
if (isFile) {// 创建文件
try {
file.createNewFile();// 创建新文件
} catch (IOException e) {
e.printStackTrace();
}
} else {
file.mkdirs();// 创建目录
}
}
}
}

public static void main(String[] args) {
long sttime = System.nanoTime();
Copy sourceToDestiny = new Copy();
sourceToDestiny.abcd();
long endtime = System.nanoTime();
System.out.println("Totaol time is: "+(endtime-sttime)/1000000000+"秒");
}
}
zwnylsf 2012-04-14
  • 打赏
  • 举报
回复
二楼说的没错,要精确复制就得到用到重载的方法,当然为何不用包装流来做呢,
javaIO设计就是采用的装饰器模式,可以选择包装好的类来完成各种操作,BufferedReader,BufferedWriter
结贴是美德 2012-04-14
  • 打赏
  • 举报
回复
fop.write(buffer);把整个buffer都写进去了,但是其实buffer除了最前面放了个数字1,后面全是0,lz请用这个重载方法:void write(byte[] b, int off, int len)

62,615

社区成员

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

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