读取文本文件的每一行并将其与用户输入进行比较

时间:2015-04-03 17:35:45

标签: java

您好我正在编写一个程序,使用逐步细化读取顺序文本文件,并在运行程序时,1)插入2)修改或3)删除用户给出的条目。目前我的问题是我需要输入一个存在的字符串(如果它没有退出),然后将它与"输出"进行比较。 (或修改)字符串来修改它。

import java.io.*;
import java.nio.file.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.Scanner;

public class master2{
public static void main(String args[]) throws Exception
{
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter 1.insertion into file 2.modify into file 3.delete in file");
    int choice=sc.nextInt();
    try
    {
        File newTextFile = new File("masterfile.txt");
        File temp = File.createTempFile("duplicateMaster", ".txt", newTextFile.getParentFile());
        String charset = "UTF-8";
        if(choice==1)
        {
            System.out.println("Enter String to insert");
            String str = sc.next();
            FileWriter fw = new FileWriter("masterfile.txt", true);
            BufferedWriter bufferedWriter = new BufferedWriter(fw);
            Scanner fileSC = new Scanner(newTextFile);
            int count = 0;
            while(fileSC.hasNextLine())
            {
                String line = fileSC.nextLine();
                System.out.print("Line " + count + " " );
                count++;
                if (line.compareTo(str)==0)
                {
                    System.out.println("Data already exists!");
                    System.exit(0);
                }
            }
            fw.append("\n" + str);
            System.out.println("Entry added!");
            fw.close();
            fileSC.close();
            System.exit(0);
        }
        if(choice==2){
            Path path = Paths.get("masterfile.txt");
            String content = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
            System.out.println("Enter String you want to modify which is present in text file");
            String input = sc.next();
            System.out.println("Enter new modified string");
            String output = sc.next();
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(newTextFile), charset));
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(temp), charset));
            Scanner fileSC = new Scanner(newTextFile);
            int count = 0;
            while(fileSC.hasNextLine())
            {
                String line = fileSC.nextLine();
                System.out.print("Line " + count + " " );
                count++;
                if (line.contains(input))
                {
                    content = line.replace(input, output);
                    writer.write(content);
                }
            }
            newTextFile.delete();
            temp.renameTo(newTextFile);
            System.out.print("Modified");
            reader.close();
            writer.close();
        }
        if(choice==3)
        {
            String delete = "foo";
            System.out.println("Enter string to delete");
            delete = sc.nextLine();
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(newTextFile), charset));
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(temp), charset));
            for (String line; (line = reader.readLine()) != null;) {
                line = line.replace(delete, "");
                writer.println(line);
                temp.renameTo(newTextFile);
                temp.delete();

            }
        }
    }catch (Exception e)
    {
        System.out.println(e);
    }
    }
}

1 个答案:

答案 0 :(得分:1)

我发现了一些问题。

1)您需要为临时文件创建FileWriter,而不是masterfile.txt。

2)如果扫描仪中的字符串与用户的输入不匹配,您仍然需要将其写入临时文件,事实上您甚至不需要进行比较,让String.replace()做它适合你

while(fileSC.hasNextLine()) {
  String line = fileSC.nextLine();
  System.out.print("Line " + count + " " );
  count++;
  line = line.replace(input, output);
  bufferedWriter.write(line);
}

3)找到匹配后,您不能只System.exit(0)。那文件的其余部分呢?此外,您应该关闭Scanner和FileWriter。