如何正确创建将.txt文件写入Java应用程序的Writer对象?

如何正确创建将.txt文件写入Java应用程序的Writer对象?,java,file,Java,File,试图在Java应用程序中创建Writer对象(必须创建.txt文件)时遇到以下问题。因此,我试图以这种方式创建它: private Writer writer = new BufferedWriter(new FileWriter("thefile.txt")); 问题是Eclipe给了我以下语法错误: Multiple markers at this line - The value of the field DatiPianiInterventiRowCallbackHandler

试图在Java应用程序中创建Writer对象(必须创建.txt文件)时遇到以下问题。因此,我试图以这种方式创建它:

private Writer writer = new BufferedWriter(new FileWriter("thefile.txt"));
问题是Eclipe给了我以下语法错误:

Multiple markers at this line
    - The value of the field DatiPianiInterventiRowCallbackHandler.writer is not used
    - Default constructor cannot handle exception type IOException thrown by implicit super constructor. Must define an explicit 
     constructor

为什么??我错过了什么?如何解决此问题并正确创建一个新的Writer对象来编写文本文件?

由于您的错误,您创建的Writer是正确的,但不在正确的位置。您似乎是作为类的成员创建它,它应该在方法中创建(并关闭)。作为类的成员,此代码可以抛出IOException,这是导致第二个错误的原因(第一个错误只是警告您不要对writer执行任何操作)


FileWriter构造函数引发已检查的异常。您应该在方法中实例化对象并使用try-catch块,或者指定您的方法引发相同的异常

private Writer writer;
然后在一种方法中你做到了

writer=new BufferedWriter(new FileWriter("thefile.txt"));

但是,它必须被try-catch包围,或者您的方法必须声明相同的异常。这是处理选中异常的规则。

private Writer Writer=new BufferedWriter(new FileWriter(“thefile.txt”)

相当于

private Writer writer;

public MyClass() throws IOException {
    super();
    this.writer = new BufferedWriter(new FileWriter("thefile.txt"));
}

但后者确实有效,因为它正确地声明了选中的异常。

“未使用字段DatiPianiInterfertiRowCallbackHandler.writer的值”不是错误,而是警告。显然,您从来没有使用您创建的writer,这几乎从来都不是您真正想要的。您将其写入Java应用程序是什么意思?来自Eclipse的消息非常清楚。你对他们有什么不了解?
private Writer writer;