kunal bhardwaj
kunal bhardwaj

Reputation: 19

Reading and writing to a same file in java

How do I read each char from a txt file and write the same or replaced char at the same position in the same file and move to the next char and keep doing this till the end of the file?

Upvotes: 0

Views: 1397

Answers (2)

Kassadin
Kassadin

Reputation: 449

You need RandomAccessFile

RandomAccessFile rf = new RandomAccessFile("data.txt", "rw"); 

then you can read or write data at any position of the file, just seek() to the position, and read() or write()

Upvotes: 2

Peter Kahn
Peter Kahn

Reputation: 13026

Why not use two files and then move the new file over the old at the end. That gives you an abort option?

Jdk File class javadoc will give you the methods you need for reading and writing

https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html

Upvotes: 1

Related Questions