Othmane
Othmane

Reputation: 1124

Split an array of Bytes previously read from a file

I have a recursive function, which takes as an argument an array of Bytes (read from a file) and then tries to split it into two parts for recursion calls.

The file format is for example like this:

word1 word2
word3 word4
   ....
word97 word98
word99 word100

So, to read it I use this code:

byte[] content = new byte[(int) file.length()];
FileInputStream fileInputStream = null;

fileInputStream = new FileInputStream(file);
fileInputStream.read(content);

But then I would like to Split the content array into two parts that each would represent the half of the original file. For example if the original file contains four lines, then the firstHalf array would contain the first two lines and the secondHalf array the last two lines of the original file.

I used this:

int halfTheLengthOfContent = (int) Math.ceil(content.length / 2.0);
firstHalf = Arrays.copyOfRange(content, 0, halfTheLengthOfContent)
and 

secondHalf = Arrays.copyOfRange(content, halfTheLengthOfContent + 1, content.length)

But it doesn't work since the resulting arrays don't correspond to what I want. What I want is that the firstHalf array would contain the same content as if I've done this (with file1 this time containing the first half of the content of the original file and file2 the second half of it):

byte[] firstHalf = new byte[(int) file1.length()];
FileInputStream fileInputStream = null;

fileInputStream = new FileInputStream(file1);
fileInputStream.read(firstHalf);

and

byte[] secondHalf = new byte[(int) file2.length()];
FileInputStream fileInputStream = null;

fileInputStream = new FileInputStream(file2);
fileInputStream.read(secondHalf);

For example if the original file is this:

word1 word2
word3 word4
word5 word6
word7 word8

Then file1 is:

word1 word2
word3 word4

and file2 is this:

word5 word6
word7 word8

Could you please help me ?

Upvotes: 0

Views: 903

Answers (1)

Boris the Spider
Boris the Spider

Reputation: 61158

The concept of "bytes" and "lines" do not work well together.

Bytes are the core elements of a file, there may be a single byte per character. There may be two.

Splitting the byte[] that contains the entire file into two even portions is very unlikely to result in splitting into two byte[] containing the same number of lines. It is unlikely the split will even be on a linebreak, much more likely in the middle of a word.

What you need is to read the file as lines and manipulate them. For example:

final List<String> lines = Files.readAllLines(Paths.get("path", "to", "file"));
final int middle = lines.size() / 2;
final List<String> firstHalf = lines.subList(0, middle);
final List<String> secondHalf = lines.subList(middle, lines.size());

If the file has 4 lines then middle will be 2. The first half will contain lines 0, 1 and the second half will contain lines 2, 3. Remember that a List is zero-indexed and sublist excludes the upper bound.

With an odd number of lines the second half will contain the extra element.

Upvotes: 3

Related Questions