简体   繁体   English

读取一个2048字节的文件

[英]Read a file with 2048 bytes

Guys i have a file which has only one line. 伙计们,我有一个只有一行的文件。 The file has no encoding it is a simple text file with single line. 该文件没有编码,它是一个单行的简单文本文件。

For every 2048 byte in a line , there is new record of 151 byte (totally 13*151 byte = 1945 records + 85 byte empty space) . 对于2048 byte in a line每个2048 byte in a line ,有151 byte (totally 13*151 byte = 1945 records + 85 byte empty space)新记录151 byte (totally 13*151 byte = 1945 records + 85 byte empty space) similarly for the next 2048 bytes. 接下来的2048个字节也是如此。

What is the best file i/o to use? 什么是最好的文件I / O使用? i am thinking of reading 2048 bytes from file and storing it in an array . 我正在考虑从文件中读取2048个字节并将其存储在数组中。

while (offset < fileLength &&(numRead=in.read(recordChunks, offset,alength)) >= 0) 
{ 
}

how can i get from the read statement only 2048 bytes at a time . 我如何一次只能从read语句获得2048个字节。 i am getting IndexOutofBoundException. 我正在获取IndexOutofBoundException。

只需使用FileInputStream ,各种read方法将允许您执行所需的操作。

How about: 怎么样:

byte byte1 = dataArray[0];
byte byte2 = dataArray[1];

In my opinion, the easiest way to read data from a file is to use a BufferedReader : 我认为,从文件读取数据的最简单方法是使用BufferedReader:

try
{
    BufferedReader br = new BufferedReader(new FileReader("your_file"));

    //Read the first line
    String s = br.readLine();
}
catch(Exception e)
{
    e.printStackTrace();
}

Then just do what you want with the String you get ! 然后,只要对String进行所需的操作即可!

Hope this helps. 希望这可以帮助。

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

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