This article is based on the possibility that when you use randomAccessFile to read and write a file, you can be sure that the transcoding form is not wrong, but the random code still appears.
package ABC;
import java.io.*;
public class markdown {
public static void main(String[] argv) {
try {
RandomAccessFile br = new RandomAccessFile("D:\\officePC\\onedirve\\OneDrive\\桌面\\1.txt","rw");
String str;
long position = 0;
long next_position = 0;
while((str = br.readLine())!=null) {
position = br.getFilePointer();
String str1 = new String(str.getBytes("ISO-8859-1"));
String str2 = new String("##".getBytes("ISO-8859-1")) + str1;
br.seek(next_position);
System.out.println(str2);
br.write(str2.getBytes());
br.write(new String("\n").getBytes());
next_position = position;
}
br.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
After the above code has executed two loops, this situation occurs:
As you can see, I first read a position, position, and then change each line (add). The length of each line changes, but the memory length of each line does not change, so the system sends its extra characters to the next line, and the next line is overwritten.
solution: you must read the rest in and modify it.
Read More:
- Java: How to Find the Minimum Value from a Random Array by Bubble Sort Method
- [Solved] Kafka in Windows error:java. nio. file. Filesystemexception: this file is in use by another program and cannot be accessed by the process
- [Solved] java.security.AccessControlException: Access Denied Error
- [Solved] swagger Docmentation Access Error: Illegal DefaultValue 1024 for parameter type integer, java.lang.NumberFormatException
- The Java class generated by protocol reports an error: cannot access
- Mongodb java version 3.X, prompt “XXX (an encryption algorithm) is not available” when there is a user name and password
- [Solved] hadoop Error: 9000 failed on connection exception java.net.ConnectException Denied to Access
- JAVA Error Illegal access: this web application instance has been stopped already. Could not load net.sf
- Springboot integrates Redis annotation and access error: java.io.NotSerializableException: com.demo.entity.MemberEntity
- Java error: unable to find or load main class (package name in source file)
- Java parsing xml file encounters special symbols & will be abnormal solutions
- [Solved] jar file Execute Error: power shell error: unable to access jarfile
- [Solved] No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.errError: Network Error
- The precision of decimal calculation with double and float in Java is not accurate
- [Solved] Read the resources resource and convert it to file error: java.io.filenotfoundexception
- Java learning unreported exception java.io.IOException ; must be caught or declared to be thrown
- [Solved] java.lang.IllegalAccessError: class org.springframework.data.redis.core.$ Proxy237 cannot access its superinterface org.springframework.data.redis.core.RedisConnectionUtils$RedisConnectionProxy
- CreateProcess error = 5, access denied. [How to Solve]
- Idea unified setting code to UTF-8 code and solution of Tomcat garbled code
- The java springboot websocket service server actively closes the connection and causes java.io.EOFException to be thrown