我想实现一个功能
读一个文本文件,然后将其中的指定文字替换成我想要替换的文字。
请教,如何实现

解决方案 »

  1.   

    可能的程序如下:打开文件A,用于读;
    打开文件B,用于写;while( 读文件A的一行 ) {
      用String类的replace()方法进行文字替换;
      把得到的新String写入文件B中;
    }关闭文件A;
    关闭文件B;
      

  2.   

    RandomAccessFile
    replaceAll
    createFile
      

  3.   

    import java.io.*;public class ReadOneWriteAnother
    {
    public static void main(String[] args)
    {
    System.out.println("Main begin...");
    //源文件路径
    String sourceFilePath = new String("D:\\test\\file\\test_source.txt");
    String targetFilePath = new String("D:\\test\\file\\test_target.txt");
    //要替换的字符串
    String fromString = new String("someting to replace");
    //替换为
    String toString = new String("XXX YYYYY ZZZ");

    GoGoGo(sourceFilePath, targetFilePath, fromString, toString);
    System.out.println("Main end...\n----------------...");
    }

    public static void GoGoGo(
    String sourceFilePath, 
    String targetFilePath,
    String fromString,
    String toString)
    {
    System.out.println("GoGoGo begin...");
    //临时字符串
    String tempString = new String("");

    File sf = new File(sourceFilePath);
    File tf = new File(targetFilePath);

    try
    {
    BufferedReader brs = new BufferedReader(new FileReader(sf));
    BufferedWriter bwt = new BufferedWriter(new FileWriter(tf));

    tempString = brs.readLine();

    while(tempString!=null)
    {
    tempString = tempString.replaceAll(fromString, toString);
    bwt.write(tempString + "\r\n");
    tempString = brs.readLine();
    }

    brs.close();
    bwt.close();
    }
    catch(IOException e)
    {
    System.err.println("GoGoGo Error:" + e);
    }

    System.out.println("GoGoGo end...\n----------------...");
    }
    }
      

  4.   

    import java.io.*;public class ReplaceText {    public String myRead(String path) {
            FileReader myFileReader = null;
            try {
                myFileReader = new FileReader(path);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            BufferedReader myBufferedReader = new BufferedReader(myFileReader);        String myString = null;        String resultString = "";        try {
                while ((myString = myBufferedReader.readLine()) != null) {
                    resultString += myString;
                    resultString += "\n";            }
            } catch (IOException e1) {
                e1.printStackTrace();
            }        try {
                myFileReader.close();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
            return resultString;
        }    public String newReplace(String s, String oldS, String newS) {
            String resultS = "";
            String[] ss;
            ss = s.split(" ");
            for (int i = 0; i < ss.length; i++) {
                if (ss[i].equals(oldS)) {
                    ss[i] = newS;
                }
            }        for (int i = 0; i < ss.length; i++) {
                resultS += ss[i] + " ";
            }
            System.out.println(resultS);
            return resultS;    }    public void myWrite(String path, String sb) {        FileWriter fw = null;
            try {
                fw = new FileWriter(path);
            } catch (IOException e2) {
                e2.printStackTrace();
            }
            try {
                fw.write(sb.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fw.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }    public static void main(String[] args) throws IOException {
            ReplaceText rt = new ReplaceText();
            String sb = rt.newReplace(rt.myRead("c:/1.txt"), "i", "-i-");
            rt.myWrite("c:/2.txt", sb);    }}1.txt:
    ai  i can
    i seeso i goodado i dosi i iso oosi ppif 
    io] ooo99 jl  il s i  di ii i can
    i can
    i can
    i can2.txt:
    ai  -i- can
    i seeso -i- goodado -i- dosi -i- iso oosi ppif 
    io] ooo99 jl  il s -i-  di ii -i- can
    i can
    i can
    i can
      

  5.   

    段首和上一段尾没有分割付,所以出现了bug 
    不好意思