简体   繁体   中英

Reading and writing to a same file in java

如何从txt文件读取每个字符,并在同一文件中的相同位置写入相同或替换的字符,然后移至下一个字符,并继续这样做直到文件末尾?

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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM