繁体   English   中英

Java的randomaccessfile

[英]randomaccessfile with java

我正在尝试从文本文件读取整数。 该文件具有以下文本:

3 1.9 a2 8 9
1 3 5.6 xx 7
7.2 abs 7  :+  -4
5
ds ds ds

我在Java中使用randomAccessFile。 当我尝试读取第一个int时,我得到857747758(我写了int number1 = inputStream.nextInt();以及其他类似的txt文件上其他int的字符。)它告诉我长度为59。我只是想知道为什么它给了我这么大的数目,而不仅仅是3个? 另外,读完int 3后我将文件指针移到哪里? 我知道它从位置0开始,但是我只需要帮助弄清楚文件点如何移动。 它算空吗? 我知道它将它们转换为二进制。

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;


public class Program5 {
public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    RandomAccessFile inputStream = null;
    int n1, n2, n3, n4, n5, n6, n7, n8, n9;
    int sum = 0;

    System.out.println("Please enter the file name you wish to read from.");
    String file_name = keyboard.next();

    try {
        inputStream = new RandomAccessFile(file_name, "r");

        n1 = inputStream.readInt();
        // inputStream.seek(3);
        // n2 = inputStream.readInt();
        // n3 = inputStream.readInt();
        // n4 = inputStream.readInt();
        // n5 = inputStream.readInt();
        // n6 = inputStream.readInt();
        // n7 = inputStream.readInt();
        // n8 = inputStream.readInt();
        // n9 = inputStream.readInt();
        System.out.println(inputStream.length());
        System.out.println(inputStream.getFilePointer());
        System.out.println(n1);
        // System.out.println(n2);
        sum = n1; // n2; /* n3 + n4 + n5 + n6 + n7 + n8 + n9; */
        inputStream.close();
    } catch (FileNotFoundException e) {
        System.out.println("Problem opening up file" + file_name);
        System.exit(0);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    System.out.println("The sum of the numbers is: " + sum);

    System.out.println("End of program.");

}
 }

得到这个

 Please enter the file name you wish to read from.
 inputP5.txt
 length at 59
 file pointer at 4
 n1 =857747758
 The sum of the numbers is: 857747758
 End of program.

我正在尝试从文本文件读取整数。

然后,您不应该使用二进制API。

我在Java中使用RandomAccessFile 当我尝试读取第一个int时,我得到857747758(我写了int number1 = inputStream.nextInt()

不,你没有。 您使用了inputStream.readInt() 那是一个二进制API。 readInt()以网络字节顺序读取一个4字节的二进制整数,就像Javadoc中所说的那样。

您应该使用Scanner.nextInt()

readInt读取4个字节。 这样您就不会得到您想要的输出。

Java API https://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html#readInt()

从该文件读取一个带符号的32位整数。 从当前文件指针开始,此方法从文件读取4个字节。 如果按顺序读取的字节是b1,b2,b3和b4,其中0 <= b1,b2,b3,b4 <= 255,则结果等于:(b1 << 24)| (b2 << 16)+(b3 << 8)+ b4

try (RandomAccessFile raf = new RandomAccessFile(new File(
        "filename.txt"), "r")) {

    byte[] bt = new byte[4];
    raf.read(bt);
    System.out.println(Arrays.toString(bt));
    System.out.println((bt[0] << 24) | (bt[1] << 16) + (bt[2] << 8) + bt[3]);

    raf.seek(0);
    System.out.println(raf.readInt());
} catch (IOException e) {
    e.printStackTrace();
}

这是输出

[51, 32, 49, 46]
857747758
857747758

暂无
暂无

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

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