Java File to Binary Conversion

后端 未结 3 815
半阙折子戏
半阙折子戏 2020-12-10 09:20

How can i convert File to Binary? I just need it for my project. I need to encrypt a file by its binaries.

3条回答
  •  渐次进展
    2020-12-10 09:52

            try {
                StringBuilder sb = new StringBuilder();
                File file = new File("C:/log.txt");
                DataInputStream input = new DataInputStream( new FileInputStream( file ) );
                try {
                    while( true ) {
                        sb.append( Integer.toBinaryString( input.readByte() ) );
                    }
                } catch( EOFException eof ) {
                } catch( IOException e ) {
                    e.printStackTrace();
                }
                System.out.println(sb.toString());
            } catch( FileNotFoundException e2 ) {
                e2.printStackTrace();
            }
    

提交回复
热议问题