用另一个字符串替换File中的行

用另一个字符串替换File中的行

本文介绍了用另一个字符串替换File中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a text file with the following contents:

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

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

I have an array num[]= {2,3,4}; which contains the line numbers to be completely replaced with the strings from this array

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

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

第2行将被替换为AB,第3行将替换为BD,而ine 4将替换为CD。

That is line 2 will be replaced with AB, line 3 with BD and ine 4 with CD.

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

Lines which are not in the num[]array have to be written to a new file along with the changes made.

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

I have this so far.I tried several kind of loops but still it does not work.

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

当我运行代码时,所有行都显示在同一行。

When I run the code, all lines appear on the same line.

推荐答案

你差不多完成了,我用地图更新了你的代码。检查这个

You've done almost, I've updated your code with a map. Check this

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

这篇关于用另一个字符串替换File中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 17:54