与java中的异常处理相关的错误

与java中的异常处理相关的错误,java,exception,compiler-errors,Java,Exception,Compiler Errors,如何解决此错误?我曾尝试使用throws来throwFileNotFoundException但仍然存在相同的错误 编译时错误:“默认构造函数无法处理隐式超级构造函数引发的异常类型异常。必须定义显式构造函数” 代码: import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class FileOne { Scanne

如何解决此错误?我曾尝试使用
throws
throw
FileNotFoundException
但仍然存在相同的错误

编译时错误:“默认构造函数无法处理隐式超级构造函数引发的异常类型异常。必须定义显式构造函数”

代码

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class FileOne {
    
    Scanner sc = new Scanner(System.in);
    String file_name = sc.nextLine();
    File obj = new File(file_name);
    Scanner reader_obj = new Scanner(obj); // <--- error in this line
    
    public static void main(String args[]) {
        FileOne f = new FileOne();
        f.create();
        f.writeFile();
        f.readFile();

    }
    

    void create() {
        try {
            System.out.println("Enter a file name");

            if (obj.createNewFile()) {
                System.out.println("file name is" + obj.getName());
            } else {
                System.out.println("Already exists");
            }
        } catch (IOException e) {
            System.out.println("error occured while creating");
        }
    }

    //method to write in file
    void writeFile() {
        try {
            FileWriter w = new FileWriter(obj);
            w.write("Learning files now");
            w.close();
        } catch (IOException e) {
            System.out.println("Exception occured while writing a file");
        }
    }

    //method to read
    /* use the Scanner class to read the contents of the text file created */
    void readFile() {

        while (reader_obj.hasNextLine()) {
            String data = reader_obj.nextLine();
            System.out.println(data);
        }
        reader_obj.close();
    }
    
}
导入java.io.File;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.util.Scanner;
公共类FileOne{
扫描仪sc=新的扫描仪(System.in);
字符串文件_name=sc.nextLine();
File obj=新文件(文件名);

Scanner reader_obj=new Scanner(obj);//默认构造函数隐式调用的行
Scanner reader_obj=new Scanner(obj);
,可能抛出一个
FileNotFoundException
,这是一个选中的异常,必须进行处理

一种方法是显式定义无参数构造函数:

public FileOne() throws FileNotFoundException {
}

虽然,如果您要这样做,您应该考虑清楚地将成员的初始化移动到它。

将构造函数添加到下面:

public FileOne () throws FileNotFoundException {
    
}
编辑
void main()
,如下所示(您还需要从main抛出异常):


已解决错误:

  • 我使用
    throws
    main()
    reading()
    方法中抛出异常
  • 使用
    FileReader
    类从给定的输入文件读取数据
  • 最终代码

    public class FileOne {
    Scanner sc=new Scanner(System.in);
    String file_name=sc.nextLine();
     File obj=new File(file_name);
    
    
    //method for creating a file
    void create(){
        try{
     
        
        if(obj.createNewFile()){
          System.out.println("file name is"+obj.getName());
        }
        else{
            System.out.println("Already exists");
        }
    }
        catch(IOException e){
            System.out.println("error occured while creating");
        }    
    }
    //method to write in file
    void writeFile(){
     try{
         FileWriter w=new FileWriter(obj);
         w.write("Learning files now");
         w.close();
     }
     catch(IOException e){
         System.out.println("Exception occured while writing a file");
     }
    }
    void reading() throws FileNotFoundException,IOException{
        FileReader reader=new FileReader(file_name);
        int i;
        while((i=reader.read())!=-1){
            System.out.print((char)i);
        }
        reader.close();
    }
    public static void main(String args[])throws FileNotFoundException,IOException{
       FileOne f=new FileOne();
       f.create(); 
       f.writeFile();
       f.reading();  
    }
    }
    
    public class FileOne {
    Scanner sc=new Scanner(System.in);
    String file_name=sc.nextLine();
     File obj=new File(file_name);
    
    
    //method for creating a file
    void create(){
        try{
     
        
        if(obj.createNewFile()){
          System.out.println("file name is"+obj.getName());
        }
        else{
            System.out.println("Already exists");
        }
    }
        catch(IOException e){
            System.out.println("error occured while creating");
        }    
    }
    //method to write in file
    void writeFile(){
     try{
         FileWriter w=new FileWriter(obj);
         w.write("Learning files now");
         w.close();
     }
     catch(IOException e){
         System.out.println("Exception occured while writing a file");
     }
    }
    void reading() throws FileNotFoundException,IOException{
        FileReader reader=new FileReader(file_name);
        int i;
        while((i=reader.read())!=-1){
            System.out.print((char)i);
        }
        reader.close();
    }
    public static void main(String args[])throws FileNotFoundException,IOException{
       FileOne f=new FileOne();
       f.create(); 
       f.writeFile();
       f.reading();  
    }
    }