I have content of a file in a StringBuffer. The content of the file includes many lines (not on a single line). I want to edit the content of a line from index 4 (just for example) to the end of that line. I use replace() to edit the content of the StringBuffer.
The point is that the replace method has parameters such as starting index and ending index. But I don’t know what is the ending index since each line have different number of characters
I think of using str.indexOf("\n") to find the ending index of the line, but then the file have many lines, so it will return incorrect results.
this is the readFile() if u need to read the code
Thank you
public StringBuffer readFile(){ //read file line by line
File f = getFilePath(fileName);
StringBuffer sb = new StringBuffer();
String textinLine;
try {
FileInputStream fs = new FileInputStream(f);
InputStreamReader in = new InputStreamReader(fs);
BufferedReader br = new BufferedReader(in);
while (true){
textinLine = br.readLine();
if (textinLine == null) break;
sb.append(textinLine+ "\n");
}
fs.close();
in.close();
br.close();
} ... // just some catch statements heres
}
Use
String.indexOf()as you indicated, but pass in the starting position, e.g.indexOf('\n', 4);