简体   繁体   中英

How to read bytes from a file into a single string using readAllBytes?

I'm taking an algorithms class where we have to implement LZW compression in Java. I decided to use a Trie data structure for this, and I've already implemented the Trie and got it working.

Now, I would like to read the bytes from a file, convert them to padded binary (00000001 instead of 01) and then store them in my Trie. I don't need help for the Trie, but rather for reading the contents of the file.

I've tried using readAllBytes to read the contents, and appending each converted byte to a StringBuilder, but when I do this, I get a StringBuilder full of 48's and 49's. I think my binary data is being converted to ASCII, which I don't want. I simply want a string with 1's and 0's.

I've gotten the method below to work, but it relies on an ArrayList instead of a String. It also doesn't use readAllBytes, and it's very slow. (I was unable to get readAllBytes working, it just gives me an infinite loop). We will be graded on performance.

    File file = new File(path);

    ArrayList<String> codes = new ArrayList<String>();

    try (FileInputStream fileInputStream = new FileInputStream(file)) {
        int singleCharInt;
        while ((singleCharInt = fileInputStream.read()) != -1) {
            codes.add(Integer.toBinaryString((singleCharInt & 0xFF) + 0x100).substring(1));
        }
    }

    return codes;

Thank you for your time!

all my padded binary values were converted to ASCII, So my string was filled with 48, 49. etc. instead of the original date.

This makes it sound like you want to just read a file. Before you can read it, you'd need to know what charset encoding it is in. You can't tell from either the file or the extension (at least, not usually), you'd have to know. If you don't know, UTF-8 is a good bet, and this is the default for the Files API.

Path p = Paths.get("/path/to/file.txt");
String string = Files.readString(p);

That's all you need to do. No need to involve readAllBytes . If you must (which you really only should if you don't have a file at all but something else in InputStream form):

String s;
try (InputStream in = ...) {
    s = new String(in.readAllBytes(), StandardCharsets.UTF_8);
}

You can use the StringBuffer or StringBuilder class to produce your final String result.

StringBuffer buffer = new StringBuffer();

try (FileInputStream fileInputStream = new FileInputStream(file)) {
    int singleCharInt;
    while ((singleCharInt = fileInputStream.read()) != -1) {
        buffer.append(Integer.toBinaryString((singleCharInt & 0xFF) + 0x100));
    }
}

// Result: buffer.toString()

You can get btyeCode from a file like this;

public static byte[] readFileNio(String physicalPath) throwsFileNotFoundException, IOException{ 
  RandomAccessFile aFile = new RandomAccessFile(physicalPath,"r");
  FileChannel inChannel = aFile.getChannel();
  ByteBuffer buffer = ByteBuffer.allocate((int)inChannel.size());
  inChannel.read(buffer);
  buffer.flip();
  inChannel.close();
  aFile.close();
  return buffer.array();
}

then you can write byteCode to any particular place

Files.write(newPath, readFileNio(fromPath), StandardOpenOption.CREATE_NEW);

for compression use ZipOutputStream

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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