文件未被正确读取-数据 i/o 流

File is not being read properly-data i/o stream

我想读取文件 "Lab5File1.dat" 并将其内容写入 "test.dat"。

我创建了一个数组列表,因为我以后想读取不定数量的文件。

但是我收到以下错误:

java.io.EOFException
        at java.io.DataInputStream.readInt(Unknown Source)
        at FileRead$FileReadThread.run(FileRead.java:101)

我的代码如下:

public class FileRead {

     private static final String String = null;
     static String file="Lab5File1.dat";
     static String output="test.dat";
     ArrayList<Thread> threadList = new ArrayList<Thread>();
     static DataOutputStream dosZip;



   public static void main(String[] args) {
       // TODO Auto-generated method stub
       new FileRead();
   }

   public FileRead()
   {

      Thread newThread;
      try{
          dosZip = new DataOutputStream(new FileOutputStream( output ));

      }  catch(IOException fnf){
         System.out.println("Trouble creating "+output );
         fnf.printStackTrace();
         System.exit(2);
     }

     newThread = new FileReadThread("Lab5File1.dat");
     threadList.add( newThread );
     newThread.start();

     // Wait for all the threads to finish
     for( Thread th: threadList){
         try{
             th.join();
         } catch(InterruptedException ie){
             System.out.println("Thread interrupted");
         }
     }
     // flush & close the combined file
     try {  
         dosZip.flush();
         dosZip.close();
     } catch(IOException ioe){
         System.out.println("Trouble flushing and closing  file.");
         ioe.printStackTrace();
         System.exit(3);
     }

}


  public static class FileReadThread extends Thread {
     String inputFileName;

     public FileReadThread(String fileName)
     {
         inputFileName = file;         

     }

     public void run()
     {  
          InputStream is = null;
          DataInputStream dis = null;

          System.out.println("TRYING.........");

         try{

            // create input stream from file input stream
            is = new FileInputStream(inputFileName);
            // create data input stream
            dis = new DataInputStream(is);

            while ( true )
            {

                int Zip = dis.readInt();
                String City = dis.readUTF();
                String State = dis.readUTF();
                double Longitude =dis.readDouble();
                double Latitudes=dis.readDouble();
                int Zone = dis.readInt();
                int dst = dis.readInt();



                dosZip.writeInt(Zip);
                dosZip.writeUTF(City);
                dosZip.writeUTF(State);
                dosZip.writeDouble(Longitude);
                dosZip.writeDouble(Latitudes);
                dosZip.writeInt(Zone);
                dosZip.writeInt(dst);
    }

         }catch(Exception e){
                // if any I/O error occurs
                e.printStackTrace();
             }finally{

                // releases any associated system files with this stream
                if(is!=null)
                    try {
                        is.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                if(dis!=null)
                    try {
                        dis.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
             }   
  } 


 }}

在 Java 中,将一个文件的内容复制到另一个文件的最简单方法之一是使用 FileChannel class

如果你想读取文件"Lab5File1.dat"并将其内容写入"test.dat"尝试使用下面的代码(如果你使用Java早于7th,使用try-finally块而不是正确关闭频道):

try (FileChannel src = new FileInputStream("Lab5File1.dat").getChannel();
     FileChannel dest = new FileOutputStream("test.dat").getChannel()){
         dest.transferFrom(src, 0, src.size());
} catch (IOException e) {
    e.printStackTrace();
}