我有这段代码从文本文件中读取然后修改它。然后将修改后的内容写入另一个文本文件。
我在out.write(read);
得到一个空指针异常而且,并非所有的行都被写入,有些人可以帮助我。谢谢
import java.util.*;
import java.io.*;
public class File
{
BufferedReader in;
BufferedWriter out;
String read;
public File()
{
try {
in = new BufferedReader(new FileReader("myFile.txt"));
Scanner scan = new Scanner(in);
out = new BufferedWriter(new FileWriter("output.txt"));
while (scan.hasNext()) {
read = in.readLine();
//Write codes to modify file here
//___codes not written yet______//
out.write(read);
scan.next();
System.out.println("file output: " + read);
}
out.close();
in.close();
} catch (IOException e) {
System.out.println("There was a problem:" + e);
}
}
public static void main(String[] args)
{
File File = new File();
}
}
答案 0 :(得分:1)
尝试
while((read = in.readLine()) != null)
而不是
while (scan.hasNext())
答案 1 :(得分:0)
需要更改:procedure TForm1.Button1Click(Sender: TObject);
var
Stm: TMemoryStream;
begin
Stm := TMemoryStream.Create;
try
Memo.Lines.SaveToStream(Stm);
Stm.Position := 0;
Memo.Clear;
Edit.Text := IntToStr(Integer(@Stm));
except
Stm.Free;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
PStm: ^TMemoryStream;
begin
PStm := Pointer(StrToInt(Edit.Text));
try
Memo.Lines.LoadFromStream(PStm^); // <--- Access Violation
finally
PStm^.Free;
end;
end;