Java - 将文本分成 n 字节的块

Java - Divide text into chunks of n-bytes

所以我有点卡在必须读取文本文件的程序中,使用 FileInputStream 并将该文本分成 n 字节块。我必须发出一个 System.out.write();调用每个块,所以我想知道是否有一种简单的方法可以做到这一点。谢谢!

package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
import java.io.*;
import java.util.Scanner;


public class Test {

public static void main(String[] args) {

    int size=0;

    FileInputStream fstream = null;
    Scanner console = new Scanner(System.in);
    String inputFile = console.next();

    System.out.println("Chunk size?");
    Scanner in = new Scanner(System.in);
    size = in.nextInt();

    try {
        fstream = new FileInputStream(inputFile);

        System.out.println("Size in bytes : "
                + fstream.available());

        int content;

        while ((content = fstream.read()) != -1) {
            //System.out.write(); for every chunk of *size* bytes

        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fstream != null)
                fstream.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
 }
}
  1. 使用

    byte[] bytes = new byte[size];

    int byteCount;

    while((byteCount = fstream.read(bytes)) != -1) ...

  2. 您可以这样简单地创建一个字符串:

    新字符串(bytearray);

PS。抱歉缺少代码突出显示,由于某种原因无法正常工作...