工作笔记:正则切割字符串
对 “中华|人民|共和|国”这样的字符串进行切割,切割后写入以新的文件中。 JAVA中涉及i/o流操作,就必须了解计算机编码: 2、ISO-8859-1 3、Unicode 4、Unicode和utf-8,utf-16; 5、UTF-8 对于英文比较多的论坛,使用GBK则每个字符占用2个字节,而使用UTF-8英文却只占一个字节。 7、UTF-16 代码,在eclipse中写入文件 public class RegexWrite {
/** * @param yu */
public final static String FILE_NAME = "file.txt";
public final static String FILE_NAME_REGEX="file_regex.txt";
public static void main(String[] args) {
// 写文件:"中华|人民|共和|国"
try {
//创建文件输入流,写文件;
FileOutputStream fos = new FileOutputStream(FILE_NAME);
fos.write("中华|人民|共和|国".getBytes());
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2、读取文件,并转换成16进制切割,再写切割后写入到新的文件中。 public class RegexRead {
public static void main(String[] args) {
String context = "";
String str_split = "|";
try {
// 1创建缓冲文件输出流,读文件;在java中默认的创建缓存区的大小为8192个字节;
/*BufferedReader br = new BufferedReader(new FileReader( RegexWrite.FILE_NAME)); int read = 0; while ((read = br.read()) != -1) { context = context + (char) read; } br.close();*/
//2、设置缓冲区文件大小为要读写文件大小;
int read;
FileInputStream fis =new FileInputStream(RegexWrite.FILE_NAME);
//得到文件的字节个数;
int available=fis.available();
//把文件大小赋值给byte数组;
byte b[] =new byte[available];
System.out.println("available"+available);
while((read=fis.read(b))>0){
System.out.println(new String(b,0,read));
context=context+new String(b,read);
}
fis.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("读取所要切割的字符串 " + context);
// 把字符串用十六进制表示;
String str_hex = str2HexStr(context);
// 把切割的字符串转换成十六进制;
String split_hex = str2HexStr(str_split);
System.out.println("内容的十六进制表示: " + str_hex);
System.out.println("| 的十六进制表示: " + split_hex);
// 对字符串切割,返回数组;
String[] splie_context = str_hex.split(split_hex);
String str_final = "";
for (int i = 0; i < splie_context.length; i++) {
String convert_str = "";
convert_str = hexStr2Str(splie_context[i]);
// convert_str = hexString2String(splie_context[i]);
System.out
.println(splie_context[i] + " 转换后 " + convert_str);
str_final = str_final + convert_str;
}
System.out.println("str_final " + str_final);
// 把得到的字符串写入到新的文件中;
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(
RegexWrite.FILE_NAME_REGEX));
bw.write(str_final);
bw.flush();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** * 字符串转换成16进制; * * @param str * @return */
public static String str2HexStr(String str) {
char[] chars = "0123456789ABCDEF".toCharArray();
StringBuilder sb = new StringBuilder("");
byte[] bs = str.getBytes();
int bit;
for (int i = 0; i < bs.length; i++) {
bit = (bs[i] & 0x0f0) >> 4;
sb.append(chars[bit]);
bit = bs[i] & 0x0f;
sb.append(chars[bit]);
}
return sb.toString();
}
/** * 十六进制转换字符串 */
public static String hexStr2Str(String hexStr) {
String str = "0123456789ABCDEF";
char[] hexs = hexStr.toCharArray();
byte[] bytes = new byte[hexStr.length() / 2];
int n;
for (int i = 0; i < bytes.length; i++) {
n = str.indexOf(hexs[2 * i]) * 16;
n += str.indexOf(hexs[2 * i + 1]);
bytes[i] = (byte) (n & 0xff);
}
return new String(bytes);
}
}
切割后 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |