java 找到一行 更换单词,使用java查找和替换文本文件中的单词

I am trying to find and replace certain words in a text file using java. My code works to an extent however the output I am getting is wrong.

I need to replace multiple words from a line in a text file with user input however when I run my code the line copies itself once for every word I am trying to replace.

For example if I want to replace 3 words from the following:

python ycsb phase db -s -P /home/james/YCSB/workloads/workloada -p

db.url=db://IP:port -p db.database=name

I end up with 3 copies of the line, each with a different word replaced. Rather than 1 line with all 3 of the required words replaced.

Code below, thanks in advance.

public static void main(String[] args) {

System.out.print("Phase: ");

Scanner sp = new Scanner(System.in);

String p = sp.nextLine();

System.out.print("Database: ");

Scanner sd = new Scanner(System.in);

String d = sd.nextLine();

System.out.print("IP address: ");

Scanner sip = new Scanner(System.in);

int ip = sip.nextInt();

try {

File file = new File("C://users//James//Desktop//newcommand.txt");

BufferedReader reader = new BufferedReader(new FileReader(file));

String line = "", oldtext = "";

while((line = reader.readLine()) != null) {

oldtext += line + "\r\n";

}

reader.close();

String phase = oldtext.replaceAll("phase", "" + p);

String database = oldtext.replaceAll("db", "" + d);

String ips = oldtext.replaceAll("IP", "" + ip);

FileWriter writer = new FileWriter("C://users//James//Desktop//newcommand.txt");

writer.write(phase + ips + database);

writer.close();

} catch (IOException e) {

// handle e

}

}

解决方案

if I understand well the situation, maybe the problem is that you are replacing the same string,and storing in different var,

try that:

public static void main(String[] args) {

System.out.print("Phase: ");

Scanner sp = new Scanner(System.in);

String p;

p = sp.nextLine();

System.out.print("Database: ");

Scanner sd = new Scanner(System.in);

String d;

d = sd.nextLine();

System.out.print("IP address: ");

Scanner sip = new Scanner(System.in);

int ip = sip.nextInt();

{

try

{

File file = new File("C://users//James//Desktop//newcommand.txt");

BufferedReader reader = new BufferedReader(new FileReader(file));

String line = "", oldtext = "";

while((line = reader.readLine()) != null)

{

oldtext += line + "\r\n";

}

reader.close();

String replacedtext = oldtext.replaceAll("phase", "" + p);

replacedtext = replacedtext.replaceAll("db", "" + d);

replacedtext = replacedtext.replaceAll("IP", "" + ip);

FileWriter writer = new FileWriter("C://users//James//Desktop//newcommand.txt");

writer.write(replacedtext);

writer.close();

}

catch (IOException ioe)

{

ioe.printStackTrace();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第二次作业: 1. 编写点类(Point类),属性成员有x,y,都是double数据类型。需要为Point类编写构造函数。 编写直线类(Line类),需要提供两点确定一条直线的函数功能。 如果两点重合,可以返回异常或者返回null引用来解决这个问题。 直线类的数据成员和函数成员请自行设计。 2. 给定文本文件,文件名称为a.txt,文件内容为一个8行8列的字符矩阵,内容为1和0字符, 请编程计算出该矩阵水平方向或者垂直方向或者斜线方 向连续1最多的个数。 例如: 11001101 10110101 01010101 11001000 01010101 11001101 00011000 11110000 3. 编写程序求出1万以内的所有素数,并将这些素数输出到一个文本文件,每行文本只包含一个素数数据。 该文本文件内容要求可以用记事本程序来查看。 4. 编写程序求出1万以内的所有素数,然后再判断这些素数哪些是由素数拼接而成的。 例如素数23就符合条件,23本身是素数,其由素数2,和素数3拼接(连接)组成。 素数29就不满足条件,2是素数,而9不是素数。素数307不满足条件,不能忽略0. 7907这个素数符合条件,7是素数,907是素数。 需要把符合条件的拼接素数全部输出,并统计个数。 5. 要求从控制台输入英语单词单词解释两项数据, 把录入的数据追加到文件。要求提供单词查询功能。 用户输入单词后,从单词库文件查找,如果存在则输出 该单词的解释。注意,单词不能有重复,如果重复则覆盖替换 以前的解释数据。 6. 通过命令行参数输入一个文件夹的路径名称,然后编写程序找出该文件夹下文件名称重复并且文件大小也一样的文件, 如果没有“重复文件”,则输出“没有重复文件”的提示,如果有,需要输出文件名称,和文件所在的文件夹路径(绝对路径)。 提示,需要遍历该文件夹下所有子文件夹,设计一个文件类,属性包括文件名称,文件路径,文件大小,然后进行“重复” 判断,如果文件重复,则需要记录并输出,有可能有文件名重复,但是文件大小不一样,重复的文件可能不止2个,可能 在不同的子文件夹下有多个文件重复。 7. 霍夫曼编码实现压缩文本文件,见文件huffman.rar. 对文件数据读写等功能已经实现,程序在Q2Resources.zip。 Q2Resources.zip的文件禁止修改。请将TextZip.java文件所有未实现的函数按照要求给以实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值