Janek Sławiński Janek Sławiński - 5 months ago 7x
Java Question

File Writer that puts a number of a line before the line it's about to write

I am writing a method for my java class. it looks like this so far:

String file_name;
String line;

void addLine(file_name, line){
int line_number;
try {
FileWriter writer = new FileWriter(file_name, true);
PrintWriter out = new PrintWriter(writer);

out.println(line_number + line);
catch (IOException e){

How should I define line_number so it would check how many lines were there in file before I printed out next into it?

          int totalLines = 0;

          BufferedReader br   br = new BufferedReader(new   FileReader("C:\\filename.txt"));
                String  CurrentLine = "";
                while ((CurrentLine = br.readLine()) != null) {

i think you have to actually read the file by using a bufferedreader. and then keep on incrementing the totalLines till it reach the end of the file