我最近開始學習java中的文件處理。然而,在這段代碼中(下面),我試圖在所有閱讀和寫作結束時關閉文件,但是以這種方式面臨錯誤。無法在java中關閉我的DataInputStream和DataOutputStream
package trycatch;
import java.util.Scanner;
import org.omg.CORBA.DataInputStream;
import java.*;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Writer;
public class Source {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
try {
File f = new File("record.txt");
FileOutputStream writing = new FileOutputStream(f);
DataOutputStream write = new DataOutputStream(writing);
write.writeUTF("What are the things that you want to do");
String str;
FileInputStream reading = new FileInputStream(f);
java.io.DataInputStream read = new java.io.DataInputStream(reading);
str = read.readUTF();
System.out.println(str);
}
catch(FileNotFoundException e) {
System.out.println("The system collapsed");
}
finally {
write.close(); // write cannot be resolved
read.close(); // read cannot be resolved
}
input.close();
}
}
我想出來的終於關鍵字,但你能告訴我爲什麼我的IDE無法識別閱讀和寫當我把它寫在那裏?
寫不能得到解決
儘管問題得到了封閉,重複。你願意接受最有幫助的答案嗎? – dpr