第一个例子 文件的复制import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class Test1 { public static void main(String[] args) {
try {
FileReader a = new FileReader("F:\\234.txt");
FileWriter b = new FileWriter("D:\\565.txt");
int i = a.read();
while(i!=-1){
b.write(i);
i = a.read();
}
b.close();
a.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
第二个例子 也是文件的复制
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class Test1 { public static void main(String[] args) {
try {
FileReader a = new FileReader("F:\\234.txt");
BufferedReader b = new BufferedReader(a);
FileWriter c = new FileWriter("D:\\5666.txt");
BufferedWriter d = new BufferedWriter(c);
String i = b.readLine();
while(i!=null){
d.write(i);
d.newLine();
i=b.readLine();
}
d.close();
c.close();
b.close();
a.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}现在搞不懂一个问题,这2中用法有什么区别 那种方法更好一些??
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class Test1 { public static void main(String[] args) {
try {
FileReader a = new FileReader("F:\\234.txt");
FileWriter b = new FileWriter("D:\\565.txt");
int i = a.read();
while(i!=-1){
b.write(i);
i = a.read();
}
b.close();
a.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
第二个例子 也是文件的复制
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class Test1 { public static void main(String[] args) {
try {
FileReader a = new FileReader("F:\\234.txt");
BufferedReader b = new BufferedReader(a);
FileWriter c = new FileWriter("D:\\5666.txt");
BufferedWriter d = new BufferedWriter(c);
String i = b.readLine();
while(i!=null){
d.write(i);
d.newLine();
i=b.readLine();
}
d.close();
c.close();
b.close();
a.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}现在搞不懂一个问题,这2中用法有什么区别 那种方法更好一些??
解决方案 »
- private static final long serialVersionUID = 1L; 这个!L是随便写写的吧?我们也可以随意给它一个值吧。?
- 麻烦您给讲解一下Timer的用法和注意事项
- 如何固定一个JTABLE表头的宽度
- JAVA区的兄弟们,在启动VisiBroker 的GateKeeper时出现java错误,请帮忙!!!!
- 谁能给我个POI包的下载地址啊!
- socket 输入流中文数字混合字符转换??
- 如何在jbuilder中设置db2的驱动?
- 为什么我用jspsmartupload上传文件总是上传0个文件呢?而且把它的字节数组拿出来都是几十,一百来个字节?
- 简单作业题,在线等!
- java 执行错误
- 谁帮我下这个小程序怎么结束不了了 ??
- 如何判断两个实例是否从属于同一个类(两个类都不知道从属于哪一个类的情况下,哎呀,说不清楚,详内)
楼主【peterandy0116】截止到2008-07-05 11:42:47的历史汇总数据(不包括此帖):
发帖的总数量:8 发帖的总分数:80
结贴的总数量:8 结贴的总分数:80
无满意结贴数:4 无满意结贴分:65
未结的帖子数:0 未结的总分数:0
结贴的百分比:100.00% 结分的百分比:100.00%
无满意结贴率:50.00 % 无满意结分率:81.25 %
敬礼!
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.FileChannel;public class FileCopy {
public static void main(String[] args) { File fromFile = new File("F:\\234.txt"); if(!fromFile.exists()) {
System.out.printf("File to copy, %s, does not exist.",
fromFile.getAbsolutePath());
System.exit(1);
}
File toFile = new File("D:\\565.txt"); FileInputStream inFile = null;
FileOutputStream outFile = null;
try {
inFile = new FileInputStream(fromFile);
outFile = new FileOutputStream(toFile); } catch(FileNotFoundException e) {
e.printStackTrace(System.err);
assert false;
} FileChannel inChannel = inFile.getChannel();
FileChannel outChannel = outFile.getChannel(); try {
int bytesWritten = 0;
long byteCount = inChannel.size();
while(bytesWritten<byteCount) {
bytesWritten += inChannel.transferTo(bytesWritten,
byteCount-bytesWritten,
outChannel);
}
System.out.printf("File copy complete. %d bytes copied to %s%n",
byteCount, toFile.getAbsolutePath());
inFile.close();
outFile.close(); } catch(IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
System.exit(0);
}
}