程序无法找到文件

时间:2014-03-15 13:39:34

标签: java file-io

我在主类的同一个文件夹中有一个名为edges的文件,但是当我运行它时,它说有一个错误。

线程中的异常" main" java.io.FileNotFoundException:

我应该为该计划更改什么?

import graphs.arrayGraph;
import java.io.File;
import java.io.FileReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;

class underground 
{
static int N = 308;
static double [][] edges = new double [N][N];
static String [] stationNames = new String[N];

static ArrayList<String> convert (ArrayList<Integer> m)
{
    ArrayList<String> z = new ArrayList<String> ();
    for (Integer i:m) z.add(stationNames[i]);
    return z;
}

static HashSet<ArrayList<String>> convert (HashSet<ArrayList<Integer>> paths)
{
    HashSet <ArrayList <String>> k = new HashSet <ArrayList <String>> ();
    for (ArrayList <Integer> p:paths) k.add(convert(p));
    return k;
}

public static void main (String[] args) throws Exception
{
    for(int i=0; i<N; i++) for (int j=0; j<N; j++) edges[i][j]=0.0;

    URL url=    underground.class.getResource("edges");

    Scanner s = new Scanner (new FileReader(url.getPath()));
    String z = s.nextLine();
    while (s.hasNext())
    {
        z = s.nextLine();

        String[] results = z.split(",");
        edges[Integer.parseInt(results[0])] [Integer.parseInt(results[1])]= 1.0;
        edges[Integer.parseInt(results[1])] [Integer.parseInt(results[0])]= 1.0;
    }

    url=    underground.class.getResource("stations");
    s = new Scanner(new FileReader(url.getPath()));
    z = s.nextLine();
    while (s.hasNext())
    {
        z = s.nextLine();
        String[] results = z.split(",");
        stationNames[Integer.parseInt(results[0])] = results[3];
    }

    arrayGraph G = new arrayGraph (edges);
    System.out.println(convert(G.shortestPaths(Integer.parseInt(args[0]),Integer.parseInt(args[1]))));
}

}

2 个答案:

答案 0 :(得分:1)

您还必须包含文件扩展名。有时这可能是个问题。

尝试使用edges.txt或任何扩展名。

答案 1 :(得分:0)

将InputStream传递给扫描仪。不要使用FileReader。

即改变这个:

URL url=    underground.class.getResource("edges"); // are you sure the name is correct?

Scanner s = new Scanner (new FileReader(url.getPath()));

到此:

InputStream is = underground.class.getResourceAsStream("edges");
Scanner s = new Scanner(is);