2

I'm trying to run this code that was supposed to be a cut and paste easy assignment from my teacher. However, after following the instructions I still get this error message:

"The specified file, ShapeData.txt was not found."

which refers back to line 30.

I did cut and paste the file into the same folder as all the rest, so i'm not sure why I'm still getting the error. I also read about doing something in the command line, but not sure what it is I'm suppose to do.

(Oh, and this is not a homework assignment or anything that I can turn in for a grade. It is just something we can look at to get a better understanding.)

Here is my code, or at least the first couple of line anyway.

/**
 * 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 Answers 3

3
new DataReader("ShapeData.txt");

You need to provide full path of ShapeData.txt (assuming ShapeData.txt is not in working directory of the Java process).

Sign up to request clarification or add additional context in comments.

3 Comments

I will upvote if you correct assuming ShapeData.txt is not in the same folder as your class file).. The file would need to be in the working directory of the Java process, whatever that may be (will be different depending on how the JVM was launched).
@Perception: Make sense, Changed words.
thanks, i used a little from everyone and figured it out, thanks i had to move the ShapeData.txt file out of my class file, wow
1

ShapeData.txt file must be in your working directory ,because here you not specified full path. working directory may be your java bin directory

3 Comments

This is simply incorrect. Placing the file in the (I assume you mean Java) bin directory does not make it available to be loaded when a full path is not specified.
Yes, the file would need to be in the working directory for the Java process, not a bin directory.
thanks, i used a little from everyone and figured it out, thanks i had to move the ShapeData.txt file out of my class file, wow
0

You could add the file's path in your ant build file to copy it in your working directory via:

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

Or try using:

File file = new File("filename");
DataReader reader = new DataReader(file.getAbsolutePath());

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.