我要用JAVA对文件中的某一段字符串进行修改,应该怎样做?谢谢 5
假如文件内容如下:baiduzhidaotiwenjavaabcdbadc要想将zhidao改成answer,应该怎样操作,用java...
假如文件内容如下:
baidu zhidao tiwen
java abcd badc
要想将zhidao改成answer,应该怎样操作,用java 展开
baidu zhidao tiwen
java abcd badc
要想将zhidao改成answer,应该怎样操作,用java 展开
3个回答
展开全部
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Du {
public static void main(String[] args) throws IOException {
String srcFile = "test.txt";
String targetFile = "test1.txt";
String text = "zhidao";
String replace = "answer";
replaceContentInFile(srcFile, targetFile, text, replace);
}
private static void replaceContentInFile(String srcFile, String targetFile, String text, String replace) throws FileNotFoundException, IOException {
BufferedReader bfr = new BufferedReader(new FileReader(srcFile));
BufferedWriter bfw = new BufferedWriter(new FileWriter(targetFile));
String content = null;
while((content = bfr.readLine()) != null){
bfw.write(content.replaceAll(text, replace));
bfw.newLine();
}
bfw.close();
bfr.close();
}
}
---------------运行完成后目标文件内容
baidu answer tiwen
java abcd badc
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Du {
public static void main(String[] args) throws IOException {
String srcFile = "test.txt";
String targetFile = "test1.txt";
String text = "zhidao";
String replace = "answer";
replaceContentInFile(srcFile, targetFile, text, replace);
}
private static void replaceContentInFile(String srcFile, String targetFile, String text, String replace) throws FileNotFoundException, IOException {
BufferedReader bfr = new BufferedReader(new FileReader(srcFile));
BufferedWriter bfw = new BufferedWriter(new FileWriter(targetFile));
String content = null;
while((content = bfr.readLine()) != null){
bfw.write(content.replaceAll(text, replace));
bfw.newLine();
}
bfw.close();
bfr.close();
}
}
---------------运行完成后目标文件内容
baidu answer tiwen
java abcd badc
来自:求助得到的回答
展开全部
public class Test {
public static void main(String[] args) {
String str ="baidu zhidao tiwen java abcd badc";
str = str.replace("zhidao", "answer");
System.out.println("new string:" + str);
}
}
public static void main(String[] args) {
String str ="baidu zhidao tiwen java abcd badc";
str = str.replace("zhidao", "answer");
System.out.println("new string:" + str);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
创建文件Test.txt,内容如上
代码如下:
import java.io.FileReader;
import java.io.FileWriter;
public class TEST {
public static void print(Object obj){
System.out.println(obj);
}
public static void main(String[] args) throws Exception{
FileReader fr;
FileWriter fw;
char[] ch = new char[100];
StringBuffer sb = new StringBuffer();
String str = "";
int count;//读取字符的个数
char c;
try{
fr = new FileReader("Test.txt");
count = fr.read(ch);
fr.close();
for(int i = 0;i < count; i++){
c = ch[i];
sb.append(c);
}
print("替换前\n" + sb);
str = sb.toString().replace("zhidao", "answer");
fw = new FileWriter("Test.txt");
fw.write(str);
fw.close();
}catch(Exception e){
print(e);
}
print("替换后\n" + str);
}
}
代码如下:
import java.io.FileReader;
import java.io.FileWriter;
public class TEST {
public static void print(Object obj){
System.out.println(obj);
}
public static void main(String[] args) throws Exception{
FileReader fr;
FileWriter fw;
char[] ch = new char[100];
StringBuffer sb = new StringBuffer();
String str = "";
int count;//读取字符的个数
char c;
try{
fr = new FileReader("Test.txt");
count = fr.read(ch);
fr.close();
for(int i = 0;i < count; i++){
c = ch[i];
sb.append(c);
}
print("替换前\n" + sb);
str = sb.toString().replace("zhidao", "answer");
fw = new FileWriter("Test.txt");
fw.write(str);
fw.close();
}catch(Exception e){
print(e);
}
print("替换后\n" + str);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询