日期:2014-05-20 浏览次数:21325 次
import java.io.*;
import java.util.Vector;
/*
* 用法:
* ReadFromFile rf = new ReadFromFile("/res/A.txt");
Vector v = rf.parseFile();
String temp=null;
String[] annonce=new String[v.size()];
for(int i=0;i<v.size();i++){
temp = (String) v.elementAt(i);//tmp为16进制的字符串(UTF8)
annonce[i] = TypeConvert.gb2utf(TypeConvert.toHex(temp));
System.out.println("annonce="+annonce[i]);//中文
}
*/
public class ReadFromFile {
public InputStream in;
public ReadFromFile(String file_path) throws IOException {
in = this.getClass().getResourceAsStream(file_path);
}
//读取所有行,存入Vector
public Vector parseFile() throws IOException {
Vector vec = new Vector();
String line = null;
while ((line = this.readLine()) != null && line.length() > 0) {
if (line.startsWith("#")) {//#注释
} else {
vec.addElement(line);
}
}
return vec;
}
private String readLine() throws IOException {
StringBuffer buffer = new StringBuffer();
boolean isEndOfFile = false;
if (in != null) {
while (true) {
int ch = in.read() & 0xFF;
if (ch == 0xD) { // '13' '\n'
} else if (ch == 0xA) {
break;
} else if (ch == 0xFF) {
isEndOfFile = true;
break;
} else {
buffer.append((char) ch);
}
}
}
if (isEndOfFile) {
if (buffer.length() > 0) {
return buffer.toString();
} else {
return null;
}
}
return buffer.toString();
}
/////////////////////////////////////////////////////////////////////////////////
//一,读取Unicode格式
private String read_Uni(String resource)
{
byte word_uni[]=new byte[1024];
String strReturn="";
InputStream is;
try
{
is=getClass().getResourceAsStream(resource);
is.read(word_uni);
is.close();
StringBuffer stringbuffer = new StringBuffer("");
for (int j = 0; j < word_uni.length; )
{
int k = word_uni[j++]; //注意在这个地方进行了码制的转换
if (k < 0)
k += 256;
int l = word_uni[j++];
if (l < 0)
l += 256;
char c = (char) (k + (l << 8)); //把高位和低位数组装起来
stringbuffer.append(c);
}
strReturn=stringbuffer.toString();
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
is=null;
}
return strReturn;
}
//二,读取UTF-8格式
public String read_UTF(String name)
{
String strReturn = "";
InputStream in = null;
byte[] word_utf= new byte[1024];
try
{
in = getClass().getResourceAsStream(name);
in.read(word_utf);
in.close();
strReturn=new String(word_utf,"UTF-8");
}
catch(Exception e)
{
System.out.println("readUTF Error:"+e.toString());
}
finally
{
in = null;
}
return strReturn;
}
/*三,读取Unicode big endian格式
读取Unicode big endian格式时,采用readChar()方法读取,所以存放时使用char数组存放.
注意:在文本的末尾加上'$'表示文本的结束.
另外代码第10行dis.skip(2)是略过文件头2个字符,如果用microsoft notepad保存的一定存在这两个头字符.
当然,可以使用UltraEdit可以先删掉这两个头字符,然后使用新建文件,复制粘贴,保存为其它格式.这样两个头字符就没了..
*/
private String read_Uni_b_e(String resource)
{
char word_uni_b_e[]=new char[1024];
String strReturn="";
DataInputStream dis;
try
{
dis=new DataInputStream(getClass().getResourceAsStream(resource));
int counter=0;
dis.skip(2);
char temp;
while(true)
{
temp=dis.readChar();
if(temp=='$')
break;
word_uni_b_e[counter++]=temp;
}
dis.close();
strReturn=String.valueOf(word_uni_b_e,0,counter);
}
catch(Exception e)
{
System.out.println("read_Uni_b_e error!"+e.getMessage());
}
finally
{
dis=null;
}
return strReturn;
}
////////////////////////////////////////////////////////////////////////////////
/**
*
* @todo 一定要确定读取的文件为utf-8格式要不然会出错,用ue可以转换成utf-8
* @param name
* String
* @return String
*/
public String readUTF(String path) {
String strReturn = "";
int ic;
InputStream in = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
byte[] myData;
byte[] buffer = new byte[1024];
try {
in = getClass().getResourceAsStream(path);
if (in != null) {
while ((ic = in.read(buffer)) > 0) {
dos.write(buffer, 0, ic);
}
myData = baos.toByteArray();
strReturn = new String(myData, "UTF-8");
in.close();
}
dos.close();
baos.close();
} catch (Exception e) {
System.out.println("readUTF Error:" + e.toString());
} finally {
in = null;
dos = null;
baos = null;
}
return strReturn;
}
}
/*用法: TypeConvert.gb2utf(TypeConvert.toHex(tmp)); 其中tmp如E6ACA2E8BF8EE682A8E4BDBFE794A8E4BFA1E794A8E58DA1E4BCB4E4BEA3E8BDAFE4BBB6EFBC8CE5BD93E5898DE78988E69CACE58FB7E4B8BA56322E38E38082E69CACE8BDAFE4BBB6E698AFE5858DE8B4B9E8BDAFE4BBB6EFBC8CE68891E4BBACE4B88DE4BC9AE59091E682A8E694B6E58F96E8BDAFE4BBB6E4BDBFE794A8E8B4B9EFBC8CE8AFB7E694BEE5BF83E4BDBFE794A8E38082 形式。 */ /** * <p>Title: </p> * * <p>Description: </p> * * <p>Copyright: Copyright (c) 2008</p> * * <p>Company: WorthTech</p> * * @author bruce * @version 1.0 */ import java.io.UnsupportedEncodingException; public class TypeConvert { public static String toHexString(byte[] value) { String newString = ""; for (int i = 0; i < value.length; i++) { byte b = value[i]; String str = Integer.toHexString(b); if (str.length() > 2) { str = str.substring(str.length() - 2); } if (str.length() < 2) { str = "0" + str; } newString += str; } return newString.toUpperCase(); } public static byte[] toHex(String hexString) { int len = hexString.length() / 2; byte[] newByte = new byte[len]; for (int i = 0; i < len; i++) { newByte[i] = toByte(hexString.substring(i * 2, (i + 1) * 2)); } return newByte; } public static byte toByte(String data) { return (byte) Integer.parseInt(data, 16); } public static int toInt(String data) { return Integer.parseInt(data, 16); } public static int hex2int(byte[] data) { return toInt(toHexString(data)); } public static byte[] int2byte(int n) { byte[] b = new byte[4]; b[0] = (byte) (n >> 24); b[1] = (byte) (n >> 16); b[2] = (byte) (n >> 8); b[3] = (byte) n; return b; } /*使用指定字符填充字符串 * 需要格式化的字符串:strSrc * 需要填充的字符:chFormat * 需要填充的长度:nFormatLen */ public static String leftFormatString(String strSrc, char chFormat, int nFormatLen) { if (strSrc == null) { strSrc = ""; } int nLen = strSrc.length(); if (nLen < nFormatLen) { String strNum = ""; for (int i = 0; i < nFormatLen - nLen; i++) { strNum = strNum + chFormat; } return strNum + strSrc; } else { return strSrc; } } public static String rightFormatString(String strSrc, char chFormat, int nFormatLen) { if (strSrc == null) { strSrc = ""; } int nLen = strSrc.getBytes().length; if (nLen < nFormatLen) { String strNum = ""; for (int i = 0; i < nFormatLen - nLen; i++) { strNum = strNum + chFormat; } return strSrc + strNum; } else { return strSrc; } } public static String byte2hexBcd(byte[] b) { if (b == null) { return "字节数组为空"; } String hs = ""; String stmp = ""; for (int n = 0; n < b.length; n++) { stmp = Integer.toHexString(b[n] & 0xFF); if (stmp.length() == 1) { hs = hs + "0" + stmp; } else { hs = hs + stmp; } } return hs.toUpperCase(); } public static byte[] str2bcd(String asc) { int len = asc.length(); int mod = len % 2; if (mod != 0) { asc = "0" + asc; len = asc.length(); } byte abt[] = new byte[len]; if (len >= 2) { len = len / 2; } byte bbt[] = new byte[len]; abt = asc.getBytes(); int j, k; for (int p = 0; p < asc.length() / 2; p++) { if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) { j = abt[2 * p] - '0'; } else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) { j = abt[2 * p] - 'a' + 0x0a; } else { j = abt[2 * p] - 'A' + 0x0a; } if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) { k = abt[2 * p + 1] - '0'; } else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) { k = abt[2 * p + 1] - 'a' + 0x0a; } else { k = abt[2 * p + 1] - 'A' + 0x0a; } int a = (j << 4) + k; byte b = (byte) a; bbt[p] = b; } return bbt; } public static String gb2utf(byte[] gbString) throws UnsupportedEncodingException { return new String(gbString, "UTF-8"); } }