2017-07-25 81 views
0

我最近開始學習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無法識別閱讀當我把它寫在那裏?

寫不能得到解決

+0

儘管問題得到了封閉,重複。你願意接受最有幫助的答案嗎? – dpr

回答

1

您聲明write在try塊內。它不能在finally塊內解析,因爲這是一個不同的範圍。

您需要在try塊之前聲明write,使之獲得在最後:

DataOutputStream write = null; 
try { 
... 
    write = new DataOutputStream(writing); 
... 
} finally { 
    if (write != null) { 
     write.close(); 
    } 
} 

隨着最新版本的Java,你可以/應該使用try-與資源結構,以確保正確的資源處理。有了這個,你可以省略最後塊當試塊留在JVM會照顧關閉您的資源:

try (DataOutputStream write = new DataOutputStream(writing)) { 
    ... 
} 
1

你的讀取和寫入領域是本地try塊,終於無法訪問然後在嘗試之外初始化它。

0

寫入和讀取在try塊中創建,它們的作用域僅在塊中。將聲明移至input並且它應該起作用。

1

試試看這樣的:

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); 
    DataOutputStream write = null; 
    java.io.DataInputStream read = null; 
    try { 
    File f = new File("record.txt"); 

    FileOutputStream writing = new FileOutputStream(f); 
    write = new DataOutputStream(writing); 
    write.writeUTF("What are the things that you want to do"); 

    String str; 

    FileInputStream reading = new FileInputStream(f); 
    read = new java.io.DataInputStream(reading); 
    str = read.readUTF(); 
    System.out.println(str); 
    } 
    catch(FileNotFoundException e) { 

     System.out.println("The system collapsed"); 
    } 
    finally { 
     if (write != null) 
      write.close(); // write cannot be resolved 
     if (read != null) 
      read.close(); // read cannot be resolved 
    } 

    input.close(); 
} 
} 
相關問題