一般系统能够根据第一个字节来判断文件是使用什么编码格式,最好推荐使用是UTF8没有BOM格式的编码,(ISO8859-1不能显示中文,但是别的国家用的特别多)编码问题特别蛋疼,还有全角半角之分,(我特别讨厌全角符号和全角英文,设计者在这一方面很蠢,都不知道多少人因为这个踩过坑,因为不小心按了组合键,变成全角的,然后代码死活编译失败,查了很久找不出有啥错误)总是很容易忽视细节问题,比如window下的换行是\r\n而Linux下是\n
推荐用户使用目录路径为C:/programe file/ 而不是C:\programe file\这样的路径,window支持/路径
因为后者要转义成C:\\programe file\\比较麻烦,而且很多时候很纠结要不要这样写
package com.copycat.test;
import java.io.File;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.FileInputStream;
public class ReadTest {
public static void main(String args[])
{
try {
/* 读入TXT文件 */
String pathname = "G:\\input.txt";
File filename = new File(pathname);
InputStreamReader reader = new InputStreamReader(new FileInputStream(filename));
BufferedReader br = new BufferedReader(reader);
String line = "";
line = br.readLine();
String str=line;
String str2="\\n"; //验证\n在内存里的形式
String str3="\n"; //结果输出为:the same to the str2
if(str.equals(str2))
{
System.out.print("the same to the str2");
}
else if(str.equals(str3))
{
System.out.println("the same to the str3");
}
else
{
System.out.println("unkown error");
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}