0
    public void read(String file_name){
    WordNode node = new WordNode();
    PhraseNode Node = new PhraseNode();
    String [] components = new String[6];
    String line = null;
    int i = 0;

    try{
        // file stream classes
        FileReader file_in = new FileReader(file_name);
        BufferedReader buffer = new BufferedReader(file_in);

        // read from file while the function doesn't return null
        while((line = buffer.readLine()) != null){
            // if the line isn't the new line character
            // then add it to the array of strings
            components[i] = line;
            ++i;

            // if i is 6 the array is full
            if(i == 6){
                // if the last element of the array is "_" then it is a phrase type
                // otherwise it is just a word
                if(components[4].equals("_")){
                    Node.set_entry(components[0],components[1]);
                    Node.set_components(components[2]);
                    insert(Node);
                }
                else{
                    node.set_entry(components[0], components[1]);
                    node.set_components(components[2],components[3],components[4]);
                    insert(node);
                }

                // reset it to fill the array again
                i = 0;
            }
        }

        // close the buffer
        buffer.close();
    }
    // required exception handling with file input
    catch(FileNotFoundException error){
        System.out.println("Unable to open file: " + file_name);
    }
    catch(IOException error2){
        System.out.println("Error reading file");
    }
}

this function is reading in lines from a file to fill a binary tree. The while loop doesn't do much except use counter variables to make sure the correct node type is added to the tree.

the file name is an argument. in main it's spelt correctly and the file is in the workspace folder. So I have no idea why FileNotFoundException is thrown.

I don't know much of Java io because we only covered java for a few weeks at the end of my cs class. Any feed back as to why this code might be throwing an exception would be greatly appreciated.

Bob
  • 21
  • 2
  • 4
    Possible duplicate of [Java new File() says FileNotFoundException but file exists](http://stackoverflow.com/questions/19307622/java-new-file-says-filenotfoundexception-but-file-exists) – Haseeb Anser Mar 11 '16 at 05:51
  • a `FileNotFoundException`, as the name suggests, means Java couldn't find the file you're telling it to read. Are you, perhaps, passing in *just* a file name, and not a full path? Or running your application in a different directory than the location of the file? – dimo414 Mar 11 '16 at 05:51
  • maybe file havn't read permission – Vaseph Mar 11 '16 at 05:53
  • When you say its in the workspace folder do you mean inside java project you have created ? Can you tell me the value of the file_name variable. Are you using a "\" before the filename ? – Som Bhattacharyya Mar 11 '16 at 06:20
  • The file name is translator_file.txt and I have it in the folder with the source code files. Where should it be or what should I do to make it possible for the file to be reached by the program? – Bob Mar 11 '16 at 06:29

2 Answers2

0

Try to use absolute path to your file. If it will works, it means that you use incorrect relative path

Oleh Kurpiak
  • 1,339
  • 14
  • 34
0

Apart from the absolute and relative path issue have a check on the forward slash or back slash in the file path location that you are passing in the program to read the file from.

raikumardipak
  • 1,461
  • 2
  • 29
  • 49
  • I only know enough io in java to set up those two classes and the examples I've seen show how to do it by passing the file name as the argument. Should the argument to the FileReader object be the path to the file instead? – Bob Mar 11 '16 at 06:32
  • Thanks for your help. I figured out what you meant. – Bob Mar 11 '16 at 08:18