gpt4 book ai didi

java - 格式化程序异常处理

转载 作者:行者123 更新时间:2023-11-29 03:37:05 26 4
gpt4 key购买 nike

我遇到了我不理解的代码错误。我根据 Java 文档设置了所有内容,但显然我不理解某些东西。首先,这是代码:

import java.io.FileNotFoundException;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

public class CreateTextFile {

private Formatter output; //object used to output text to file

public void openFile() throws FileNotFoundException {
try {
output = new Formatter( "sets.txt" ) { //First Error

@Override
public String format(LogRecord record) {
throw new UnsupportedOperationException("Not supported yet.");
}


}; //open the file
} catch ( SecurityException securityException ) {
System.err.println("You do not have access to this file.");
System.exit(1); //terminate the program
} catch (FileNotFoundException fileNotFoundException) { //second error

}
}
}

第一个错误:

constructor Formatter in class Formatter cannot be applied to given types; required: no arguments found: String reason: actual and formal argument lists differ in length

我理解错误信息,但我不同意。 Formatter 肯定有一个接受单个字符串的构造函数(见下文)

第二个错误:

constructor Formatter in class Formatter cannot be applied to given types; required: no arguments found: String reason: actual and formal argument lists differ in length

我在方法声明中抛出 FileNotFoundException,正如下面的 Java 文档中所述。

这直接来自 [http://doc.java.sun.com/DocWeb/api/java.util.Formatter][1]

Formatter(String fileName) throws FileNotFoundException Constructs a new formatter with the specified file name.

The charset used is the default charset for this instance of the Java virtual machine.

The locale used is the default locale for this instance of the Java virtual machine.

fileName The name of the file to use as the destination of this formatter. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.

Throws SecurityException: If a security manager is present and checkWrite(fileName) denies write access to the file

Throws FileNotFoundException: If the given file name does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file

不确定如何清除这些错误。谁能帮忙?谢谢!

最佳答案

在您的代码中:

import java.util.logging.Formatter;

在您引用的 Javadoc 的 URL 中:

java.util.Formatter

不是同一个类

关于java - 格式化程序异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14914905/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com