使用Java中的Scanner类读取.txt文件

我正在研究一个Java程序,该程序逐行读取文本文件,每个文本文件都有一个数字,将每个数字都将其扔到数组中,然后尝试使用插入排序对数组进行排序。我需要有关程序读取文本文件的帮助。


我收到以下错误消息:


java.io.FileNotFoundException: 10_Random (The system cannot find the file specified)

at java.io.FileInputStream.open(Native Method)

at java.io.FileInputStream.<init>(Unknown Source)

at java.util.Scanner.<init>(Unknown Source)

at insertionSort.main(insertionSort.java:14)

我的“ src”,“ bin”和主项目文件夹中有.txt文件的副本,但仍找不到该文件。我正在使用Eclipse。


import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;


public class insertionSort {


public static void main(String[] args) {


    File file = new File("10_Random");


    try {


        Scanner sc = new Scanner(file);


        while (sc.hasNextLine()) {

            int i = sc.nextInt();

            System.out.println(i);

        }

        sc.close();

    } 

    catch (FileNotFoundException e) {

        e.printStackTrace();

    }

 }

}


慕的地6264312
浏览 1632回答 3
3回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java