Okay, so I'm trying to do something similar, that it should be pretty simple, but with these new NIO interfaces, everything confuses me! Here, what I'm trying to do, I need to scan the file as bytes until you find certain bytes! When I come across these specific bytes, I need to capture this data segment and do something with it, then go to it and do it again. I would think that with all these markers and positions and restrictions in ByteBuffer, I could do it, but I can't get it to work! That's what I still have ..
test.text:
this is a line of text a this is line 2b line 3 line 4 line etc.etc.etc.
Test.java:
import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; public class Test { public static final Charset ENCODING = Charset.forName("UTF-8"); public static final byte[] NEWLINE_BYTE = {0x0A, 0x0D}; public Test() { String pathString = "test.txt";
So, the first part works, the fc.read (buffer) function works only once and pulls out the whole file in ByteBuffer. Then in the second do loop, I can execute a byte cycle by byte, just fine, and it gets into the if statement when it gets into \ n (or \ r), but then I canβt figure out how to get PORTION from bytes that I just looked into a separate byte array for work! I tried splicing and various flips, and I tried wrapping as shown in the above code, but it can't seem to make it work, both buffers always have a full file, and so I do something that I spliced ββor wrapped!
I just need to skip the byte file by byte, looking at a specific section at a time, and then my final goal, when I looked and found the right place, I want to paste some data into a direct place! I need this lineBuffer, which is output to "LINE:", to have ONLY a part of the bytes that I have missed so far! Help and thanks!
java nio bytebuffer filechannel
Joshua chambers
source share