1

我有一个包含以下内容的文本文件:

public class MyC{
public void MyMethod()
{
    System.out.println("My method has been accessed");
    System.out.println("hi");
}
}

我有一个数组 num[]= {2,3,4}; 其中包含要完全替换为此数组中的字符串的行号

字符串[] 值 = 新字符串[] {"AB","BC","CD"};

也就是说,第 2 行将替换为 AB,第 3 行替换为 BD,第 4 行替换为 CD。

不在 num[]array 中的行必须与所做的更改一起写入新文件。

到目前为止我有这个。我尝试了几种循环,但它仍然不起作用。

public class ReadFileandReplace {

/**
 * @param args
 */
public static void main(String[] args) {



    try {



         int num[] = {3,4,5};

         String[] VALUES = new String[] {"AB","BC","CD"};

         int l = num.length;

         FileInputStream fs= new FileInputStream("C:\\Users\\Antish\\Desktop\\Test_File.txt");
         BufferedReader br = new BufferedReader(new InputStreamReader(fs));

         LineNumberReader reader = new LineNumberReader(br);

         FileWriter writer1 = new FileWriter("C:\\Users\\Antish\\Desktop\\Test_File1.txt");

         String line;
         int count =0;

         line = br.readLine();
         count++;

         while(line!=null){
              System.out.println(count+": "+line);
              line = br.readLine();
              count++;

              int i=0;
                  if(count==num[i]){
                      int j=0;;
                    System.out.println(count);
                    String newtext = line.replace(line, VALUES[j]) + System.lineSeparator();
                    j++;
                                            writer1.write(newtext);
                  }
                  i++;
                  writer1.append(line);
              }


    writer1.close();
    }
    catch (IOException e) {
        e.printStackTrace();
    } finally {
    }

}


}

预期的输出应如下所示:

public class MyC{
AB
BC
    CD
    Sys.out.println("hi");
}
}

当我运行代码时,所有行都出现在同一行。

4

4 回答 4

2

您将每一行附加到同一个字符串。您还应该在每行的末尾添加行分隔符。(您可以使用 健壮地做到这一点System.getProperty("line.separator")

于 2013-01-08T10:33:38.580 回答
2

你几乎完成了,我已经用地图更新了你的代码。检查这个

int num[] = {3, 4, 5};
String[] values = new String[]{"AB", "BC", "CD"};

HashMap<Integer,String> lineValueMap = new HashMap();
for(int i=0 ;i<num.length ; i++) {
    lineValueMap.put(num[i],values[i]);
}


FileInputStream fs = new FileInputStream("test.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fs));

FileWriter writer1 = new FileWriter("test1.txt");

int count = 1;
String line = br.readLine();
while (line != null) {
    String replaceValue = lineValueMap.get(count);
    if(replaceValue != null) {
        writer1.write(replaceValue);
    } else {
        writer1.write(line);
    }
    writer1.write(System.getProperty("line.separator"));
    line = br.readLine();
    count++;
}
writer1.flush();
于 2013-01-08T10:51:10.993 回答
0

您没有附加结束行字符。writer1.append(line); 正在将数据附加到没有结束符的行中。因此它显示在一行中。您可能需要将其更改为: writer1.append(line).append("\n");

于 2013-01-08T10:33:12.173 回答
0

试试这个

package src;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.TimeZone;

public class MainTest {


    static int i ;

    public static void main(String[] arg)
    {
        try {



             int num[] = {3,4,5};

             String[] VALUES = new String[] {"AB","BC","CD"};

             FileInputStream fs= new FileInputStream("C:\\Test\\ren.txt");
             BufferedReader br = new BufferedReader(new InputStreamReader(fs));

             FileWriter writer1 = new FileWriter("C:\\Test\\ren1.txt");

             String line;
             Integer count =0;

             line = br.readLine();
             count++;

             while(line!=null){


                 for(int index =0;index<num.length;index++){
                     if(count == num[index]){
                         line = VALUES[index];
                     }
                 }


                 writer1.write(line+System.getProperty("line.separator"));

                 line = br.readLine();

                 count++;


                 }


        writer1.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        } finally {
        }

}
}
于 2013-01-08T10:54:23.157 回答