一个处理文本的问题。
我想实现这样一个效果。
就是读取一个文件,一行一行的读。当读到一行等于我指定的值。
然后再这行的下一行插入一段字符串。然后把这个文件改名保存。
请问该如何实现。最好能给个例子谢谢!
问题点数:20、回复次数:9Top
1 楼Mars_lee(二子)回复于 2001-05-24 17:26:00 得分 0
那就按照这个步骤做呗,有什么问题么?
Top
2 楼ender(ender)回复于 2001-05-24 17:32:00 得分 0
哈哈,你该不是想让大家帮你写程序吧?Top
3 楼backlove(我愿意)回复于 2001-05-24 17:38:00 得分 0
有可能Top
4 楼Kert_ake(有坑我就跳)回复于 2001-05-25 13:31:00 得分 0
给个例子不就是帮你做完了么??Top
5 楼skyyoung(路人甲)回复于 2001-05-25 13:45:00 得分 20
java jINSERT test.out 9 "hello world"
will insert the string "hello world" at line number 9 in the file "test.out".
of course you need more error checking... [JDK1.1]
import java.io.*;
public class jINSERT {
public static void main(String args[]){
try {
jINSERT j = new jINSERT();
j.insertStringInFile
(new File(args[0]),Integer.parseInt(args[1]), args[2]);
}
catch (Exception e) {
e.printStackTrace();
}
}
public void insertStringInFile(File inFile, int lineno, String lineToBeInserted)
throws Exception {
// temp file
File outFile = new File("$$$$$$$$.tmp");
// input
FileInputStream fis = new FileInputStream(inFile);
BufferedReader in = new BufferedReader
(new InputStreamReader(fis));
// output
FileOutputStream fos = new FileOutputStream(outFile);
PrintWriter out = new PrintWriter(fos);
String thisLine = "";
int i =1;
while ((thisLine = in.readLine()) != null) {
if(i == lineno) out.println(lineToBeInserted);
out.println(thisLine);
i++;
}
out.flush();
out.close();
in.close();
inFile.delete();
outFile.renameTo(inFile);
}
}
Top
6 楼flying_bird(飞翔鸟)回复于 2001-05-25 14:27:00 得分 0
学学路人甲吧。。。
我觉得他越来越象我的偶像了。。呵呵。
Top
7 楼hello_wyq(半瓶墨水)回复于 2001-05-25 15:50:00 得分 0
haha
Top
8 楼nicolas(nicolas)回复于 2001-05-25 17:18:00 得分 0
打开一个目的文件(读文件read.txt),一个写文件(write.txt),每读read.txt文件一行,就
往write.txt文件里写入,如果遇到指定的值就多写入一行要写入的值。Top
9 楼ender(ender)回复于 2001-05-28 10:59:00 得分 0
哈哈,真是诲人不倦啊……佩服…………Top




