我使用下面的程序复制一个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+"秒");
}
}
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+"秒");
}
}
javaIO设计就是采用的装饰器模式,可以选择包装好的类来完成各种操作,BufferedReader,BufferedWriter
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+"秒");
}
}
while ((c=in.read())!=-1){
out.wirte(c);
}
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("读写错误");
}
}
}//文件的读写操作的原理就是上面这样的 ,自己的代码要自己调试
//加油 嘿嘿
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();
}
}
}