java删除01.html文件中的</body>和</html>并向文件中增加<h1>1</h1></body></html>
时间: 2023-11-16 11:07:07 浏览: 16
抱歉,由于我是一个语言模型,我无法直接修改文件。但是,您可以使用以下Java代码来实现您所需的功能:
```java
import java.io.*;
public class FileManipulation {
public static void main(String[] args) {
try {
File file = new File("01.html");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "";
StringBuilder stringBuilder = new StringBuilder();
while ((line = reader.readLine()) != null) {
if (line.contains("</body>") || line.contains("</html>")) {
// skip this line
} else {
stringBuilder.append(line + "\n");
}
}
reader.close();
// add <h1>1</h1></body></html> to the end of the file
stringBuilder.append("<h1>1</h1></body></html>");
FileWriter writer = new FileWriter(file);
writer.write(stringBuilder.toString());
writer.close();
System.out.println("Successfully modified the file.");
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
```
这段代码将打开名为“01.html”的文件,读取其中的每一行,并将其添加到一个字符串构建器中。如果该行包含"</body>"或"</html>",则跳过该行。最后,将"<h1>1</h1></body></html>"添加到文件的末尾,并将新的字符串写入文件中。