So I want to write a String at a specific point in a text file. Should be quite easy but this is my first time using the BufferedWriter class. My source is as follows:
public static String readFile(String fileName) throws IOException {
String toReturn = "";
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("test.txt"));
while ((sCurrentLine = br.readLine()) != null) {
toReturn = toReturn+"\n"+sCurrentLine;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return toReturn;
}
public static void addAfter(String toAdd, char after, String fileName) throws IOException {
String file = readFile(fileName);
int length = file.length();
char[] chr = file.toCharArray();
boolean pos[] = new boolean[length];
for(int i = 0; i < length; i++) {
if(chr[i] == after) {
pos[i] = true;
}
}
BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true));
}
I would like to add the String toAdd at position i using the BufferedWriter class. How would I go about jumping to the desired point and writing toAdd?
Thanks in advance