有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JAVAutil。扫描器java。伊奥。FileNotFoundException,错误是什么?

我试图读取一个文件并打印出每行的子字符串。我想不出我的错误是什么。我的链接有效,那么是什么导致了错误

import java.io.File; import java.io.FileNotFoundException; import java.util.*; public class FileReader { public void fileReader() { File file = newFile("https://www.cs.uoregon.edu/Classes/14F/cis212/assignments/phonebook.txt"); try{ Scanner scan = new Scanner(file); while(scan.hasNextLine()) { String numAndName = scan.nextLine(); String newNum = numAndName.substring(0, 8); System.out.println(newNum); } scan.close(); } catch(FileNotFoundException e) { e.printStackTrace(); } } }

共 (1) 个答案

  1. # 1 楼答案

    new File("https://www.cs.uoregon.edu/Classes/14F/cis212/assignments/phonebook.txt");
    

    问题是文件名。这不是一个文件名,而是一个URL,它不是指一个文件,而是指一个HTTP资源。将其删除,然后更改以下内容:

    Scanner scan = new Scanner(file);
    

    为此:

    Scanner scan = new Scanner(new URL("https://www.cs.uoregon.edu/Classes/14F/cis212/assignments/phonebook.txt").openStream());
    

    E&;OE