-1

私のファイルには次のものがあります:

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

以下のコードは、次のことを行います。1. 行数が に等しいnum[index]場合、その行に からの文字列が含まれているかどうかをチェックします VALUES1[index]。true の場合、その文字列を最初のインデックスから置き換えてVALUES[index]、新しいファイルに書き込みます。

2.num インデックスが等しくない場合num[index]は、その行をそのまま新しいファイルに書き込みます。

問題は、新しいファイルに書き込んだ後、元の行 1 と 2 がまだ新しいファイルに表示されることです。それを取り除く方法。

私のコードは次のとおりです。

public class MainTest {


    static int i ;

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



             int num[] = {1,2,4}; //Line Numbers

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

             String[] VALUES1 = new String[] {"class","void","System"}; //To Replace


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

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

             String line;
             String line1 = null;

             Integer count =0;

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

             while(line!=null){


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

                         if(line.contains(VALUES1[index])){

                             line1= line.replace(VALUES1[index], VALUES[index]);
                              writer1.write(line1+System.getProperty("line.separator"));
                         }


                     }
                 }


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

                 line = br.readLine();

                 count++;


                 }


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

     }
}

ここに私の結果があります:

    public AB MyC{
    **public class MyC{ //This still appears.**
    public BC MyMethod()
    **public void MyMethod()//This still appears.**
    {
        CD.out.println("My method has been accessed");
        System.out.println("My method has been accessed");
        System.out.println("hi");
    }
}
4

2 に答える 2

0

この行をコードでコメントする

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

その後、試してみてください

于 2013-01-10T07:29:22.890 に答える
0

この行が置き換えられたかどうかを示すインジケーターを追加する必要があります

while(line!=null){

             boolean exists = false;
             for(int index =0;index<num.length;index++){
                 if(count == num[index]){

                     if(line.contains(VALUES1[index])){
                         exists = true;
                         line1= line.replace(VALUES1[index], VALUES[index]);
                          writer1.write(line1+System.getProperty("line.separator"));
                     }


                 }
             }

             if (!exists)
                 writer1.write(line+System.getProperty("line.separator"));
于 2013-01-10T07:22:26.357 に答える