在文本文件中查找字符串,然后在未找到字符串的情况下将字符串添加到文本文件的方法,否则拒绝输入 [英] Method to find string in text file, then add in string to text file if string is not found else reject entry

查看:84
本文介绍了在文本文件中查找字符串,然后在未找到字符串的情况下将字符串添加到文本文件的方法,否则拒绝输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static void main(String[] args) throws IOException{
if (args.length == 1)
{
    BufferedReader bf = new BufferedReader (new FileReader("fruit.txt"));
    int linecount = 0;
    String line;
    //run thur the txt file to check if input exist
    while (( line = bf.readLine()) != null)
    {
        linecount++;
        int indexfound = line.indexOf(args[0]);
        if (indexfound > -1) {
            System.out.println("fruit exist on line " + linecount);
            System.out.println("add another fruit");    
            System.exit(0);
        } else {
            BufferedWriter bw = new BufferedWriter(new FileWriter("fruit.txt", true));
            String fruit = "";
            fruit = args[0];
            bw.write("\r\n" + fruit);
            System.out.println(fruit+ "added"); 
        }
    }
    f.close();
    bw.close();
}

我想让该程序在文本文件fruit.txt中进行搜索 检查其中是否已经有水果.

I want to have the program to search in a text file fruit.txt check if a fruit already exist in it.

如果存在水果,则提示用户再输入1

if fruit exist then prompt user to input another 1

其他 添加到文本文件的下一行

else add in to the nextline of the text file

这是我到目前为止所得到的. 但我不确定为什么不是我想要的.

This is what I got so far. but I am not sure why it isnt what I wanted.

在我的文本文件中,它以3个水果开头

in my text file it started with 3 fruit

apple
orange
pear

添加浆果后

apple
orange
pear
berry
berry

我加瓜后

apple
orange
pear
berry
berry
melon
melon
melon
melon

推荐答案

您只是在第一行中检查水果,如果找不到,您将继续添加它.

You are just checking for the fruit in the first line, and if it is not found, you are continuing to add it.

您需要首先完整地读取文件,以检查每一行是否包含您的水果,然后,如果其中不包含水果,则只需将其转储到其中即可.如果包含,则拒绝它.

You need to read your file completely first, one for checking each line, that it contains your fruit or not, and then if it does not contain, just dump that fruit in it. And if it contains, reject it.

因此,您需要将其他部分移到外面.而不是在找到水果时执行System.exit(),而是可以将布尔变量设置为true,然后根据布尔变量的值来决定是否添加水果.

So, in your while, you need to move that else part outside. And rather than doing System.exit() when fruit is found, you can set a boolean variable to true, and then later on, based on the value of boolean variable, you can decide whether to add the fruit or not.

boolean found = false;
while (( line = bf.readLine()) != null) {

    linecount++;
    int indexfound = line.indexOf(args[0]);
    if (indexfound > -1) {
        System.out.println("fruit exist on line " + linecount);
        System.out.println("add another fruit");   
        found = true;
        break;
    }
}

if (!found) {

    BufferedWriter bw = new BufferedWriter(new FileWriter("fruit.txt", true));
    String fruit = "";
    fruit = args[0];

    bw.write("\r\n" + fruit);
    System.out.println(fruit+ "added"); 

    bw.close();  // You need to close it here only. 
}

bf.close();                     

这篇关于在文本文件中查找字符串,然后在未找到字符串的情况下将字符串添加到文本文件的方法,否则拒绝输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆