简体   繁体   English

使用RandomAccessFile Java从数字文件读取

[英]Reading From a File of Numbers Using RandomAccessFile Java

I have an assignment to write some numbers in a text file using PrintStream and then reading from that same file using RandomAccessFile. 我的任务是使用PrintStream在文本文件中写入一些数字,然后使用RandomAccessFile从该文件中读取数据。 While the writing part works as intended, I get the following output when running my code. 虽然编写部分可以按预期工作,但是在运行代码时会得到以下输出。

 807416096
840971040
874525984
Exception in thread "main" java.io.EOFException
908080928
941635872
at java.io.RandomAccessFile.readInt(RandomAccessFile.java:776)
at Problema4.main(Problema4.java:21)
Java Result: 1

Here is the code: 这是代码:

import java.io.*;
import java.util.*;

public class Problema4 {

public static void main(String[] args) throws IOException, FileNotFoundException
{
    PrintStream ps = new PrintStream(new FileOutputStream("fisiernou.txt"));
    int i=0;
    while (i<11)
    {
        ps.print(i);
        ps.print(" ");
        i++;
    }
    ps.close();
    RandomAccessFile raf = new RandomAccessFile("fisiernou.txt", "r");
    raf.seek(0);
    //System.out.println(raf.readInt());
    while (raf.getFilePointer()<raf.length())
        System.out.println(raf.readInt());
    raf.close();
}

}

You are writing integer as strings ( ps.print(i) ). 您正在将整数写为字符串(ps.print(i))。 If you write 1, in the file you are writting the ascii character of 1. Suposse this is the unique number we write, the file then have only one byte. 如果您在文件中写入1,则您将写入1的ascii字符。假设这是我们写入的唯一数字,则文件只有一个字节。

When reading, you are using raf.readInt(). 阅读时,您正在使用raf.readInt()。 This method reads 4 bytes and convert them to an integer. 此方法读取4个字节并将其转换为整数。 If now you try to read your file, this only contains one byte (the ascii character of 1), and then you get an EOF excepcion. 如果现在尝试读取文件,则该文件仅包含一个字节(ascii字符为1),然后您将获得EOF例外。

Use the same type of method for writting and reading. 使用相同类型的方法进行书写和阅读。 You can write with FileOutputStream.write(int). 您可以使用FileOutputStream.write(int)进行编写。

RandomAccessFile.readInt() reads a binary 32-bit integer from the file. RandomAccessFile.readInt()从文件中读取一个32位二进制整数。 This means that it read 4 bytes and transform those 4 bytes into an int. 这意味着它将读取4个字节,并将这4个字节转换为一个int。 It doesn't read the string representation of an int. 它不读取int的字符串表示形式。 Read its javadoc . 阅读其javadoc

When your raf pointer is reading from the file, it is possible to hit an 'End of File' character. 当您的raf指针从文件中读取时,有可能会命中“文件结尾”字符。 From the Java API: 通过Java API:

"It is generally true of all the reading routines in this class that if end-of-file is reached before the desired number of bytes has been read, an EOFException (which is a kind of IOException) is thrown." “在此类中的所有读取例程中,通常来说,如果在读取所需的字节数之前已到达文件末尾,则会引发EOFException(这是IOException的一种)。”

http://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html http://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM