假如文件内容如下:
baidu zhidao tiwen
java abcd badc
要想将zhidao改成answer,应该怎样操作,用java
我要用JAVA对文件中的某一段字符串进行修改,应该怎样做?谢谢
答案:4 悬赏:30 手机版
解决时间 2021-03-07 12:15
- 提问者网友:孤山下
- 2021-03-07 09:01
最佳答案
- 五星知识达人网友:一袍清酒付
- 2021-03-07 09:09
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
全部回答
- 1楼网友:十鸦
- 2021-03-07 11:20
创建文件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);
}
}
- 2楼网友:旧脸谱
- 2021-03-07 11:12
第一个。
substring()方法截取子字符串有两种方式。
(1)public string substring(int beginindex)
这个方法从字符串中的beginindex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回。该子字符串始于指定索引外的字符,一直到此字符串末尾
(2)public string substring(int beginindex,int endindex)
这个方法从当前字符串中取出一个子串,从beginindex位置开始到endindex-1为结束,子串返回的长度为endindex-beginindex,注意返回的子字符串不包括endindex位置的字符,如果beginindex为负,或endindex大于此string对象的长度,或beginindex大于endindex时会有异常。
第二个
方法concat是用于将指定的字符串串联到此字符串的结尾
具体方法如下:
public class substringdemo{
public static void main(string []args){
string str = "this is the string is true";
string toget1 = null;
string toget2 = null;
toget1 = str.substring(12,18);
system.out.println("the string length is "+ str.length());
system.out.println("the sub string is " +toget1);
toget2 = str.substring(12);
system.out.println("the sub string is " +toget2);
system.out.println("以下为第二个方法");
string con = null;
con = toget1.concat(toget2);
system.out.println("con = " + con);
system.out.println("以下为第三个方法");
string r1 = str.replace("s","aaa");//将x转换成aaa
system.out.println("replace: "+r1);
}
}
- 3楼网友:痴妹与他
- 2021-03-07 09:50
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);
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯