0

对不起这个愚蠢的问题。但我真的无法找到我做错的地方。请帮忙。我正在尝试使用 JSON 解析文件。文件也存在于系统中。但它显示 filenotfound 异常。这真的很令人沮丧。

我的代码片段如下:

        System.out.println("Please provide JSON file path : ");
        filePathJson = "\"D:\\files\\test.xlsx\"";
                //in.nextLine();

        System.out.println("Please provide Excel file path : ");
        filePathExcel = in.nextLine();

        Object obj = parser.parse(new FileReader(filePathJson));
        System.out.println("hii");

        JSONArray array = new JSONArray();

我得到的错误:

Please provide JSON file path : 
Please provide Excel file path : 
"D:\\files\\test1.xlsx"
java.io.FileNotFoundException: "D:\files\test.xlsx" (The filename, directory name, or volume label syntax is incorrect)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:137)
    at java.io.FileInputStream.<init>(FileInputStream.java:96)
    at java.io.FileReader.<init>(FileReader.java:58)
    at JavaJsonSplitter.main(JavaJsonSplitter.java:50)

有人可以指出我做错了什么。

请忽略一个无用的系统输出。

4

5 回答 5

4

您实际上是在文件名中加上引号。删除它们,您只需要在命令行等上使用实际引号。当您为FileReader(或任何其他需要文件名而不是命令行的方法)提供文件名时,您只需提供文件名(即使其中包含空格)。

例如,

filePathJson = "\"D:\\files\\test.xlsx\"";

变成

filePathJson = "D:\\files\\test.xlsx";
于 2013-09-24T12:08:50.513 回答
2

删除文件路径周围多余的双引号。这根本不是必需的。

filePathJson = "D:\\files\\test.xlsx";
于 2013-09-24T12:09:43.923 回答
2
filePathJson = "\"D:\\files\\test.xlsx\"";

应该像

filePathJson = "D:\\files\\test.xlsx";
于 2013-09-24T12:09:48.750 回答
0

@user2696466 -

请查看修改代码看看它是否有效

System.out.println("Please provide JSON file path : ");
    **filePathJson = "D:/files/test.xlsx/"**
            //in.nextLine();

    System.out.println("Please provide Excel file path : ");
    filePathExcel = in.nextLine();

    Object obj = parser.parse(new FileReader(filePathJson));
    System.out.println("hii");

    JSONArray array = new JSONArray();
于 2013-09-24T12:14:08.590 回答
0

线索在这里

java.io.FileNotFoundException: "D:\files\test.xlsx" (The filename, directory name, or volume label syntax is incorrect)

尝试这个

filePathJson = "D:\\files\\test.xlsx";
于 2013-09-24T12:14:16.547 回答