[英]randomaccessfile with java
I am trying to read ints from a text file. 我正在尝试从文本文件读取整数。 This file has the following text:
该文件具有以下文本:
3 1.9 a2 8 9
1 3 5.6 xx 7
7.2 abs 7 :+ -4
5
ds ds ds
I'm using the randomAccessFile with java. 我在Java中使用randomAccessFile。 When I try to read the first int I get 857747758 (I wrote int number1 = inputStream.nextInt();, as well and others like it for the other ints on the txt file.) it told me that the length is 59. I am just wondering why its giving me this large number, and not just 3?
当我尝试读取第一个int时,我得到857747758(我写了int number1 = inputStream.nextInt();以及其他类似的txt文件上其他int的字符。)它告诉我长度为59。我只是想知道为什么它给了我这么大的数目,而不仅仅是3个? Also, where would i move the file pointer after i read int 3?
另外,读完int 3后我将文件指针移到哪里? i know that it's starts at location 0, but i just need help figuring out how file point moves.
我知道它从位置0开始,但是我只需要帮助弄清楚文件点如何移动。 Does it count empty spaces?
它算空吗? I know that it converts them into binary.
我知道它将它们转换为二进制。
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.");
}
}
getting this 得到这个
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.
I am trying to read ints from a text file.
我正在尝试从文本文件读取整数。
Then you shouldn't be using a binary API. 然后,您不应该使用二进制API。
I'm using the
RandomAccessFile
with Java.我在Java中使用
RandomAccessFile
。 When I try to read the first int I get 857747758 (I wroteint number1 = inputStream.nextInt()
当我尝试读取第一个int时,我得到857747758(我写了
int number1 = inputStream.nextInt()
No you didn't. 不,你没有。 You used
inputStream.readInt()
. 您使用了
inputStream.readInt()
。 That's a binary API. 那是一个二进制API。
readInt()
reads a 4-byte binary integer in network byte order, just like it says in the Javadoc. readInt()
以网络字节顺序读取一个4字节的二进制整数,就像Javadoc中所说的那样。
You should be using Scanner.nextInt()
. 您应该使用
Scanner.nextInt()
。
The readInt reads the 4bytes. readInt读取4个字节。 So you won't get the output u wanted.
这样您就不会得到您想要的输出。
The java api https://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html#readInt() Java API https://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html#readInt()
Reads a signed 32-bit integer from this file.
从该文件读取一个带符号的32位整数。 This method reads 4 bytes from the file, starting at the current file pointer.
从当前文件指针开始,此方法从文件读取4个字节。 If the bytes read, in order, are b1, b2, b3, and b4, where 0 <= b1, b2, b3, b4 <= 255, then the result is equal to: (b1 << 24) |
如果按顺序读取的字节是b1,b2,b3和b4,其中0 <= b1,b2,b3,b4 <= 255,则结果等于:(b1 << 24)| (b2 << 16) + (b3 << 8) + b4
(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();
}
Here is the output 这是输出
[51, 32, 49, 46]
857747758
857747758
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.