设为首页 加入收藏

TOP

计算机二级辅导:JAVA文件操作大全
2014-11-23 22:12:32 】 浏览:371
Tags:计算机 二级 辅导 :JAVA 文件 操作 大全

  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();
      }
    }


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇java的native使用方法 下一篇计算机二级JAVA辅导:四种Filter过..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目