import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException;public class FileOperate {//文件操作类 public String content; public String Econtent; public String temp; public String txts;
public void ReadTxt() {//读取存在的文本文件 try { BufferedReader in = new BufferedReader(new FileReader("文件名.txt")); boolean eof = false; temp = in.readLine(); content = temp; while (!eof) { String line = in.readLine(); if (line == null) eof = true; else { content = content + line; } } in.close(); System.out.println(content);//输出文本文件 } catch (IOException e) { System.out.println("Error --" + e.toString()); } } }
没看清题意,不好意思~只读中文,你可以加上限制条件判断每个字符int是否在中文的范围之内! 这样就不能readline(),要用字节的读法,给你一个参考函数:public static void readFileByChars(String fileName){ File file = new File(fileName); Reader reader = null; try { System.out.println("以字符为单位读取文件内容,一次读一个字节:"); // 一次读一个字符 reader = new InputStreamReader(new FileInputStream(file)); int tempchar; while ((tempchar = reader.read()) != -1){ //对于windows下,\r\n这两个字符在一起时,表示一个换行。 //但如果这两个字符分开显示时,会换两次行。 //因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。 if (((char)tempchar) != '\r'){ System.out.print((char)tempchar); } } reader.close(); } catch (Exception e) { e.printStackTrace(); } try { System.out.println("以字符为单位读取文件内容,一次读多个字节:"); //一次读多个字符 char[] tempchars = new char[30]; int charread = 0; reader = new InputStreamReader(new FileInputStream(fileName)); //读入多个字符到字符数组中,charread为一次读取字符数 while ((charread = reader.read(tempchars))!=-1){ //同样屏蔽掉\r不显示 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]); } } } }
System.out.println((int)'我');
} 输出的是25105
if(((st.charAt(i) >= 0x3400) && (st.charAt(i) < 0x9FFF)) || (st.charAt(i) >= 0xF900))
{
。。
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;public class FileOperate {//文件操作类 public String content;
public String Econtent;
public String temp;
public String txts;
public void ReadTxt() {//读取存在的文本文件
try {
BufferedReader in = new BufferedReader(new FileReader("文件名.txt"));
boolean eof = false;
temp = in.readLine();
content = temp;
while (!eof) {
String line = in.readLine();
if (line == null)
eof = true;
else {
content = content + line;
}
}
in.close();
System.out.println(content);//输出文本文件
}
catch (IOException e) {
System.out.println("Error --" + e.toString());
}
}
}
这样就不能readline(),要用字节的读法,给你一个参考函数:public static void readFileByChars(String fileName){
File file = new File(fileName);
Reader reader = null;
try {
System.out.println("以字符为单位读取文件内容,一次读一个字节:");
// 一次读一个字符
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = reader.read()) != -1){
//对于windows下,\r\n这两个字符在一起时,表示一个换行。
//但如果这两个字符分开显示时,会换两次行。
//因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
if (((char)tempchar) != '\r'){
System.out.print((char)tempchar);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("以字符为单位读取文件内容,一次读多个字节:");
//一次读多个字符
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
//读入多个字符到字符数组中,charread为一次读取字符数
while ((charread = reader.read(tempchars))!=-1){
//同样屏蔽掉\r不显示
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) {
}
}
}
}
StringBuffer bf = new StringBuffer();
char[] chars = Content.toCharArray();
for (int i=0;i<chars.length;i++) {
if(chars[i]>127){
bf.append(chars[i]);
}
}
String str = bf.toString();
return str;
}
你在IE里面由于选错了编码。经常看到莫名其妙的汉字,虽然是乱码,但绝对是汉字。
import java.io.FileReader;
import java.io.IOException;class FileOperate {//文件操作类
//返回指定文本文件中的文本内容 readTxt(文件路径)
public static String readTxt(String fileURL) {//读取存在的文本文件
try {
BufferedReader br = new BufferedReader(new FileReader(fileURL);
String temp;
String sumStr="";
while (( temp= br.readLine())!=null) {
sumStr+=(temp+"\n")
}
}
catch (IOException e) {}
finally{
br.close();
}
return sumStr;
}
}
ASCII:48~57
UNICODE:U+0030~U+00390~9(全角)
UNICODE:U+FF10~U+FF19
汉字 的 unicode 编码 范围 来 判断 是否是 汉字 然等候 在 选择读取
if(((st.charAt(i) > = 0x3400) && (st.charAt(i) < 0x9FFF)) ¦ ¦ (st.charAt(i) > = 0xF900))
{
。。
}
{
。。
}
这种事什么区分法 我试了 确实可以!
http://baike.baidu.com/view/40801.htm因为Java更多是处理UNICODE的,了解UNICODE16就差不多了。
U+FF00~U+FFEF是一些标点、全角数字和字母,以及一些其他一些字符
可以直接采用Unicode码来进行比较(把0x换成\u再在两边各加一个单引号):比如:c[i] >= '\u4e00'