2

先生からの簡単な割り当てのカットアンドペーストであるはずのこのコードを実行しようとしています。ただし、指示に従った後でも、次のエラーメッセージが表示されます。

指定されたファイル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();
            }
        }
4

3 に答える 3

3
new DataReader("ShapeData.txt");

ShapeData.txtのフルパスを指定する必要があります(ShapeData.txtがJavaプロセスの作業ディレクトリにない場合)。

于 2012-11-16T15:28:48.733 に答える
1

ShapeData.txt file must be in your working directory、ここではフルパスを指定していないためです。作業ディレクトリはあなたのJavabinディレクトリかもしれません

于 2012-11-16T15:29:02.843 に答える
0

次の方法で、ファイルのパスをantビルドファイルに追加して、作業ディレクトリにコピーできます。

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

または、以下を使用してみてください。

File file = new File("filename");
DataReader reader = new DataReader(file.getAbsolutePath());
于 2012-11-16T15:59:40.240 に答える