无法写入 java txt 文件
Can not write to java txt file
我目前正在使用 blueJ 学习 java 我有一个作业,我必须写入一个 txt 文件,检查该文件是否存在并读取该文件。我的代码在下面,可以编译但是当我尝试 运行 write() 方法时,我收到以下错误 java.lang.nullpointerexception;
我不知道我哪里出错了,在这个阶段它开始让我抓狂。
import java.io.*;
public class ReadWrite
{
// instance variables - replace the example below with your own
private String file;
private String text;
/**
* Constructor for objects of class ReadWrite
*/
public ReadWrite(String file, String text)
{
// initialise instance variables
file=this.file;
text=this.text;
}
public void write()
{
try{
FileWriter writer = new FileWriter(file);
writer.write(text);
writer.write('\n');
writer.close();
}
catch(IOException e)
{
System.out.print(e);
}
}
public boolean writeToFile()
{
boolean ok;
try{
FileWriter writer = new FileWriter(file);
{
write();
}
ok=true;
}
catch(IOException e) {
ok=false;
}
return ok;
}
public void read(String fileToRead)
{
try {
BufferedReader reader = new BufferedReader(new FileReader(fileToRead));
String line = reader.readLine();
while(line != null) {
System.out.println(line);
line = reader.readLine();
}
reader.close();
}
catch(FileNotFoundException e) {
}
catch(IOException e) {
}
}
}
您的构造函数正在反向分配值。目前你有
public ReadWrite(String file, String text)
{
// initialise instance variables
file=this.file;
text=this.text;
}
这是将传入变量file
和text
赋值给实例变量,实例变量为null。
你需要的是:
public ReadWrite(String file, String text)
{
// initialise instance variables
this.file = file;
this.text = text;
}
将来避免这种情况的一个有用方法是使您的参数成为 final
- 这意味着您不能为它们分配任何东西,并且您将在编译器中捕获它。
public ReadWrite(final String file, final String text)
{
// won't compile!
file = this.file;
text = this.text;
}
进一步的改进是使实例变量 file
和 text
final
,这意味着它们 有 被赋值。这样,您就可以使用编译器来帮助您捕获错误。
public class ReadWrite
{
private final String file;
private final String text;
public ReadWrite(final String file,
final String text)
{
this.file = file;
this.text = text;
}
// ...
}
我目前正在使用 blueJ 学习 java 我有一个作业,我必须写入一个 txt 文件,检查该文件是否存在并读取该文件。我的代码在下面,可以编译但是当我尝试 运行 write() 方法时,我收到以下错误 java.lang.nullpointerexception;
我不知道我哪里出错了,在这个阶段它开始让我抓狂。
import java.io.*;
public class ReadWrite
{
// instance variables - replace the example below with your own
private String file;
private String text;
/**
* Constructor for objects of class ReadWrite
*/
public ReadWrite(String file, String text)
{
// initialise instance variables
file=this.file;
text=this.text;
}
public void write()
{
try{
FileWriter writer = new FileWriter(file);
writer.write(text);
writer.write('\n');
writer.close();
}
catch(IOException e)
{
System.out.print(e);
}
}
public boolean writeToFile()
{
boolean ok;
try{
FileWriter writer = new FileWriter(file);
{
write();
}
ok=true;
}
catch(IOException e) {
ok=false;
}
return ok;
}
public void read(String fileToRead)
{
try {
BufferedReader reader = new BufferedReader(new FileReader(fileToRead));
String line = reader.readLine();
while(line != null) {
System.out.println(line);
line = reader.readLine();
}
reader.close();
}
catch(FileNotFoundException e) {
}
catch(IOException e) {
}
}
}
您的构造函数正在反向分配值。目前你有
public ReadWrite(String file, String text)
{
// initialise instance variables
file=this.file;
text=this.text;
}
这是将传入变量file
和text
赋值给实例变量,实例变量为null。
你需要的是:
public ReadWrite(String file, String text)
{
// initialise instance variables
this.file = file;
this.text = text;
}
将来避免这种情况的一个有用方法是使您的参数成为 final
- 这意味着您不能为它们分配任何东西,并且您将在编译器中捕获它。
public ReadWrite(final String file, final String text)
{
// won't compile!
file = this.file;
text = this.text;
}
进一步的改进是使实例变量 file
和 text
final
,这意味着它们 有 被赋值。这样,您就可以使用编译器来帮助您捕获错误。
public class ReadWrite
{
private final String file;
private final String text;
public ReadWrite(final String file,
final String text)
{
this.file = file;
this.text = text;
}
// ...
}