简体   繁体   English

Java 压缩 .txt 文件

[英]Java compressing a .txt file

I am currently trying to write a program which reads in a compressed file which is written in bits or 0s and 1s, and convert them in to strings of 0s and 1s.我目前正在尝试编写一个程序,该程序读取以位或 0 和 1 写入的压缩文件,并将它们转换为 0 和 1 的字符串。

The School provided a class and method for reading 1 bit and converting that in to a character char.学校提供了读取 1 位并将其转换为字符 char 的类和方法。 So to read and convert one bit to a char, all i need to do is type in my code:所以要读取并将一位转换为字符,我需要做的就是输入我的代码:

char oneBit = inputFile.readBit();

in my main method.在我的主要方法中。

How do I get my program to read over every bit within the compressed file and convert them to char?如何让我的程序读取压缩文件中的每一位并将它们转换为字符? using the .readBit method?使用.readBit方法? And how would I convert all the char 0s and 1s in to strings of 0s and 1s?我如何将所有字符 0 和 1 转换为 0 和 1 的字符串?

The readBit method: 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;
}

where in is the input file.其中in是输入文件。

Try using this resource尝试使用此资源

Sample implementation.示例实现。

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