Chinaunix首页 | 论坛 | 博客
  • 博客访问: 351915
  • 博文数量: 51
  • 博客积分: 2011
  • 博客等级: 大尉
  • 技术积分: 613
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-06 17:19
文章分类

全部博文(51)

文章存档

2011年(1)

2010年(3)

2009年(27)

2008年(20)

我的朋友

分类: Java

2008-09-26 11:14:49

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

 /**
  *
  * 先判断文件是否存在,然后判断文件是否为空。
  * 如果存在且不为空,则输出文件内容在控制台。
  *
  * @author rmouse_2005   
  * @data 2005-10-14  
  * @version 1.0
  */ 

public class Test {

 /**
  *
  * Test类主函数
  *
  * @param args
  * @throws IO异常
  */
 public static void main(String[] args) throws IOException {
  String path = "h:\\eclipse\\test\\a.txt";
  Test t = new Test();
  if (t.isExist(path)) {
   if (t.isEmpty(path)) {
    System.out.println("success!");
    System.out.println();
    t.getValue(path);
   } else {

    System.out.println("false!");
   }
  } else {
   System.out.println("false!");
  }
 }

 /**
  *
  * 判断文件是否存在
  *
  * @param file 文件名
  * @return 文件是否存在
  * true - 文件存在
  * false - 文件不存在
  */
 boolean isExist(String file) {
  File f = new File(file);
  if (f.exists()) {
   System.out.println("File is Exist!");
   return true;
  } else {
   System.out.println("File isn't Exist!");
   return false;
  }
 }

 /**
  *
  * 判断文件是否为空
  *
  * @param file 文件名
  * @return 文件是否为空
  * true - 文件为空
  * false - 文件不为空
  * @throws IO异常
  */
 boolean isEmpty(String file) throws IOException {
  FileReader fr = new FileReader(file);
  if (fr.read() == -1) {
   System.out.println("File is Empty!");
   return false;
  } else {
   System.out.println("File isn't Empty!");
   return true;
  }
 }

 /**
  *
  * 读取文件
  *
  * @param file 文件名
  * @throws IO异常
  */
 void getValue(String file) throws IOException {
  FileReader fr;
  fr = new FileReader(file);
  BufferedReader br = new BufferedReader(fr);
  String line = "";
  line = br.readLine();
  while (line != null) {
   String lineNew = "";
   lineNew = line;
   System.out.println(lineNew);
   line = br.readLine();
  }
 }
}

阅读(958) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~