As an example, if you have an existing text file and you run this code the data will be inserted into the begining of the file. I wouldn't suggest using this technique if the file contains a large amount of data as performance might be an issue.
Code:
import java.io.*;
class Insert{
public static void main(String[] args){
readData();
}
public static void readData(){
try{
StringBuffer sb = new StringBuffer();
BufferedReader buffr = new BufferedReader(new FileReader("C://Java/text.txt"));
while(true){
String s = buffr.readLine();
if(s == null) break;
sb.append(s);
}
buffr.close();
writeData(sb);
}catch(Exception e){System.err.println(e);}
}
public static void writeData(StringBuffer sb){
try{
BufferedWriter buffw = new BufferedWriter(new FileWriter("C://Java/text.txt"));
String additionaldata = new String("Additional data");
sb.insert(0,additionaldata);
buffw.write(new String(sb));
buffw.flush();
buffw.close();
}catch(Exception e){System.err.println(e);}
}
}