gpt4 book ai didi

java - 无法在java中关闭我的DataInputStream和DataOutputStream

转载 作者:行者123 更新时间:2023-12-01 20:18:20 26 4
gpt4 key购买 nike

我最近开始学习java中的文件处理。但是,在这段代码(如下)中,我试图在所有读写结束时关闭文件,但这样做时遇到错误。

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();
}
}

我正在尝试使用 finally 关键字,但是您能告诉我为什么当我在那里写入时,我的 IDE 无法识别读取写入吗?

write cannot be resolved

最佳答案

你的读写字段对于try block 来说是本地的,finally无法访问then。在try之外初始化它。

关于java - 无法在java中关闭我的DataInputStream和DataOutputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45309197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com