Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的程序没有';不要打开文本文件来读取它_Java - Fatal编程技术网

Java 我的程序没有';不要打开文本文件来读取它

Java 我的程序没有';不要打开文本文件来读取它,java,Java,course.txt文件未被我的代码读取。它允许我输入文件名,但不打开文件 package javaexam; import java.io.*; import java.util.ArrayList; import java.util.HashMap; import java.io.BufferedReader; import java.util.Scanner; public class BufferReader { public static void main(String[

course.txt
文件未被我的代码读取。它允许我输入文件名,但不打开文件

package javaexam;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.io.BufferedReader;
import java.util.Scanner;

public class BufferReader {
    public static void main(String[] args)  {
        Scanner scanner=new Scanner(System.in);
        BufferedReader bf = null; // shows warning that assigned but never used
        String line;
        System.out.println("Please enter the file name");      
        try { 
            bf = new BufferedReader(new FileReader("C:\\Users\\MohammedArfa\\Desktop\\New folder\\" + scanner.next()));
        } catch(FileNotFoundException fnfex) {
            //shows warning that the buffer assignment is declared but never used
            System.out.println(fnfex.getMessage()+"The file was not found"); 
        }
        System.exit(0);
        try {
            while((line=bf.readLine()) != null) {
                System.out.println(line);
            }
        } catch(IOException ex) {
            System.out.println(ex.getMessage()+"Error reading file");
        } finally {
            System.out.println(0);
        }
    }
}

将system.exit(0)移动到上面的catch语句中

 try { 
     bf = new BufferedReader(new        FileReader("C:\\Users\\MohammedArfa\\Desktop\\New folder\\" + scanner.next()));
 } catch(FileNotFoundException fnfex) {
      //shows warning that the buffer assignment is declared but never used
      System.out.println(fnfex.getMessage()+"The file was not found"); 
      System.exit(0);
 }

如果system.exit(0)不在catch内,那么它总是被执行,从而在到达打印输出循环之前终止程序

你有什么错误吗?你真的应该在你的代码格式上下功夫。然后您会看到这是非常错误的
System.exit(0)在您的代码中。下次要多加小心。投票关闭,因为看起来像是一个“大”打字错误(因为这
System.exit(0)
看起来不需要)。没有错误,但不显示文件内容