java IO文件操作的奇怪问题

Satiini 2007-10-19 03:31:59
import java.io.*;

public class FileTest implements Runnable{

private static String constStr = "hello";
private static int count = 0;
private String id = "";

public FileTest(String id){
this.id = id;
count++;
}

public static void main(String[] args) {
for (int i=0;i<10;i++){
FileTest ft = new FileTest("my id is " + i);
Thread t = new Thread(ft);
t.start();
}

}

public void run() {
try {
FileOutputStream fos = new FileOutputStream("test.txt");
DataOutputStream dos = new DataOutputStream(fos);
dos.writeUTF(this.id + ", and count is " + count + ", and constStr is " + constStr);
//Thread.sleep(1000);
FileInputStream fis = new FileInputStream("test.txt");
DataInputStream dis = new DataInputStream(fis);
String str = dis.readUTF();//read初始时指在文件开始出,每读一次往后移动一个UTF
System.out.println(str);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

输出是:
my id is 0, and count is 7, and constStr is hello
my id is 2, and count is 10, and constStr is hello
my id is 9, and count is 10, and constStr is hello
my id is 8, and count is 10, and constStr is hello
my id is 5, and count is 10, and constStr is hello
my id is 5, and count is 10, and constStr is hello
java.io.EOFException
at java.io.DataInputStream.readFully(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at FileTest.run(FileTest.java:31)
at java.lang.Thread.run(Unknown Source)
java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at FileTest.run(FileTest.java:31)
at java.lang.Thread.run(Unknown Source)
java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at FileTest.run(FileTest.java:31)
at java.lang.Thread.run(Unknown Source)
java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at FileTest.run(FileTest.java:31)
at java.lang.Thread.run(Unknown Source)

请问各位大虾为何会有异常呢?
...全文
724 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ilk1983 2007-10-24
  • 打赏
  • 举报
回复
每次运行的结果不一样啊
Satiini 2007-10-20
  • 打赏
  • 举报
回复
即使按照sharpyuce所说的及时关闭文件,在我多次运行时依然会出现上述异常!!!
sharpyuce 2007-10-19
  • 打赏
  • 举报
回复
文件和门一样,有开有关,再开不难!
sharpyuce 2007-10-19
  • 打赏
  • 举报
回复

import java.io.*;

public class FileTest implements Runnable {

private static String constStr = "hello";
private static int count = 0;
private String id = "";

public FileTest(String id) {
this.id = id;
count++;
}

public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
FileTest ft = new FileTest("my id is " + i);
Thread t = new Thread(ft);
t.start();
}

}

public void run() {
try {
FileOutputStream fos = new FileOutputStream("test.txt");
DataOutputStream dos = new DataOutputStream(fos);
dos.writeUTF(this.id + ", and count is " + count
+ ", and constStr is " + constStr);
// Thread.sleep(1000);
dos.close();
fos.close();
FileInputStream fis = new FileInputStream("test.txt");
DataInputStream dis = new DataInputStream(fis);
String str = dis.readUTF();// read初始时指在文件开始出,每读一次往后移动一个UTF
System.out.println(str);
dis.close();
dis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

output:
my id is 0, and count is 5, and constStr is hello
my id is 1, and count is 5, and constStr is hello
my id is 2, and count is 5, and constStr is hello
my id is 3, and count is 5, and constStr is hello
my id is 4, and count is 6, and constStr is hello
my id is 5, and count is 10, and constStr is hello
my id is 6, and count is 10, and constStr is hello
my id is 7, and count is 10, and constStr is hello
my id is 8, and count is 10, and constStr is hello
my id is 9, and count is 10, and constStr is hello
sharpyuce 2007-10-19
  • 打赏
  • 举报
回复

import java.io.*;

public class FileTest implements Runnable {

private static String constStr = "hello";
private static int count = 0;
private String id = "";

public FileTest(String id) {
this.id = id;
count++;
}

public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
FileTest ft = new FileTest("my id is " + i);
Thread t = new Thread(ft);
t.start();
}

}

public void run() {
try {
FileOutputStream fos = new FileOutputStream("test.txt");
DataOutputStream dos = new DataOutputStream(fos);
dos.writeUTF(this.id + ", and count is " + count
+ ", and constStr is " + constStr);
// Thread.sleep(1000);
dos.close();
fos.close();
FileInputStream fis = new FileInputStream("test.txt");
DataInputStream dis = new DataInputStream(fis);
String str = dis.readUTF();// read初始时指在文件开始出,每读一次往后移动一个UTF
System.out.println(str);
dis.close();
dis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

哥们文件和门一样打开了就要关~~

output:
my id is 0, and count is 7, and constStr is hello
my id is 1, and count is 7, and constStr is hello
my id is 2, and count is 7, and constStr is hello
my id is 3, and count is 7, and constStr is hello
my id is 4, and count is 7, and constStr is hello
my id is 5, and count is 7, and constStr is hello
my id is 6, and count is 8, and constStr is hello
my id is 7, and count is 10, and constStr is hello
my id is 8, and count is 10, and constStr is hello
my id is 9, and count is 10, and constStr is hello
Satiini 2007-10-19
  • 打赏
  • 举报
回复
就算加上fos.flush();fos.close();
也会有异常阿!
hexizheng 2007-10-19
  • 打赏
  • 举报
回复

dos.writeUTF(this.id + ", and count is " + count + ", and constStr is " + constStr);
后面加个fos.flush();fos.close();
上面更本就没有把东西输出到文件中,下次打开的时候文件还是空的呢,这时读取当然是EOFException了
光辉岁月 2007-10-19
  • 打赏
  • 举报
回复
发现很多人问问题很积极,不过评分,可没什么动静哦?
光辉岁月 2007-10-19
  • 打赏
  • 举报
回复

多线程的执行特征啊,就是时间片轮转,轮流执行啊,不过你的程序错误与这个好象没什么直接联系,但是你的程序中读文件的时候遇到文件结尾了你还继续读,但是你没处理啊,
java.io.EOFException
这个就是很好的提示了,运行时的异常,
光辉岁月 2007-10-19
  • 打赏
  • 举报
回复

多线程的执行特征啊,就是时间片轮转,轮流执行啊,不过你的程序错误与这个好象没什么直接联系,但是你的程序中读文件的时候遇到文件结尾了你还继续读,但是你没处理啊,
java.io.EOFException
这个就是很好的提示了,运行时的异常,
「已注销」 2007-10-19
  • 打赏
  • 举报
回复
应该是多线程冲突了吧
Satiini 2007-10-19
  • 打赏
  • 举报
回复
不要沉啊,各位达人情不吝赐教。

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧