タイトル:ファイル内の特定の行番号にデータを書き込む
これが私のサンプルファイルです:
public class MyC{
public void MyMethod()
{
System.out.println("My method has been accessed");
Syst.out.println("hi");
String l;
nt x=0;
}
}
私の目的は何ですか?
5行目に移動して、文字列Syst.out.println( "hi");を修正します。System.out.println( "hi");へ これまで、エラーのある行にアクセスし、文字列を変数に保存してエラーを置き換えることができました。
これが私が今まで達成したことです:
import java.io.*;
public class NewReplaceLine {
public static void main(String[] args)
{
FileInputStream fs = null;
try {
fs = new FileInputStream("C:\\Users\\workspace\\Compilers\\testfile.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
for(int i = 0; i < 6; ++i)
try {
br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
String lineIWant = br.readLine();
String newtext = lineIWant.replace("Syst", "System");
System.out.println(newtext);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
私ができないのは、行番号5に修正した文字列をファイルに書き直すことです。ありがとう