假设我有一段文本,如下
!bbbbbbbbbbbbbb
!cccccccccccccc
@dddddddddddddd
如上文本,我想经过一段逻辑处理之后,把!变成@,遇到以@开头的文本行则不处理请大家帮帮忙

解决方案 »

  1.   

    我只能说下思路首先用IO类里面函数取出文本得到str然后replace就OK
      

  2.   

    你replace不需要往里写吗?你的意思是不是要建一个临时文件那样?
      

  3.   

    replaceAll("!","@");然后这样应该能搞定
      

  4.   

    楼主不是要读取一个文本文档嘛?  如果是一个String的话就更好说了
      

  5.   

    我晕,原来你的replace是这样啊,我是要有逻辑条件的替换,符合条件才行
      

  6.   

    replaceAll("!","@");这样不行??我觉得这样就是复合你的要求了
      

  7.   

    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);}}
      

  8.   

    呵呵,说了要有条件才可以,如果没有条件,那我直接写的时候就写成@,哪还来后面的replaceAll
      

  9.   

    BufferedReader br = new Reader();这个写错了, 
    应该是
    BufferedReader br = new BufferedReader (rf);
      

  10.   

    To  thinkhejie 我的需求是替换过之后,还要写回文件,位置和原先的一样,不能改变
      

  11.   

    我用RandomAccessFile的writeByte实现了把!替换成@,但这样做的话,readLine得到的字符串第一个字符却不是@
    例如:
    @abcdefg
    readline得到的却是a,而不是@
      

  12.   

    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) {
    }
    }
    }
      

  13.   


    判断是不是以@开头,可以用 str.startsWith("@")
      

  14.   

        “while ((tmpString = reader.readLine()) != null) {”   其中的readLine方法,将文件中的指针移动了,
      可以直接查看文档检查一下是否将“!”替换成“@”
      

  15.   

    To  caoze是的,但用了reader.writeByte后,第一个字符就被屏蔽掉了,拿第一个字符往往拿到的是第二个
      

  16.   

    找找中文的jdk api文档看看吧!我初学!呵呵
      

  17.   

    简单方法不会,就会笨的
    先把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();
     } 

    }
    }
      

  18.   

    网上有一本关于基于低级IO的文本数据库
    可以针对文本行的增删改查,不知道有人有这本书吗?有的话,就共享一下,源码,参考文档Email:  [email protected]先行谢过