替换文本:将文本文件中的所有src替换为dst
题意:
将文本文件中的所有src替换为dst
方法一:使用String
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner; public class Demo2 {
public static void main(String[] args) throws FileNotFoundException {
// 使用Scanner处理文本
Scanner sc = new Scanner(new File("ddd.txt")); // 文件可能不存在,所以要声明异常
String passage = ""; while(sc.hasNextLine()) // 把ddd.txt的内容保存到passage字符串中
passage += sc.nextLine() + "\n"; // nextLine()中不包含回车 // 把ddd.txt中的src替换为dst
String src = "Hello";
String dst = "World";
passage = passage.replace(src, dst); // 注意replace()方法的返回值
// 使用PrintWriter写入文本
PrintWriter pw = new PrintWriter("ddd.txt");
pw.print(passage); // 将替换后的文本写回ddd.txt (覆盖写) pw.close(); // 记得关流,不然数据写不进去
}
}
方法二:使用StringBuffer
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner; public class Demo {
public static void main(String[] args) throws FileNotFoundException {
// 使用Scanner处理文本
Scanner sc = new Scanner(new File("ddd.txt")); // 文件可能不存在,所以要声明异常
StringBuffer sb = new StringBuffer();
while(sc.hasNextLine()) {
sb.append(sc.nextLine()); // nextLine()中不包含回车
sb.append('\n');
} // 把sb中的src替换为dst
String src = "static";
String dst = "Hello";
int index = sb.indexOf(src); // 找到src第一次出现的位置
int end;
while(index != -1) {
end = index + src.length();
sb.replace(index, end, dst); // 用dst替换src字符串
index = sb.indexOf(src, end); // 从end开始,可以避免不必要的扫描
}
// 使用PrintWriter写入文本
PrintWriter pw = new PrintWriter("ddd.txt");
pw.print(sb.toString()); // 将替换后的文本写回ddd.txt (覆盖写) pw.close(); // 记得关流,不然数据写不进去
}
}
替换文本:将文本文件中的所有src替换为dst的更多相关文章
- PyCharm中批量查找及替换
选中需要操作的字符 Ctrl + R 替换 Ctrl + Shift + F 全局查找 Ctrl + Shift + R 全局替换 源自: PyCharm中批量查找及替换 - Ella_Wu - 博客 ...
- 文件6. 查找替换.txt文本文件中的内容
servlet实现对文本文件的查找替换 .jsp界面 <form> <table> <tr> <td>选择文本文件:</td> <td ...
- C# 在word中查找及替换文本
C# 在word中查找及替换文本 在处理word文档时,很多人都会用到查找和替换功能.尤其是在处理庞大的word文档的时候,Microsoft word的查找替换功能就变得尤为重要,它不仅能让我们轻易 ...
- C# 替换文本文件中的某一行
C# 替换文本文件中的某一行 (要求此文件存在) /// <summary> /// LineIndex 表示新的内容所在的行位置 /// </summary> /// < ...
- linux中批量替换文本中字符串--转载
(一)通过vi编辑器来替换.vi/vim 中可以使用 :s 命令来替换字符串.:s/well/good/ 替换当前行第一个 well 为 good:s/well/good/g 替换当前行所有 well ...
- bat批处理 查找替换:批处理如何查找并替换文本里特定字符串中的部分内容
批处理如何查找并替换文本里特定字符串中的部分内容 摘自:http://www.bathome.net/thread-43349-1-1.html 脚本如下: @if()==() echo off &a ...
- NX二次开发-将信息窗口中的文本保存到文本文件中UF_UI_save_listing_window
#include <uf.h> #include <uf_ui.h> UF_initialize(); //打开信息窗口 UF_UI_open_listing_window() ...
- Java 添加、删除、替换、格式化Word中的文本(基于Spire.Cloud.SDK for Java)
Spire.Cloud.SDK for Java提供了TextRangesApi接口可通过addTextRange()添加文本.deleteTextRange()删除文本.updateTextRang ...
- linux下批量查找/替换文本内容
一般在本地电脑上批量替换文本有许多工具可以做到,比如sublime text ,但大多服务器上都是无图形界面的,为此收集了几条针对linux命令行 实现批量替换文本内容的命令: 1.批量查找某个目下文 ...
随机推荐
- java代码优化写法(转摘)
本文源地址:https://blog.csdn.net/syc001/article/details/72841650 可供程序利用的资源(内存.CPU时间.网络带宽等)是有限的,优化的目的就是让程序 ...
- Ansible 安装使用过程中遇到过的问题
1.[root@ansible ~]# ansible-doc -l [DEPRECATION WARNING]: docker is kept for backwards compatibility ...
- linux拆分文件
1.先看下文件总的行数: wc -l filename 我们现在来看看它具体的参数该怎么用: split支持自定义输出文件大小和输出文件行数两种模式,此外还可以定义每一行最大的值. -l 按输出文件行 ...
- DFS-深度优先搜索与BFS-广度优先搜索
1.DFS DFS是一个递归过程.(类似于二叉树的前序遍历) 参考:深度优先搜索(Depth-First-Search)精髓 2.BFS 可以理解为按层遍历,借助队列结构来实现.(类似于二叉树的层次遍 ...
- PAT甲级——A1047 Student List for Course
Zhejiang University has 40,000 students and provides 2,500 courses. Now given the registered course ...
- PAT甲级——A1014 Waiting in Line
Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...
- 进一步封装poco下的mysql操作
为方便程序对mysql操作,我对poco的mysql进行了再次封装,主要是针对自己应用需要的部分. 开发工具:netbean 系统环境:centos7 poco版本: poco-1.9.0-all 主 ...
- 023-linux(2)
1. head 查看文件的前N行 -n ,表示查看前几行 head - test.txt 2. tail 查看文件的后N行 -n,表示查看文件的后几行 tail - test.txt -f(循环读取) ...
- 一、WebService基础概念
一.Web Service简介 1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intrane ...
- TZOJ 1503 Incredible Cows(折半搜索+二分)
描述 Farmer John is well known for his great cows. Recently, the cows have decided to participate in t ...