簡體   English   中英

Java 壓縮 .txt 文件

[英]Java compressing a .txt file

我目前正在嘗試編寫一個程序,該程序讀取以位或 0 和 1 寫入的壓縮文件,並將它們轉換為 0 和 1 的字符串。

學校提供了讀取 1 位並將其轉換為字符 char 的類和方法。 所以要讀取並將一位轉換為字符,我需要做的就是輸入我的代碼:

char oneBit = inputFile.readBit();

在我的主要方法中。

如何讓我的程序讀取壓縮文件中的每一位並將它們轉換為字符? 使用.readBit方法? 我如何將所有字符 0 和 1 轉換為 0 和 1 的字符串?

readBit 方法:

public char readBit() {
    char c = 0;

    if (bitsRead == 8)
        try {
            if (in.available() > 0) { // We have not reached the end of the
                                        // file
                buffer = (char) in.read();
                bitsRead = 0;
            } else
                return 0;
        } catch (IOException e) {
            System.out.println("Error reading from file ");
            System.exit(0); // Terminate the program
        }

    // return next bit from the buffer; bit is converted first to char
    if ((buffer & 128) == 0)
        c = '0';
    else
        c = '1';
    buffer = (char) (buffer << 1);
    ++bitsRead;

    return c;
}

其中in是輸入文件。

嘗試使用此資源

示例實現。

public class BitAnswer {

    final static int RADIX = 10;

    public static void main(String[] args) {
        BitInputStream bis = new BitInputStream("<file_name>");
        int result = bis.readBit();
        while( result != -1 ) {
            System.out.print(Character.forDigit(result, RADIX));
            result = bis.readBit();
        }

        System.out.println("\nAll bits read!");
    }
}
public  void compress(){
        String inputFileName = "c://tmp//content.txt";
        String outputFileName = "c://tmp//compressedContent.txt";
        FileOutputStream fos = null; 
        StringBuilder sb = new StringBuilder();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        OutputStream outputStream= null;    
        try (BufferedReader br = new BufferedReader(new FileReader(new File(inputFileName)))) {
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            outputStream = new  DeflaterOutputStream(byteArrayOutputStream); // GZIPOutputStream(byteArrayOutputStream) - use if you want unix .gz format
            outputStream.write(sb.toString().getBytes());
            String compressedText = Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray());
            fos=new FileOutputStream(outputFileName);
            fos.write(compressedText.getBytes());
            System.out.println("done compress");
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try{
                if (outputStream != null) {
                    outputStream.close();
                }
                if (byteArrayOutputStream != null) {
                    byteArrayOutputStream.close();
                }
                if(fos != null){
                    fos.close();
                }
            }catch (Exception e) {
                    e.printStackTrace();
            }
            System.out.println("closed streams !!! ");
        }   
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM