for example, i have a txt file that reads:
number of items: 2
number of zombies: 3
....

i want to write a method that increments just the numbers, and leave the "number of items" and "number of zombies" alone. is there any way to do this?
i can easily read to a specific line, but i can't figure out how to write in it.

Edited 6 Years Ago by plasticfood: n/a

The pointer I can give you is most of programming languages I know, have NO method to modify the content of a file. However, You can copy line by line from a file to another and make your modification in between. Say, in your case, you read the first line, and copy it to another file, leaving it intact, then you read the second line, but you don't write. You modify it using a string or any other structure you like, after it's been modified you write it to the file.

you can write to a file using

try{
            
            PrintWriter pr = new PrintWriter(new FileWriter(new File("filename")));
        
        }catch(FileNotFoundException s){
    
          system.out.println("error file not found");

        }catch(IOException){

           system.out.println("error ioexception")
        
        }

The pointer I can give you is most of programming languages I know, have NO method to modify the content of a file. However, You can copy line by line from a file to another and make your modification in between. Say, in your case, you read the first line, and copy it to another file, leaving it intact, then you read the second line, but you don't write. You modify it using a string or any other structure you like, after it's been modified you write it to the file.

thanks.

This article has been dead for over six months. Start a new discussion instead.