記一次文件轉碼與二進制查看學習

要求:app

漢字:GBK內碼工具

西文:ASCII碼ui

間隔符:blog

 /**
     * 字段間隔符
     */
    private static final String FIELD_INTERVAL = new String(new byte[]{0x01});

    /**
     * 行間隔符
     */
    private static final String LINE_INTERVAL = new String(new byte[] {0x0A});

  

轉碼:(對西文進行轉ASCII碼)ci

private void appendWithAscii(StringBuilder builder, Object... value) {
  for (Object object : value) {
    if (object != null) {
      builder.append(getAscii(String.valueOf(object)));
    }
    builder.append(FIELD_INTERVAL);
  }
}


private String getAscii(String input) {
        byte[] asciiByte = input.getBytes(Charsets.US_ASCII);
        return new String(asciiByte);
    }

  

  

以GBK方式經過二進制形式寫入文件:get

 /**
     * 以二進制方式寫入
     * @param fileName
     * @param content
     * @param encoding
     * @return
     */
    public static boolean writeFileBinary (String fileName, String content, String encoding) {
        boolean flag = false;
        DataOutputStream out = null;
        try {
            out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(fileName)));
            byte[] contentByte = content.getBytes(encoding);
            out.write(contentByte);
            flag = true;
        } catch (IOException e) {
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
            
        }
        
        return flag;
    }

  

文件二進制查看:工具(Binary Viewer)input

其中 01 爲 0x01,對應上面的  字段間隔符 (域間隔符)it

       0A爲0x0A,對應上面的 行間隔符io

 

 

此內容謹記錄工做中所遇之事,深刻了解待後續更新.....class


未完待續....

相關文章
相關標籤/搜索