Bennett
Bennett

Reputation: 317

Java Filenotfound exception being thrown

A FileNotFound Exception is being thrown for my code even though I have the file in the exact directory I stated. I have also tried ...new File("euler8.txt");... with no success. My code is as follows:

        private static void euler8() throws IOException
{   
    int current;
    int largest=0;
    int c =0;
    ArrayList<Integer> bar = new ArrayList<Integer>(0);
    File infile = new File("C:/Users/xxxxxxxx/workspace/Euler1/euler8.txt");
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(
            new FileInputStream(infile),
            Charset.forName("UTF-8")));
    try
    {
        while((c = reader.read()) != -1) 
        {
            bar.add(c);
        }
    }
    finally{reader.close();}
    for(int i=0; i<bar.size(); i++)
    {
        current = bar.get(i) * bar.get(i+1) * bar.get(i+2) * bar.get(i+3) * bar.get(i+4);
        if(largest<current)
            largest = current;
    }
}

Image of what it is doing:

http://img163.imageshack.us/img163/7017/halpbk.png

Upvotes: 0

Views: 288

Answers (4)

Theodoros Chatzigiannakis
Theodoros Chatzigiannakis

Reputation: 29213

Except for everything else that has been suggested, you could check whether you're having this issue (which we've been seeing in our lab): Files with twice the extension. In other words, make sure your euler8.txt is really called that and not euler8.txt.txt, for instance, because, with hidden extensions, the file explorer will show the first but it may not strike you as odd initially, if you don't remember that it's supposed to hide the extension.

Upvotes: 4

Patricia Shanahan
Patricia Shanahan

Reputation: 26175

The backslashes are quite unnecessary. I ran this program:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class Test {
  public static void main(String[] args) throws IOException {
    File infile = new File("C:/Users/pats/workspace/test/euler8.txt");
    BufferedReader reader = new BufferedReader(new InputStreamReader(
        new FileInputStream(infile), Charset.forName("UTF-8")));
    try {
      String s;
      while ((s = reader.readLine()) != null) {
        System.out.println(s);
      }
    } finally {
      reader.close();
    }
  }
}

which is very similar, and it printed the contents of my file.

I think you need to check both that the file exists, and that you have access to it.

Upvotes: 0

Justin Smith
Justin Smith

Reputation: 433

This code will not work if all of the directories do not yet exist as well, so I'd assume (hopefully I am correct) that you have a typo, or are missing a folder.

I usually prefer to have a java.io.File reference to the parent directory, then use it as parent in a subsequent file reference, i.e.:

File dir = new File("parentDir");
File inFile = new File(dir, "fileName");

Also, java.io.File has an exists() method that returns true or false, and its subsequent mkdir(),mkdirs(), and createNewFile() return true or false if they actually create the requested file.

That said, I modified your code to the following, and it executes on my machine; but I do not know what data you are trying to run through this.

    int current;
    int largest = 0;
    int c = 0;
    ArrayList<Integer> bar = new ArrayList<Integer>(0);
    File dir = new File("C:/Users/myuser/workspace/Euler1");
    if(!dir.exists()){
        dir.mkdirs();
    }
    File infile = new File(dir, "euler8.txt");
    if(!infile.exists()){
        infile.createNewFile();
    }
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(
            new FileInputStream(infile),
            Charset.forName("UTF-8")));
    try {
        while ((c = reader.read()) != -1) {
            bar.add(c);
        }
    } finally {
        reader.close();
    }
    for (int i = 0; i < bar.size(); i++) {
        current = bar.get(i) * bar.get(i + 1) * bar.get(i + 2) * bar.get(i + 3) * bar.get(i + 4);
        if (largest < current) {
            largest = current;
        }
    }

Upvotes: 1

dashrb
dashrb

Reputation: 980

Forward slashes work fine, and are preferred because they work on any platform (relative paths are better than absolute). Make sure your path exists as specified, and verify that you have read access on the directories leading to the file. For example, if you're running your java program as a different user, you may not have read access on the "myuser" folder.

Upvotes: 2

Related Questions