二级JAVA文件操作大全
来源:优易学  2011-11-2 15:32:19   【优易学:中国教育考试门户网】   资料下载   IT书店

  package com.pengyue;
  import java.io.*;
  public class FileOperate {
    public FileOperate() {
    }
    /**
     * 新建目录
     * @param folderPath String 如 c:/fqf
     * @return boolean
     */
    public void newFolder(String folderPath) {
      try {
        String filePath = folderPath;
        filePath = filePath.toString();
        java.io.File myFilePath = new java.io.File(filePath);
        if (!myFilePath.exists()) {
          myFilePath.mkdir();
        }
      }
      catch (Exception e) {
        System.out.println("新建目录操作出错");
        e.printStackTrace();
      }
    }
    /**
     * 新建文件
     * @param filePathAndName String 文件路径及名称 如c:/fqf.txt
     * @param fileContent String 文件内容
     * @return boolean
     */
    public void newFile(String filePathAndName, String fileContent) {
      try {
        String filePath = filePathAndName;
        filePath = filePath.toString();
        File myFilePath = new File(filePath);
        if (!myFilePath.exists()) {
          myFilePath.createNewFile();
        }
        FileWriter resultFile = new FileWriter(myFilePath);
        PrintWriter myFile = new PrintWriter(resultFile);
        String strContent = fileContent;
        myFile.println(strContent);
        resultFile.close();
      }
      catch (Exception e) {
        System.out.println("新建目录操作出错");
        e.printStackTrace();
      }
    }
    /**
     * 删除文件
     * @param filePathAndName String 文件路径及名称 如c:/fqf.txt
     * @param fileContent String
     * @return boolean
     */
    public void delFile(String filePathAndName) {
      try {
        String filePath = filePathAndName;
        filePath = filePath.toString();
        java.io.File myDelFile = new java.io.File(filePath);
        myDelFile.delete();
      }
      catch (Exception e) {
        System.out.println("删除文件操作出错");
        e.printStackTrace();
      }
    }

[1] [2] [3] 下一页

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训