欢迎光临散文网 会员登陆 & 注册

第五题

2023-02-20 14:57 作者:猫猫的掠夺刀  | 我要投稿

import java.io.*; public class IOmain { public static void main(String[] args) { // 定义源文件路径和目标文件路径 String srcPath = "d:/test1.txt"; String destPath = "d:/test2.txt"; // 创建输入流和输出流 BufferedReader reader = null; BufferedWriter writer = null; try { reader = new BufferedReader(new FileReader(srcPath)); writer = new BufferedWriter(new FileWriter(destPath)); // 一次读取一行数据 String line; while ((line = reader.readLine()) != null) { // 将数字替换为空字符串 line = line.replaceAll("\\d", ""); // 写入目标文件 writer.write(line); // 换行 writer.newLine(); } } catch (IOException e) { e.printStackTrace(); } finally { // 关闭输入流和输出流 if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } if (writer != null) { try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } } }

第五题的评论 (共 条)

分享到微博请遵守国家法律