java根據字節長度進行txt文件解析

在日常開發中,有時候需要對第三方文件推送過來的數據文件進行解析,雙方約定的格式是GBK編碼,一行代表一條記錄,字節定長的方式去存入和獲取,這樣就不能用substring進行字符截取了,具體實現方法直接看代碼吧:

/**
     * 解析第三方傳入文件
     * 
     * @param filePath
     *            傳入文件路徑
     * @throws Exception
     */
    public static void parseFile(String filePath)
            throws Exception {
       
        try {
            File file = new File(filePath);
            InputStream is = new FileInputStream(file);
            BufferedReader br = new BufferedReader(new InputStreamReader(is,Charset.forName("GBK")));
            String line = "";
            while ((line = br.readLine()) != null) {
                // 客戶名-20位-截取6-25
                String cifName= StringCommonUtil.substringByte(line,6, 19).trim();
                // 身份證號-18位-截取31-48
                String blackListType = StringCommonUtil.substringByte(line,31, 17).trim();
              //todo其他業務處理
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
     
    }

其中,這一句進行對文件輸入流的字節編碼(如果約定其他編碼,可以統一替換爲其他格式)

BufferedReader br = new BufferedReader(new InputStreamReader(is,Charset.forName("GBK")));

字節截取的方法如下:

/**
     * 按字節截取字符串 ,指定截取起始字節位置與截取字節長度
     * 
     * @param orignal
     *            要截取的字符串
     * @param offset
     *            截取Byte長度;
     * @return 截取後的字符串
     * @throws UnsupportedEncodingException
     *             使用了JAVA不支持的編碼格式
     */
    public static String substringByte(String orignal, int start, int count) {

        // 如果目標字符串爲空,則直接返回,不進入截取邏輯;
        if (orignal == null || "".equals(orignal))
            return orignal;

        // 截取Byte長度必須>0
        if (count <= 0)
            return orignal;

        // 截取的起始字節數必須比
        if (start < 0)
            start = 0;

        // 目標char Pull buff緩存區間;
        StringBuffer buff = new StringBuffer();

        try {
            // 截取字節起始字節位置大於目標String的Byte的length則返回空值
            if (start >= getStringByteLenths(orignal))
                return null;
            int len = 0;
            char c;
            // 遍歷String的每一個Char字符,計算當前總長度
            // 如果到當前Char的的字節長度大於要截取的字符總長度,則跳出循環返回截取的字符串。
            for (int i = 0; i < orignal.toCharArray().length; i++) {
                c = orignal.charAt(i);

                // 當起始位置爲0時候
                if (start == 0) {
                    len += String.valueOf(c).getBytes("GBK").length;
                    if (len <= count)
                        buff.append(c);
                    else
                        break;
                } else {
                    // 截取字符串從非0位置開始
                    len += String.valueOf(c).getBytes("GBK").length;
                    if (len >= start && len <= start + count) {
                        buff.append(c);
                    }
                    if (len > start + count)
                        break;
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        // 返回最終截取的字符結果;
        // 創建String對象,傳入目標char Buff對象
        return new String(buff);
    }

    /**
     * 計算當前String字符串所佔的總Byte長度
     * 
     * @param args
     *            要截取的字符串
     * @return 返回值int型,字符串所佔的字節長度,如果args爲空或者“”則返回0
     * @throws UnsupportedEncodingException
     */
    public static int getStringByteLenths(String args)
            throws UnsupportedEncodingException {
        return args != null && args != "" ? args.getBytes("GBK").length : 0;
    }
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章