gpt4 book ai didi

java - 读取数据文件地址(Java)

转载 作者:行者123 更新时间:2023-12-02 12:44:12 25 4
gpt4 key购买 nike

我有这段用于读取数据的代码并且工作正常,但我想更改读取数据的起点 - 我的 DataFile.txt 是“abcdefghi”输出是

1)97                
2)98
3)99
4)100

我想从第二个字节开始,所以输出是

1)98              
2)99
3)100
4)etc

代码:

import java.io.*;
public class ReadFileDemo3 {
public static void main(String[] args)throws IOException {
MASTER MASTER = new MASTER();
MASTER.PART1();
}
}

class MASTER {
void PART1() throws IOException{
System.out.println("OK START THIS PROGRAM");
File file = new File("D://DataFile.txt");
BufferedInputStream HH = null;
int B = 0;
HH = new BufferedInputStream(new FileInputStream(file));
for (int i=0; i<4; i++) {
B = B + 1;
System.out.println(B+")"+HH.read());
}
}
}

最佳答案

您可以简单地忽略前 n 个字节,如下所示。

HH = new BufferedInputStream(new FileInputStream(file));
int B = 0;
int n = 1; // number of bytes to ignore

while(HH.available()>0) {
// read the byte and convert the integer to character
char c = (char)HH.read();
if(B<n){
continue;
}
B++;
System.out.println(B + ")" + (int)c);
}
<小时/>

编辑:如果您想访问文件中的随机位置,则需要使用 RandomAccessFile 。请参阅this详细示例。

相关SO帖子:

关于java - 读取数据文件地址(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44833625/

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