当前位置: 代码迷 >> J2SE >> java IO文件操作的奇怪有关问题
  详细解决方案

java IO文件操作的奇怪有关问题

热度:232   发布时间:2016-04-24 13:24:07.0
java IO文件操作的奇怪问题
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)

请问各位大虾为何会有异常呢?


------解决方案--------------------

dos.writeUTF(this.id + ", and count is " + count + ", and constStr is " + constStr); 
后面加个fos.flush();fos.close();
上面更本就没有把东西输出到文件中,下次打开的时候文件还是空的呢,这时读取当然是EOFException了
------解决方案--------------------
Java code
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();        }    }}
  相关解决方案