java - 文件未正确读取 - 数据 I/O 流

标签 java multithreading io

我想读取文件“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

如果您想读取文件“Lab5File1.dat”并将其内容写入“test.dat”,请尝试使用以下代码(如果您使用 7th 之前的 Java,请使用 try-finally block 来正确关闭 channel ) :

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();
}

关于java - 文件未正确读取 - 数据 I/O 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29116004/

相关文章:

Java Stream API 将 lambda 表达式存储为变量

java - 如何在 Java 中使用 String Formatter 设置宽度和填充整数

c# - 串口通讯积累了大量正在运行的线程

c++ - Xcode 7 : C++ threads ERROR: Attempting to use a deleted function

java + 使用wait和notify我们会错过信号吗?

php - 从 C++ 写入文件并从 PHP 读取同一文件

char * 或 char ** 可以伪装成 FILE * 吗?

java - 使用静态变量节省数组空间

c - 在 while 循环中使用 scanf 读取带有空格的用户输入?

java - 如何获取同一索引的多个子字符串?