Java帮助,错误消息“找不到文件”

时间:2012-11-16 15:27:08

标签: java

我正在尝试运行这段代码,该代码应该是我老师剪切和粘贴的简单分配。但是,按照说明操作后,我仍然收到此错误消息:

  

“找不到指定的文件ShapeData.txt。”

引用第30行。

我确实将文件剪切并粘贴到与其他文件夹相同的文件夹中,因此我不确定为什么我仍然会收到错误。我还读到了在命令行中做某事,但不确定我想做什么。

(哦,这不是不是作业作业或任何我可以上等级的东西。这只是我们可以看到的东西,以便更好地理解。)

这是我的代码,或者至少是前几行。

/**
 * Concepts demonstrated:
 *  Object Inheritance
 *  Interfaces
 *  Interface Implementation
 *  Reading Data from a File
 *  Sorting an Array
 *  Manipulating Strings 
 */

import java.util.Scanner;

    /**
     * This lab demonstrates the basics of object-oriented programming.
     */
    public class Lab8 {

        private static Shape[] shapes; // An array to hold all the shape objects

        public static void main(String[] args) {
            DataReader reader = new DataReader("ShapeData.txt");// The reader is used to read data from a file


            // Display program information
            System.out.println("Ima Java Programmer");
            System.out.println("Shape Info");

            // Load data from the file
            if(reader.loadData("ShapeData.txt")) { // The filename is entered using a command-line argument
                shapes = reader.getShapeData(); // Store the arrays in the array

                // Display how many shapes were read from the file
                System.out.println("Successfully loaded " + shapes[0].getCount() + 
                                   " shapes from the selected data file!");
                displayMenu();
            }
        }

3 个答案:

答案 0 :(得分:3)

new DataReader("ShapeData.txt");

您需要提供ShapeData.txt的完整路径(假设ShapeData.txt不在Java进程的工作目录中)。

答案 1 :(得分:1)

ShapeData.txt file must be in your working directory,因为在这里你没有指定完整路径。工作目录可能是你的java bin目录

答案 2 :(得分:0)

您可以在ant build文件中添加文件路径,通过以下方式将其复制到工作目录中:

<copy todir="/path/to/copy" overwrite="false">
    <fileset dir="/source/path" />
</copy>

或尝试使用:

File file = new File("filename");
DataReader reader = new DataReader(file.getAbsolutePath());
相关问题