Java文件操作(转载)

Java文件操作(转载)

Java文件操作(转载)
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();
  }
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值