请教一个读取字符串的问题在文件中,每行的头10个字节为一个字段,后面的为一个字段,
是以固定宽度来分割字段的,而不是以中间分割符来界定,
现在有个问题就是:
头10个字节可能出现中文,也可以出现阿拉伯字符或数字,或者中文和数字的混合,
如果宽度不够,则右补齐空格至10个字节.
请问在这种情况下,程序如何读取从开始每行10个字节后的字符串数据,
简单的substring是不行的了,因为有中文或者混合求算法,谢谢了
是以固定宽度来分割字段的,而不是以中间分割符来界定,
现在有个问题就是:
头10个字节可能出现中文,也可以出现阿拉伯字符或数字,或者中文和数字的混合,
如果宽度不够,则右补齐空格至10个字节.
请问在这种情况下,程序如何读取从开始每行10个字节后的字符串数据,
简单的substring是不行的了,因为有中文或者混合求算法,谢谢了
* project_name: Test
* package_name: netsource
* package_declaration: package netsource;
* filename: DisposalFile.java
* author: yuhaiming
* date: 2007-11-8
*/
package netsource;
import java.io.*;
public class DisposalFile {
/**
* 文件内容:
* CSDN
中国人
爱情a
e你我他1
*/
public static String[] str =new String[10];
public static int length = 10;
public static int count = 0;
/**
* 处理文件及数据
* @throws Exception
*/
public static void disposalfile()throws Exception{
File file = new File("D:\\workspace\\Test\\source.txt");
//System.out.println(file.length());
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String aline = reader.readLine();
while(aline!=null){
putValue(aline);
aline = reader.readLine();
}
}
/**
* 字符转换
* @param aline
* @throws Exception
*/
public static void putValue(String aline)throws Exception{
int curlength = aline.getBytes("GBK").length;
byte[] targetbuf = new byte[length];
byte[] soucrebuf = aline.getBytes("GBK");
for(int i=0;i<length;i++){
if(i<curlength)
targetbuf[i]=soucrebuf[i];
else targetbuf[i] =' ';
}
//System.out.println(new String(targetbuf));
str[count++] = new String(targetbuf);
}
/**
* 打印
*/
public static void printmyvalue(){
for(int i=0;i<str.length;i++){
if(str[i]!=null){
System.out.println(str[i]);
}else break;
}
}
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
disposalfile();
printmyvalue();
}
}