【问题标题】:Program that reads and writes to a txt/log file读取和写入 txt/log 文件的程序
【发布时间】:2018-03-15 22:19:34
【问题描述】:

我真的是编程新手,我有基本的知识。

我正在尝试制作一个程序:

  1. 读取已有文件的路径
  2. 读取我想从现有文件中提取的字符串。
  3. 读取创建新文件的路径。
  4. 使用从点 3 开始的指定路径创建一个文件。其中包含从点 2 选择的单词。

例如,如果我有一个 log/txt 文件。从中我想提取我在第 2 点从键盘读取的所有字符串。

在此之后,我希望将所有这些字符串写入一个新的 log/txt 文件中。

我希望你明白我想要做什么。

这是我到现在为止写的,但我有点卡在想法上,因为我还在学习编码语法,所以很难实现它。

package com.kwextractor;
import java.io.*;
import java.util.Scanner;


public class Main {
    public static String inputFilePath;
    public static String outputFilePath;
    public static String textToGet;


        public static void main(String[] args) throws IOException {
        Scanner filePathIn = new Scanner(System.in);
        Scanner inputText = new Scanner(System.in);
        Scanner filePathOut = new Scanner(System.in);

        System.out.println("Enter file path:");
        inputFilePath = filePathIn.nextLine();

        System.out.println("Enter the string you want to extract:");
        textToGet = inputText.nextLine();

        System.out.println("Enter the path of the exported file:");
        outputFilePath = filePathOut.nextLine();

        File inputFile = new File(inputFilePath +".txt");

        BufferedReader inputReader = new BufferedReader(new FileReader(inputFile));
        String nextLine;
        while ((nextLine = inputReader.readLine()) != null)
        {
            if (nextLine.equals(textToGet)) {

                BufferedWriter writer = null;
                try {
                    File outputFile = new File(outputFilePath+".txt");
                    System.out.println(outputFile.getCanonicalPath());
                    writer = new BufferedWriter(new FileWriter(outputFile));
                    writer.write(textToGet);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        // Close the writer regardless of what happens...
                        writer.close();
                    } catch (Exception e) {
                    }
                }
            }
        }
    }
}

【问题讨论】:

  • 如果您一直受困于想法,那么您想要做的最后件事就是向我们提出一个不完整的问题。花点时间回到白板上,具体规划出你想用你的程序完成什么。然后朝着完成的方向努力。当您在某个特定部分陷入困境时,请回来询问我们。到时候你会更好的装备它。
  • 出于教育目的,这段代码已经是一项艰巨的工作......尝试 - 真正 - 理解代码,例如异常处理本身已经很重要了,因为那里有很多代码没有足够的异常处理......大多数时候它看起来像 `catch (Exception e) { e.printStackTrace(); }` 甚至更好的//TODO,还有流、缓冲区,甚至没有理由将变量公开,也没有理由在您的代码中将其设为静态。 (我希望这些是你学习编程的足够“想法”)
  • @makoto 我在这里没有看到任何不完整的问题。我准确地解释了我想要创建的内容,并添加了代码,希望有人会尝试解释我做错了什么,因为当然程序没有运行,也许有人可以纠正我。
  • 是的,您希望我们解释如何创建整个应用程序这一事实使您的问题“不完整”充其量。你不能只指望我们在这里这样做。我们可以在零件方面为您提供帮助,但您必须付出努力。我强烈建议您按照我上面的建议进行练习。
  • @moneydhaze 我会得到所有这些提示并尝试应用它们。谢谢

标签: java file filewriter file-read


【解决方案1】:
  1. 做 4 个较小的项目,每个项目执行您列出的步骤之一。
  2. 使用硬编码路径而不是从控制台输入重新执行此项目,以避免一些复杂性。
  3. 一旦 #2 工作,我将添加控制台输入,但使用 StringUtils.isBlank 添加围绕输入的检查以处理错误输入。

简而言之,你试图一次做太多事情。编程很像一个数学方程式,一次只需要一个未知数。

【讨论】:

  • 这是个好主意。首先,我想创建不同的类,然后在 main 函数中调用它们。但我会试试你说的这件事,谢谢!
【解决方案2】:

所以伙计们.. 我试图重新设计程序并搜索更多关于如何完成我想做的事情,我卡住的地方是我需要创建文件并写入文件的部分。 如果有人能告诉我在需要写入新文件的部分我做错了什么,我将不胜感激。 这是我重新制作的代码:

    package com.readtxtfile;
import java.io.*;
import java.util.Scanner;

public class Main {
    private static String inputFilePath;
    private static String  textToGet;
    private static void readInputFile()
    {
        String line1 = null;

        Scanner input1 = new Scanner(System.in);
        System.out.println("Insert the input file path:");
        inputFilePath = input1.nextLine();


        try {
            FileReader readInput = new FileReader(inputFilePath+".txt");
            BufferedReader bufferedInput = new BufferedReader(readInput);
            line1 = bufferedInput.readLine();
            if (line1 != null)
                System.out.println("FILE LOADED SUCCESFULLY");

            bufferedInput.close();

        } catch (FileNotFoundException ex) {
            System.out.println(
                    "Unable to open file '" + inputFilePath + "'");
        } catch (IOException ex) {
            System.out.println(
                    "Error reading file '" + inputFilePath + "'");
        }
    }
    private static void textSearch()
    {
        Scanner input2 = new Scanner(System.in);
        System.out.println("Insert the text you want to extract:");
        textToGet = input2.nextLine();
    }
    private static void writeOutputFile()
    {

        Scanner input3 = new Scanner(System.in);
        System.out.println("Insert the output file path:");
        String outputFilePath = input3.nextLine();
            Scanner input4 = new Scanner(inputFilePath);

            int lineNumber = 0;
            while (input4.hasNextLine()) {
                String line2 = input4.nextLine();
                lineNumber++;
                if(textToGet.equals(line2))
                {
                    File output_file = new File(outputFilePath +".txt");
                    FileWriter writeOutput = null;
                    BufferedWriter bufferedInput = new BufferedWriter(writeOutput);
                    try {
                        writeOutput = new FileWriter(output_file +".txt");
                        writeOutput.write(line2);
                    } catch (IOException e)
                    {
                        e.printStackTrace();
                    }

                }
                }
            }
    public static void main(String[] args)
    {
     readInputFile();
     textSearch();
     writeOutputFile();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 2013-02-02
    • 2021-07-15
    • 1970-01-01
    相关资源
    最近更新 更多