1. 问题描述:

原始数据是以行为单位的, 每行固定长度931个字节, 汉字占2个字节, 按照字典描述,共有96个字典,只有第32个字典为中文地址, 所以需要单独处理. 由于项目设计保密,故删除敏感数据. 供实验的数据是测试数据.

在处理过程中,按照规定的字典长度截取字符串的时候,发现处理到汉字的时候出错. 那就需要单独处理汉字. 比较麻烦. 所以写了如下简便方法, 如有更好的解决方案,还请多多交流.

如何计算含有中文的字符串长度.

2. 解决方案:

源码:

package com.dk.rf;

import java.io.*;
import java.util.ArrayList;
import java.util.List; /**
* Created by zzy on 17/1/9.
*/
public class ReadFile {
public static void main(String[] args) {
String path = "/Users/zzy/Downloads/QQdownload/test-readhanzi.txt";
readFileByLines(path); } /**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
// reader = new BufferedReader(new FileReader(file));
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { handleLines(tempString);
char [] chars;
chars = tempString.toCharArray(); line++;
if (line > 100){
break ;
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
} /**
* 处理一行
* @param line
*/
public static void handleLines(String line){
// System.out.println(line.length());
// 每一行数据分为96个字段 List strList = new ArrayList();
int start = 0;
int end = 0;
int [] ss = {42,42,42,8,3,1,1,1,1,1,
6,10,11,11,11,11,11,21,21,21,
4,6,12,4,6,4,3,2,12,6,
8,15,40,3,4,6,10,1,1,5,
2,2,2,2,4,4,11,11,12,12,
12,12,3,3,8,1,8,8,8,8,
8,8,8,8,8,8,8,1,16,8,
8,8,8,8,8,32,2,1,2,14,
4,3,9,12,3,1,8,1,12,15,
21,1,2,1,1,97
}; for (int i = 0; i < ss.length; i++ ){
if (i == 32){ // 单独处理地址
char[] cc = line.toCharArray();
int ss_32=0 ;//
int ff = 0;
System.out.println("-------"+start);
for (int j = start; j < start+ss[i]; j++) {
ss_32++;
ff ++;
if (!isLetter(cc[j])){
// 如果是汉字
ss_32++;
}
if (ss_32 == 40){
ss[i] = ff; break;
}
}
} end = start + ss[i];
if(start>=line.length())
return; String temp = line.substring(start, end);
start = end;
strList.add(temp);
System.out.println("ss["+ i+ "]"+ss[i]+"temp="+temp);
// TO ,设计业务,需要继续,春节后交接 } } /**
* 判断一个字符是Ascill字符还是其它字符(如汉,日,韩文字符)
*
* @param c
* @return
*/
public static boolean isLetter(char c) {
int k = 0x80;
return (c / k) == 0 ? true : false;
} }

3. 相关文件:

test-readhanzi.txt 下载链接

java处理含有中文的字符串.的更多相关文章

  1. java对含有中文的字符串进行Unicode编码

    public class MyUtil { public static void main(String[] args) throws Exception { String s = "a中a ...

  2. isspace 对含有中文 的字符串进行检查的时候表现不正常!?

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> ...

  3. C#:对含有中文的字符串进行MD5加密

    MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider(); var Sign = BitConverter.ToString(MD5. ...

  4. java判断字符串中是否含有中文

    /** * 判断字符串中是否含有中文 */ public static boolean isCNChar(String s){ boolean booleanValue = false; for(in ...

  5. 解决Java getResource 路径中含有中文的情况

    问题描述 当Java调用getResource方法,但是因为路径中含有中文时,得不到正确的路径 问题分析 编码转换问题 当我们使用ClassLoader的getResource方法获取路径时,获取到的 ...

  6. 解决Java工程URL路径中含有中文的情况

    问题: 当Java工程路径中含有中文时,得不到正确的路径 *** 解决: 这其实是编码转换的问题.当我们使用ClassLoader的getResource方法获取路径时,获取到的路径被URLEncod ...

  7. mybatis 插入 含有美元符号($) 字符串 报 java.lang.IndexOutOfBoundsException: No group 2 的问题

    一:问题描述: 在springboot-security框架生成BCryptPasswordEncoder()方法生成加密后的密码后,带有$符号,导致新增用户的时候插入不了,报(IndexOutOfB ...

  8. C#、Java实现按字节截取字符串包含中文汉字和英文字符数字标点符号等

    C#.Java实现按字节截取字符串,字符串中包含中文汉字和英文字符数字标点符号等. 在实际项目应用过程中,尤其是在web开发时可能遇到的比较多,就以我的(JiYF笨小孩管理系统)为例,再发布文章时候, ...

  9. base64编码的字符串(含有中文) 前端解码

    base64编码的字符串(含有中文) 前端解码 https://xue5602.github.io/2018/12/19/atob%E8%A7%A3%E7%A0%81utf-8%E5%AD%97%E7 ...

随机推荐

  1. SpringBoot四大神器之auto-configuration

    SpringBoot 自动配置主要通过 @EnableAutoConfiguration, @Conditional, @EnableConfigurationProperties 或者 @Confi ...

  2. centos7 安装mysql5.7

    [root@izbp1buyhgwtrvlxv3u2gqz soft]# wget http://dev.mysql.com/get/mysql57-community-release-el7-8.n ...

  3. 即将发布的 ASP.NET Core 2.2 会有哪些新玩意儿?

    今年 6 月份的时候时候 .NET 团队就在 GitHub 公布了 ASP.NET Core 2.2 版本的 Roadmap(文末有链接),而前两天 ASP.NET Core 2.2 预览版 2 已经 ...

  4. Mysql_安装

    安装环境:win7 1.下载zip安装包: MySQL8.0 For Windows zip包下载地址:https://dev.mysql.com/downloads/file/?id=476233, ...

  5. 二十九、layui分页插件的使用

    <div id="page1"></div> <script> //开启分页 var page = 1; function findstoreL ...

  6. Python-序列号和模块复习-64

    # 序列化模块 # 数据类型转化成字符串的过程就是序列化 # 为了方便存储和网络传输 # json # dumps # loads # dump 和文件有关 # load load不能load多次 i ...

  7. static:get()什么意思

    在类里面static关键词相当于self关键词

  8. SpringBoot之普通类获取Spring容器中的bean

    package com.geostar.geostack.git_branch_manager.common; import org.springframework.beans.BeansExcept ...

  9. CentOS 7安装MongoDB

    1 下载安装包 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.2.4.tgz 2 解压 .tgz 3 将解压包 ...

  10. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2)

    A. Be Positive 题意:给出一个数组 每个树去除以d(d!=0)使得数组中大于0的数 大于ceil(n/2) 求任意d 思路:数据小 直接暴力就完事了 #include<bits/s ...