简体   繁体   English

如何使用 readAllBytes 将文件中的字节读入单个字符串?

[英]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.我正在使用算法 class,我们必须在 Java 中实现 LZW 压缩。 I decided to use a Trie data structure for this, and I've already implemented the Trie and got it working.我决定为此使用 Trie 数据结构,并且我已经实现了 Trie 并让它工作。

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.现在,我想从文件中读取字节,将它们转换为填充二进制(00000001 而不是 01),然后将它们存储在我的 Trie 中。 I don't need help for the Trie, but rather for reading the contents of the file.我不需要 Trie 的帮助,而是读取文件的内容。

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.我尝试使用 readAllBytes 来读取内容,并将每个转换后的字节附加到 StringBuilder,但是当我这样做时,我得到了一个充满 48 和 49 的 StringBuilder。 I think my binary data is being converted to ASCII, which I don't want.我认为我的二进制数据正在转换为我不想要的 ASCII。 I simply want a string with 1's and 0's.我只是想要一个带有 1 和 0 的字符串。

I've gotten the method below to work, but it relies on an ArrayList instead of a String.我已经得到了下面的方法,但它依赖于 ArrayList 而不是字符串。 It also doesn't use readAllBytes, and it's very slow.它也不使用 readAllBytes,而且速度很慢。 (I was unable to get readAllBytes working, it just gives me an infinite loop). (我无法让 readAllBytes 工作,它只是给了我一个无限循环)。 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.我所有填充的二进制值都转换为 ASCII,所以我的字符串用 48、49 等填充,而不是原始日期。

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.如果您不知道,UTF-8 是一个不错的选择,这是文件 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 .无需涉及readAllBytes If you must (which you really only should if you don't have a file at all but something else in InputStream form):如果必须(只有在根本没有文件但 InputStream 形式的其他文件时才应该这样做):

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.您可以使用StringBufferStringBuilder class 来生成最终的字符串结果。

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;您可以从这样的文件中获取 btyeCode;

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用于压缩使用ZipOutputStream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 readAllBytes 读取文件的字节,但是当我更改文件时 output 没有改变 - Using readAllBytes to read a file's bytes, but output is not changing when I change the file 从同一文件java中读取字符串和字节 - Read String and bytes from the same file java 如何使用datainput流从文件中读取字节数组,并在Java中打印字节数组 - How to read an array of bytes from a file using datainput stream, and print the array of bytes in java 如何从文件中读取特定数量的字节到Java中的bytes数组中? - How to read a specific number of bytes from a file into a bytes array in Java? 有没有办法在不使用 byte[] 或 readAllBytes() 的情况下使用 java 1.8 读取 PDF 文件 stream - Is there a way to read PDF file stream with out using byte[ ] or readAllBytes() using java 1.8 如何比较从文件读取的字节和单个字符串 - How to compare read byte from file to single character string 如何在字符串readAllBytes中保留换行符 - How to preserve line break in String readAllBytes 如何使用RandomAccessFile从文件中读取字符串? - how to read the string from file using RandomAccessFile? 从文件读取多行到单个字符串 - Read multiple lines from file to a single string 一次从文件读取单个字符串 - Read a Single String from a File at a Time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM