码迷,mamicode.com
首页 > 编程语言 > 详细

java IO存在问题

时间:2015-10-31 23:05:43      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:

//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();
}

java IO存在问题

标签:

原文地址:http://www.cnblogs.com/yongssu/p/4926370.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!