简体   繁体   中英

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.

The School provided a class and method for reading 1 bit and converting that in to a character 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? And how would I convert all the char 0s and 1s in to strings of 0s and 1s?

The readBit method:

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.

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 !!! ");
        }   
    }

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