Java文件读写与创建

来源:互联网 发布:编程猫李天驰个人简介 编辑:程序博客网 时间:2024/05/22 17:31

package common.fileOperate;

import java.io.*;

public class FileRw {
 public FileRw() {
 }

 /**
  * 读取文件filePath中一行的数据,并返回这个数据
  *
  * @param filePath
  * @return
  * @throws FileNotFoundException
  */
 public String ReadFileOneLine(String filePath) throws FileNotFoundException {
  String currentRecord = null;// 保存文本的变量
  // 创建新的BufferedReader对象
  BufferedReader file = new BufferedReader(new FileReader(filePath));
  String returnStr = null;
  try {
   // 读取一行数据并保存到currentRecord变量中
   currentRecord = file.readLine();
  } catch (IOException e) {// 错误处理
   System.out.println("读取数据错误.");
  }
  if (currentRecord == null)
   // 如果文件为空
   returnStr = "没有任何记录";
  else {// 文件不为空
   returnStr = currentRecord;
  }
  // 返回读取文件的数据
  return returnStr;
 }

 /**
  * 读取文件中的所有内容
  *
  * @param filePath
  * @return
  * @throws FileNotFoundException
  */
 public String ReadFile(String filePath) throws Exception {

  //String picturefolderurl=readpro.prop.getProperty("picturefolderurl");

  File file = new File(filePath);
  BufferedReader reader = null;
  String laststr = "";
  try {
   System.out.println("以行为单位读取文件内容,一次读一整行:");
   reader = new BufferedReader(new FileReader(file));
   String tempString = null;
   int line = 1;
   //一次读入一行,直到读入null为文件结束
   while ((tempString = reader.readLine()) != null) {
    //显示行号
    System.out.println("line " + line + ": " + tempString);
    laststr = laststr + tempString;
    line++;
   }
   reader.close();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if (reader != null) {
    try {
     reader.close();
    } catch (IOException e1) {
    }
   }
  }
  return laststr;
 }

 /**
  * 写文件操作 写为一行
  *
  * @param filePath
  *            文件路径
  * @param tempcon
  *            写入的内容
  * @throws FileNotFoundException
  */
 public void WriteFile(String filePath, String tempcon)
   throws FileNotFoundException {
  try {
   // 创建PrintWriter对象,用于写入数据到文件中
   PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
   // 用文本格式打印整数Writestr
   pw.println(tempcon);
   // 清除PrintWriter对象
   pw.close();
  } catch (IOException e) {
   // 错误处理
   System.out.println("写入文件错误" + e.getMessage());
  }
 }

 /**
  * 文件的写入 将要分行的数组以数组的形式传入
  *
  * @param filePath(文件路径)
  * @param fileName(文件名)
  * @param args[]
  * @throws IOException
  */
 public void writeFile(String filePath, String[] args) throws IOException {
  FileWriter fw = new FileWriter(filePath);
  PrintWriter out = new PrintWriter(fw);
  for (int i = 0; i < args.length; i++) {
   out.write(args[i]);
   out.println();

  }
  fw.close();
  out.close();
 }

 /**
  * 判断文件是否存在
  *
  * @return
  */
 public boolean IsFileExists(String filePath) {

  File f = new File(filePath);

  if (f.exists()) {// 检查File.txt是否存在
   return true;
  } else {
   return false;
  }

 }

 /**
  * 创建新文件
  *
  * @param filePath
  * @return
  */
 public boolean CreateFile(String filePath) {
  boolean flag = true;
  File f = new File(filePath);

  if (f.exists()) {// 检查File.txt是否存在
   f.delete();
   try {
    f.createNewFile();
   } catch (IOException e) {
    flag = false;
   }
  } else {
   try {
    f.createNewFile();
   } catch (IOException e) {
    flag = false;
   }

  }
  return false;

 }

 /**
  * 把要写入的字符串写到文件里面
  * @param str 要写入的内容
  * @param destFilePath 目标文件地址
  * @throws Exception
  */
 public static void writefilewithmqhxhtm(String str, String destFilePath)
   throws Exception {
  File temp = new File(destFilePath);
  DataOutputStream outs = new DataOutputStream(new FileOutputStream(temp));
  outs.write(str.getBytes());
  outs.close();

 }

 /* 下面这一般你可以用来测试java应用程序来读取文件,将前面的"//"去掉后你可以运行:java FileRw 来测试。 */
 public static void main(String args[]) throws Exception {
  FileRw fm = new FileRw();
  String filepath = "c:/test.txt";
  System.out.println(fm.IsFileExists(filepath));
  System.out.println(fm.ReadFile(filepath));
  if (!fm.IsFileExists(filepath)) {
   fm.CreateFile(filepath);
   // fm.WriteFile(filepath, "asf");
   // System.out.println(fm.ReadFileOneLine(filepath));
   String[] str = new String[3];
   str[0] = "0=0";
   str[1] = "1=1";
   str[2] = "2=2";
   try {
    fm.writeFile(filepath, str);
   } catch (IOException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
   }

  }
 }
}