【问题标题】:File is not being read properly-data i/o stream未正确读取文件 - 数据 i/o 流
【发布时间】:2015-03-18 07:06:42
【问题描述】:

我想读取文件“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();
                    }
             }   
  } 


 }}

【问题讨论】:

  • 问题是while ( true ),到达文件末尾需要停止。 DataInputStream主要用于Sockets,读取文件,最好使用BufferedReaderScanner
  • 好吧,你不是在检查你是否已经到达文件的末尾,你只是继续循环。
  • 我同意 Titus 的观点,即“while true”循环是问题所在 - 但我不同意他使用 BufferedReader 或扫描仪的建议,除非这实际上是一个文本文件.如果你只是想复制一个文件,为什么不使用Files.copy
  • 谢谢!循环是问题

标签: java multithreading io


【解决方案1】:

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

如果您想读取文件“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();
}

【讨论】:

    猜你喜欢
    • 2012-07-03
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 2020-06-26
    • 2023-03-08
    • 2011-06-10
    • 2018-04-27
    • 1970-01-01
    相关资源
    最近更新 更多