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)请问各位大虾为何会有异常呢?

解决方案 »

  1.   


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

  2.   


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

  3.   


    dos.writeUTF(this.id + ", and count is " + count + ", and constStr is " + constStr); 
    后面加个fos.flush();fos.close();
    上面更本就没有把东西输出到文件中,下次打开的时候文件还是空的呢,这时读取当然是EOFException了
      

  4.   

    就算加上fos.flush();fos.close(); 
    也会有异常阿!
      

  5.   


    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
      

  6.   


    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
      

  7.   

    即使按照sharpyuce所说的及时关闭文件,在我多次运行时依然会出现上述异常!!!