有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java断言错误:违反:行。txt存在

我在Data文件夹下有一个文件lines.txt。问题是当我把lines.txt作为输入时,我得到了一个错误。错误发生在这一行SimpleReader inFile = new SimpleReader1L(inputFileName);

我知道这与路径有关,因为它没有被识别,但我没有足够的经验来修复它。有人能告诉我该怎么做吗

import components.set.Set;
import components.simplereader.SimpleReader;
import components.simplereader.SimpleReader1L;
import components.simplewriter.SimpleWriter;
import components.simplewriter.SimpleWriter1L;

public final class pp {

public static void main(String[] args) {
        SimpleReader in = new SimpleReader1L();
        SimpleWriter out = new SimpleWriter1L();
        /*
         * Get input file name
         */

        out.print("Input file (with fragments): ");

        String inputFileName = in.nextLine();
        SimpleReader inFile = new SimpleReader1L(inputFileName);

        /*
         * Get initial fragments from input file
         */
        Set<String> fragments = linesFromInput(inFile);
        /*
}
}

这是simpleReader1L

 public SimpleReader1L(String name) {
        assert name != null : "Violation of: name is not null";
        this.name = name;
        try {
            URL url = new URL(name);
            this.rep =
                    new BufferedReader(new InputStreamReader(url.openStream()));
        } catch (MalformedURLException e) {
            // throw new AssertionError("Violation of: " + url + " is valid");
            try {
                this.rep = new BufferedReader(new FileReader(name));
            } catch (FileNotFoundException ex) {
                throw new AssertionError("Violation of: " + name + " exists");
            }
        } catch (FileNotFoundException e) {
            throw new AssertionError("Violation of: " + name + " exists");
        } catch (IOException e) {
            throw new AssertionError("Violation of: " + name + " can be read");
        }
    }

共 (2) 个答案

  1. # 1 楼答案

    new SimpleReader1L("lines.txt")的调用尝试打开当前目录中的文件“lines.txt”。但该文件不在当前目录中。它位于不同的目录中

    解决方案:

    1. 使用文件的正确的相对路径名。。。相对于运行应用程序的目录1

    2. 为文件使用绝对路径名


    如果您不理解相对路径名和绝对路径名的含义或区别,请尝试阅读以下内容:


    1-确定目录是什么可能有点棘手。这取决于您如何运行应用程序,因此不需要。

  2. # 2 楼答案

    The error happens at this line SimpleReader inFile = new SimpleReader1L(inputFileName);

    目前还不清楚SimpleReader1L希望输入什么。根据文件是以文件对象还是字符串作为文件名,您需要将实际对象或文件的正确路径作为字符串传递


    使用绝对路径(从根目录开始)。e、 g

    Windows:D:\somedir\lines.txt

    Unix/Mac:/users/<username>/home/lines.txt