此标题已被编辑

时间:2016-12-15 10:46:39

标签: bufferedreader

我编辑了这段代码因为我无法永久删除它。我编辑了这段代码因为我无法永久删除它。我编辑了这段代码因为我无法永久删除它。我编辑了这段代码因为我无法永久删除它 .i编辑了这段代码因为我不能永久删除它

请不要重新编辑

2 个答案:

答案 0 :(得分:1)

您应该从创建图表中将文件加载到图表中。

这样的事情:

public interface GraphLoader {
    /**
     * Fill a graph with no property from a single file.
     *
     * @param graph the graph to fill
     * @param path path to graph file
     * @return a graph with no property
     */
    public static void loadSingleFile(Graph graph, Path path) {
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(path.toFile()))) {
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                String[] vertices = line.split(" ");
                if (vertices.length < 2) {
                    continue;
                }
                graph.addEdge(Integer.parseInt(vertices[0]), Integer.parseInt(vertices[1]));
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * load a graph with no property from a single file.
     *
     * @param path path to graph file
     * @return a graph with no property
     */
    public static Graph loadSingleFile(Path path) {
        Graph graph = new Graph();
        loadSingleFile(graph, path);
        return graph;
    }

    /**
     * load a graph with no property from multiple files in parallel.
     *
     * @param paths paths to graph files
     * @return a graph with no property
     */
    static Graph loadMultipleFiles(Path[] paths) {
        Graph graph = new Graph();
        for (Path path : paths) {
            loadSingleFile(graph, path);
        }
        return graph;
    }

}

答案 1 :(得分:0)

  1. 使用“试用资源”。 Try with resources documentation by Oracle

  2. 按层次顺序划分“catch”块 Advantages of exceptions by Oracle

  3. 为什么要使用界面?这是一个建筑理念吗?