gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:46:05 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com