有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

读取数据字节,而不仅仅是偏移量处的字

从下面链接中的另一个问题来看,我正在读取一个数据文件,我想读取一个数据文件字,而不仅仅是值大于255的字节,我该怎么做?如果可能的话,还有精确的偏移量

Read Data File address (Java)

有回复使用RandomAcessFile,但我不能让它工作任何帮助

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class ReadTest {

    public static void main(String[] args)throws IOException  {   

    byte[]     magic = new byte[4];
    File file = new File("D://test.map");
    RandomAccessFile raf = new RandomAccessFile(file );
    raf.seek(0L);
    raf.readFully(magic);
    System.out.println(new String(magic));        
    }
}

error line 13 "RandomAccessFile raf = new RandomAccessFile(file );"


共 (1) 个答案

  1. # 1 楼答案

    您不能直接从文件中读取一个单词,而不是执行此操作。您可以读取文件的所有数据并将其存储在字符串缓冲区中,然后根据如下所示的空间将其拆分

    String str="Hello my name is saurabh";
               String word[] = str.split(" ");
               for(int i=0;i<word.length;i++){
                 System.out.println(word[i]);
           }