java IO存在问题

//java读取文件
/*String path = "D:/project/training/src/com/test1/test.txt";
File file = new File(path);
if(!file.exists() || file.isDirectory()) {
System.out.println("error");
}
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String temp = null;
StringBuffer sb = new StringBuffer();
temp = br.readLine();
while(temp != null) {
System.out.println(temp);
temp = br.readLine();
}
} catch(IOException e) {
System.out.println("error");
}*/
///??这样子都文件,如果文件是空白的,为什么还会读一个中文
String filePathAndName = "D:/project/training/src/com/test1/test2.txt";
String fileContent = "";
try {

File f = new File(filePathAndName);
if(f.isFile()&&f.exists()){
InputStreamReader read = new InputStreamReader(new FileInputStream(f),"UTF-8");
BufferedReader reader=new BufferedReader(read);
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
read.close();
}
} catch (Exception e) {
System.out.println("读取文件内容操作出错");
e.printStackTrace();
}
posted @ 2015-10-31 22:17  禅意  阅读(242)  评论(0编辑  收藏  举报