如何读写文本文件! 本人是个菜鸟,希望各位大侠给出具体的例子,谢谢大家。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.*;import java.util.*;public class CopyFile { private String org; private String target; private ArrayList list; private byte[] b = null; private CopyFile(String org, String target) { this.org = org; this.target = target; } /* 读文件 */ private void readFile() { File file = new File(org); list = new ArrayList(); if (!file.exists()) { System.out.println("文件不存在"); System.exit(0); } FileReader fr = null; BufferedReader br = null; try { fr = new FileReader(file); br = new BufferedReader(fr); String tmp = null; while((tmp = br.readLine()) != null) { list.add(tmp); } br.close(); fr.close(); } catch (IOException e) { e.printStackTrace(); } } //写文件 private void writeFile() { File file = new File(target); FileWriter fw = null; BufferedWriter bw = null; try { if (!file.exists()) { file.createNewFile(); } fw = new FileWriter(file); bw = new BufferedWriter(fw); for (int i = 0; i < list.size(); i ++) { //System.out.println(String.valueOf(list.get(i))); bw.write(String.valueOf(list.get(i))); bw.newLine(); } bw.flush(); bw.close(); fw.close(); } catch (IOException e) { e.printStackTrace(); } })参考下 我这个是用NIO作的import java.io.*;import java.nio.*;import java.nio.channels.*;public class ChannelCopy { private static final int BSIZE = 1024; public static void main(String[] args) throws Exception { FileChannel in = new FileInputStream("D:\\source.txt").getChannel(), out = new FileOutputStream("D:\\target.txt").getChannel(); ByteBuffer buffer = ByteBuffer.allocate(BSIZE); while(in.read(buffer) != -1) { buffer.flip(); // Prepare for writing out.write(buffer); buffer.clear(); // Prepare for reading } }} 一个关于界面交互中死循环的问题~·谢谢各位~·在线等着的~· java如何自动的执行一个c语言的程序,然后将运行的结果记录下来呀? 数据库连接的问题 Java中如何把日期存储到ACCESS的DATE/TIME字段中? 没空查API了,直接问 关于开放源代码项目 如何用JAVA调用windows动态库 请问Java中怎么在对象与对象之间传递消息? 如何在ROSE生成java代码时自动生成import 语句? 菜鸟想学,各位大虾指点我该如何学习,我下的jb5为rar,这是什么呀 请教有关Math.random()取整数位数的问题 请问如何读取Java类路径中的配置文件?
import java.util.*;public class CopyFile { private String org;
private String target;
private ArrayList list;
private byte[] b = null;
private CopyFile(String org, String target) {
this.org = org;
this.target = target;
}
/* 读文件 */
private void readFile() {
File file = new File(org);
list = new ArrayList();
if (!file.exists()) {
System.out.println("文件不存在");
System.exit(0);
}
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(file);
br = new BufferedReader(fr);
String tmp = null;
while((tmp = br.readLine()) != null) {
list.add(tmp);
}
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//写文件
private void writeFile() {
File file = new File(target); FileWriter fw = null;
BufferedWriter bw = null;
try {
if (!file.exists()) {
file.createNewFile();
}
fw = new FileWriter(file);
bw = new BufferedWriter(fw);
for (int i = 0; i < list.size(); i ++) {
//System.out.println(String.valueOf(list.get(i)));
bw.write(String.valueOf(list.get(i)));
bw.newLine();
}
bw.flush();
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
)
参考下
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class ChannelCopy {
private static final int BSIZE = 1024;
public static void main(String[] args) throws Exception {
FileChannel
in = new FileInputStream("D:\\source.txt").getChannel(),
out = new FileOutputStream("D:\\target.txt").getChannel();
ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
while(in.read(buffer) != -1) {
buffer.flip(); // Prepare for writing
out.write(buffer);
buffer.clear(); // Prepare for reading
}
}
}