1 用ultraedit将你的文件转成utf-8编码
2 将你的eclispse转成utf-8编码
3 执行。
package com.y3.beam;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
public class ReadFromFile {
public static void readFileByBytes(String fileName) {
File file = new File(fileName);
InputStream in = null;
try {
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
try {
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
while ((byteread = in.read(tempbytes)) != -1) {
System.out.write(tempbytes, 0, byteread);
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
}
}
}
public static void readFileByChars(String fileName) {
File file = new File(fileName);
Reader reader = null;
try {
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = reader.read()) != -1) {
if (((char) tempchar) != '/r') {
System.out.print((char) tempchar);
System.out.println(isChinese((char)tempchar));
if(isChinese((char)tempchar)){
String s = (char) tempchar+"";
System.out.println(toUnicode(s));
}
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
while ((charread = reader.read(tempchars)) != -1) {
if ((charread == tempchars.length)
&& (tempchars[tempchars.length - 1] != '/r')) {
System.out.print(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == '/r') {
continue;
} else {
System.out.print(tempchars[i]);
}
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
while ((tempString = reader.readLine()) != null) {
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
public static void readFileByRandomAccess(String fileName) {
RandomAccessFile randomFile = null;
try {
randomFile = new RandomAccessFile(fileName, "r");
long fileLength = randomFile.length();
int beginIndex = (fileLength > 4) ? 4 : 0;
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
while ((byteread = randomFile.read(bytes)) != -1) {
System.out.write(bytes, 0, byteread);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (randomFile != null) {
try {
randomFile.close();
} catch (IOException e1) {
}
}
}
}
private static void showAvailableBytes(InputStream in) {
try {
System.out.println("in byte number:" + in.available());
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A) {
return true;
}
return false;
}
public static void main(String[] args) {
String fileName = "C:/newTemp.txt";
char c='中';
System.out.println(isChinese(c));
System.out.println((int)c);
//ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
//ReadFromFile.readFileByLines(fileName);
//ReadFromFile.readFileByRandomAccess(fileName);
}
public static String toUnicode(String str){
char[]arChar=str.toCharArray();
int iValue=0;
String uStr="";
for(int i=0;i<arChar.length;i++){
iValue=(int)str.charAt(i);
if(iValue<=256){
uStr+="//u00"+Integer.toHexString(iValue);
}else{
uStr+="//u"+Integer.toHexString(iValue);
}
}
return uStr;
}
}