gpt4 book ai didi

java - 文件输出/输入异常

转载 作者:行者123 更新时间:2023-12-02 07:26:59 26 4
gpt4 key购买 nike

这个程序在终端窗口中给了我一个“异常”。 prevData.txt 的位置如代码所示。我的问题是什么?

我正在使用Bluej。还有其他问题吗?请询问。

import java.io.*;

public class prevDataReset{

public static void main(String args[]){

try{
byte bWrite [] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

OutputStream os = new FileOutputStream("C:/prevData.txt");
for(int x=0; x < bWrite.length ; x++){
os.write( bWrite[x] ); // writes the bytes
}
os.close();

InputStream is = new FileInputStream("C:/prevData.txt");
int size = is.available();

for(int i=0; i< size; i++){
System.out.print((double)is.read() + " ");
}
is.close();
}catch(IOException e){
System.out.print("Exception");
}
}
}

最佳答案

这对我有用。我建议您没有写入“C:\”的写入权限,请尝试使用 user.dir 系统属性...

类似...

public class SimpleTest {

public static void main(String args[]) {

OutputStream os = null;
InputStream is = null;
try {
String userDir = System.getProperty("user.dir");
byte bWrite[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

try {
os = new FileOutputStream(userDir + File.separator + "prevData.txt");
for (int x = 0; x < bWrite.length; x++) {
os.write(bWrite[x]); // writes the bytes
}
} finally {
try {
os.close();
} catch (Exception e) {
}
}

try {
is = new FileInputStream(userDir + File.separator + "prevData.txt");
int size = is.available();

for (int i = 0; i < size; i++) {
System.out.print((double) is.read() + " ");
}
} finally {
try {
is.close();
} catch (Exception e) {
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

关于java - 文件输出/输入异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13447522/

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