第2个回答 2010-12-26
//主要功能做出来
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
public class JieXi {
private LinkedList list = new LinkedList();
private String sourcePath;
private String targetPath;
public void setSourcePath(String sourcePath) {
this.sourcePath = sourcePath;
}
public void setTargetPath(String targetPath) {
this.targetPath = targetPath;
}
public void setList() {
FileReader fr;
try {
File file = new File(sourcePath);
Runtime.getRuntime().exec("notepad " + sourcePath);
fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while (true) {
String linestr = br.readLine();
if (linestr != null) {
list.add(linestr);
} else
break;
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void intoTxtFile() {
try {
ToFile tf = new ToFile(targetPath);
tf.newFile();
setList();
for (int i = list.size(); i > 0; i--) {
tf.toTxtFile("[" + i + "]" + (String) list.get(i - 1) + "\r\n");
}
tf.close();
Runtime.getRuntime().exec("notepad " + targetPath);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws FileNotFoundException {
JieXi jx = new JieXi();
jx.setSourcePath("D:\\4.txt");
jx.setTargetPath("D:\\2.txt");
jx.intoTxtFile();
}
}
/**
* 写入TXT文件类
*/
class ToFile {
private String filename = "D:\\default.txt";
private File file;
private PrintWriter pw;
public ToFile() throws FileNotFoundException {
newFile();
pw = new PrintWriter(file);
}
public ToFile(String filename) throws FileNotFoundException {
this.filename = filename;
newFile();
pw = new PrintWriter(file);
}
public void newFile() {
file = new File(filename);
if (!file.exists())
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
public void toTxtFile(String str) {
pw.write(str);
}
public void close() {
pw.flush();
pw.close();
}
}