读取数据文件地址(Java) [英] Read Data File address (Java)
本文介绍了读取数据文件地址(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我具有用于读取数据的这段代码,并且可以正常工作,但是我想更改从中读取数据的起点-我的DataFile.txt是"abcdefghi" 输出是
I have this code for reading data and works fine but I want to change the start point that the data is read from - My DataFile.txt is "abcdefghi" and the output is
1)97
2)98
3)99
4)100
我想从第二个字节开始,所以输出应该是
I want to start at the second byte so the output would be
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
个字节,如下所示.
You can simple ignore the first n
bytes as follows.
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);
}
编辑:如果要访问文件中的随机位置,则需要使用此.
Edit: If you want to access a random location in file, then you need to use RandomAccessFile. See this for detailed examples.
相关的SO帖子:
- How do I fetch specific bytes from a file knowing the offset and length?
- How to read a file from a certain offset
这篇关于读取数据文件地址(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文