java修改文件中的一行记录 假设我有一段文本,如下!bbbbbbbbbbbbbb!cccccccccccccc@dddddddddddddd如上文本,我想经过一段逻辑处理之后,把!变成@,遇到以@开头的文本行则不处理请大家帮帮忙 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我只能说下思路首先用IO类里面函数取出文本得到str然后replace就OK 你replace不需要往里写吗?你的意思是不是要建一个临时文件那样? replaceAll("!","@");然后这样应该能搞定 楼主不是要读取一个文本文档嘛? 如果是一个String的话就更好说了 我晕,原来你的replace是这样啊,我是要有逻辑条件的替换,符合条件才行 replaceAll("!","@");这样不行??我觉得这样就是复合你的要求了 String str = null;String strflg = null;String str1 = "!";String str2 = "@";File fi = new File ("你的文档.TXT");ReaderFile rf = new ReaderFile (fi);BufferedReader br = new Reader();//遍历文本,取出每一行。while(str = br.readline()!= null ){ //取出第一个字符。 strflg = str.substring(0,1); if(strflg.equals(str1)) { str = str.replace("!","@"); System.ou.println(str); } else {System.ou.println(str);}} 呵呵,说了要有条件才可以,如果没有条件,那我直接写的时候就写成@,哪还来后面的replaceAll BufferedReader br = new Reader();这个写错了, 应该是BufferedReader br = new BufferedReader (rf); To thinkhejie 我的需求是替换过之后,还要写回文件,位置和原先的一样,不能改变 我用RandomAccessFile的writeByte实现了把!替换成@,但这样做的话,readLine得到的字符串第一个字符却不是@例如:@abcdefgreadline得到的却是a,而不是@ RandomAccessFile reader = null; try { reader = new RandomAccessFile(filepath, "rw"); String tmpString = null; int line = 1; reader.writeByte('@'); while ((tmpString = reader.readLine()) != null) { System.out.println(tmpString); if(tmpString.length() > 0){ if (tmpString.charAt(0) != '@') { reader.writeByte('@'); } else { System.out.println("(line:" + line + ":"+ tmpString + ")"); } } line++; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } 判断是不是以@开头,可以用 str.startsWith("@") “while ((tmpString = reader.readLine()) != null) {” 其中的readLine方法,将文件中的指针移动了, 可以直接查看文档检查一下是否将“!”替换成“@” To caoze是的,但用了reader.writeByte后,第一个字符就被屏蔽掉了,拿第一个字符往往拿到的是第二个 找找中文的jdk api文档看看吧!我初学!呵呵 简单方法不会,就会笨的先把a.txt(原始数据)哪出来,改完后放入b.txt,然后再把b.txt写回去import java.io.*;public class Csdn1{ public static void main(String[] args) { BufferedReader bd = null; BufferedWriter bw = null; try { bd = new BufferedReader(new FileReader("F:\\sushuai\\23\\a.txt")); bw = new BufferedWriter(new FileWriter("F:\\sushuai\\23\\b.txt")); String str1 = null; StringBuffer str2 = null; while((str1 = bd.readLine()) != null) { // System.out.println(str1); if (str1.length() == 0 ) { } else if (str1.charAt(0) == '@'){ } else if (str1.charAt(0) == '!') { str2 = new StringBuffer(str1); str2.setCharAt(0,'@'); str1 =str2.substring(0); } bw.write(str1,0,str1.length()); bw.newLine(); } bd.close(); bw.close(); }catch(IOException e) { e.printStackTrace(); } try { bd = new BufferedReader(new FileReader("F:\\sushuai\\23\\b.txt")); bw = new BufferedWriter(new FileWriter("F:\\sushuai\\23\\a.txt")); String str3 = null; while((str3 = bd.readLine())!= null) { bw.write(str3,0,str3.length()); bw.newLine(); } bd.close(); bw.close(); } catch(IOException e1) { e1.printStackTrace(); } }} 网上有一本关于基于低级IO的文本数据库可以针对文本行的增删改查,不知道有人有这本书吗?有的话,就共享一下,源码,参考文档Email: [email protected]先行谢过 java 画虚线问题 求助 java 连接 pervasive sql 这个问题很菜,我忘了怎么解决。谢谢 Hibernate自动连接 eclipse的各个插件,runtime和sdk有什么差别? 那位高手把这段代码解释一下? 关于用java写图片识别 JSON串的解析 哪里有Thinking in Java第二版的课后题答案(部分也行)下载?(高分相赠) 请教有关事务提交的问题,先谢了。 多个class文件问题打包成可执行JAR的问题 某公司java笔试题 看看你能不能做上来!
String strflg = null;
String str1 = "!";
String str2 = "@";File fi = new File ("你的文档.TXT");
ReaderFile rf = new ReaderFile (fi);
BufferedReader br = new Reader();//遍历文本,取出每一行。
while(str = br.readline()!= null )
{
//取出第一个字符。
strflg = str.substring(0,1);
if(strflg.equals(str1))
{
str = str.replace("!","@");
System.ou.println(str);
}
else {System.ou.println(str);}}
应该是
BufferedReader br = new BufferedReader (rf);
例如:
@abcdefg
readline得到的却是a,而不是@
try { reader = new RandomAccessFile(filepath, "rw");
String tmpString = null;
int line = 1;
reader.writeByte('@');
while ((tmpString = reader.readLine()) != null) {
System.out.println(tmpString);
if(tmpString.length() > 0){
if (tmpString.charAt(0) != '@') {
reader.writeByte('@'); } else {
System.out.println("(line:" + line + ":"+ tmpString + ")");
}
}
line++;
} reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
判断是不是以@开头,可以用 str.startsWith("@")
可以直接查看文档检查一下是否将“!”替换成“@”
先把a.txt(原始数据)哪出来,改完后放入b.txt,然后再把b.txt写回去
import java.io.*;
public class Csdn1{
public static void main(String[] args) {
BufferedReader bd = null;
BufferedWriter bw = null;
try { bd = new BufferedReader(new FileReader("F:\\sushuai\\23\\a.txt"));
bw = new BufferedWriter(new FileWriter("F:\\sushuai\\23\\b.txt"));
String str1 = null;
StringBuffer str2 = null;
while((str1 = bd.readLine()) != null) {
// System.out.println(str1);
if (str1.length() == 0 ) {
} else if (str1.charAt(0) == '@'){
} else if (str1.charAt(0) == '!') {
str2 = new StringBuffer(str1);
str2.setCharAt(0,'@');
str1 =str2.substring(0);
}
bw.write(str1,0,str1.length());
bw.newLine();
}
bd.close();
bw.close();
}catch(IOException e) {
e.printStackTrace();
}
try {
bd = new BufferedReader(new FileReader("F:\\sushuai\\23\\b.txt"));
bw = new BufferedWriter(new FileWriter("F:\\sushuai\\23\\a.txt"));
String str3 = null;
while((str3 = bd.readLine())!= null) {
bw.write(str3,0,str3.length());
bw.newLine();
}
bd.close();
bw.close();
} catch(IOException e1) {
e1.printStackTrace();
}
}
}
可以针对文本行的增删改查,不知道有人有这本书吗?有的话,就共享一下,源码,参考文档Email: [email protected]先行谢过