免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1006 | 回复: 0
打印 上一主题 下一主题

java io [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-06-20 14:38 |只看该作者 |倒序浏览

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
  }
  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();
   }
  }
}
}
/**
  * 创建新文件
  *
  * @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;
}
  • /**
  •       * 创建新文件
  •       */  
  •      public boolean CreateFile(String filePath) {  
  •          boolean flag = true;  
  •          File f = new File(filePath);  
  •          if (f.exists())  
  •              f.delete();  
  •          try {  
  •              f.createNewFile();  
  •          } catch (IOException e) {  
  •              flag = false;  
  •          }  
  •          return flag;  
  •      }  
                   
                   
                   

    本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/49717/showart_1006070.html
  • 您需要登录后才可以回帖 登录 | 注册

    本版积分规则 发表回复

      

    北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
    未成年举报专区
    中国互联网协会会员  联系我们:huangweiwei@itpub.net
    感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

    清除 Cookies - ChinaUnix - Archiver - WAP - TOP