JAVA文件操作集合

/*
 * Created on 2009-6-3
 * Copyright 2009 by www.xfok.net All Rights Reserved
 *
 */

package net.xfok.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @author YangHua
 *  转载请注明出处:http://www.xfok.net/2009/10/134499.html
 */
public class MyFileOper {
    /**
     * 写文件
     *
     * @param content
     *            内容
     * @param name
     *            文件名
     * @param path
     *            保存路径
     * @throws IOException
     */
    public void writeFile(String content, String name, String path)
            throws IOException {
        if (!new File(path).isDirectory())
            new File(path).mkdirs();
        FileWriter fw = new FileWriter(path + name);
        fw.write(content);
        fw.close();
    }

    public void writeFile(String content, String name, String path,
            String charset) throws IOException {
        if (!new File(path).isDirectory())
            new File(path).mkdirs();
        try {
            OutputStreamWriter out = new OutputStreamWriter(
                    new FileOutputStream(path + name), charset);
            out.write(content);
            out.flush();
            out.close();
            System.out.println("success...");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
   
    public void writeFile(byte[] encrypt, String name, String path)
            throws IOException {
        if (!new File(path).isDirectory())
            new File(path).mkdirs();
        RandomAccessFile randomFile = new RandomAccessFile(path + name, "rw");
        long fileLength = randomFile.length();
        randomFile.seek(fileLength);
        randomFile.write(encrypt);
        randomFile.close();
    }

    /**
     * 读文件
     *
     * @param name
     *            文件名
     * @param path
     *            存储路径
     * @return
     * @throws IOException
     */
    public String readFile(String name, String path) throws IOException {
        StringBuffer sb = new StringBuffer();
        FileReader fr = new FileReader(path + name);
        int i = 0;
        while ((i = fr.read()) != -1) {
            sb.append((char) i);
        }
        fr.close();
        return sb.toString();
    }

    public String readFile(String name, String path, String charset)
            throws IOException {
        StringBuffer content = new StringBuffer();
        try {
            InputStreamReader read = new InputStreamReader(new FileInputStream(
                    path + name), charset);
            BufferedReader reader = new BufferedReader(read);
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content.toString();
    }

    /**
     * 删除文件
     *
     * @param path
     *            文件路径
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     */
    public boolean delFile(String path) throws FileNotFoundException,
            IOException {
        try {
            File file = new File(path);
            if (!file.isDirectory()) {
                System.out.println("1");
                file.delete();
            } else if (file.isDirectory()) {
                System.out.println("2");
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    File delfile = new File(path + "//" + filelist[i]);
                    if (!delfile.isDirectory()) {
                        System.out.println("path=" + delfile.getPath());
                        System.out.println("absolutepath="
                                + delfile.getAbsolutePath());
                        System.out.println("name=" + delfile.getName());
                        delfile.delete();
                        System.out.println("删除文件成功");
                    } else if (delfile.isDirectory()) {
                        delFile(path + "//" + filelist[i]);
                    }
                }
                file.delete();
            }
        } catch (FileNotFoundException e) {
            System.out.println("delFile()   Exception:" + e.getMessage());
        }
        return true;
    }

    /**
     * 读取某个文件夹下的所有文件夹和文件
     *
     * @param path
     *            文件夹
     * @throws FileNotFoundException
     * @throws IOException
     * @return boolean
     */
    public boolean readFile(String path) throws FileNotFoundException,
            IOException {
        try {
            File file = new File(path);
            if (!file.isDirectory()) {
                System.out.println("文件");
                System.out.println("path=" + file.getPath());
                System.out.println("absolutepath=" + file.getAbsolutePath());
                System.out.println("name=" + file.getName());

            } else if (file.isDirectory()) {
                System.out.println("文件夹");
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    File readfile = new File(path + "//" + filelist[i]);
                    if (!readfile.isDirectory()) {
                        System.out.println("path=" + readfile.getPath());
                        System.out.println("absolutepath="
                                + readfile.getAbsolutePath());
                        System.out.println("name=" + readfile.getName());

                    } else if (readfile.isDirectory()) {
                        readFile(path + "//" + filelist[i]);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("readFile()   Exception:" + e.getMessage());
        }
        return true;
    }

    /**
     * 得到文件夹中的文件列表
     *
     * @param path
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     */
    public List<String> readFiles(String path) throws FileNotFoundException,
            IOException {
        List<String> list = new ArrayList();
        try {
            File file = new File(path);
            list = Arrays.asList(file.list());
        } catch (Exception e) {
            System.out.println("readFile()   Exception:" + e.getMessage());
        }
        return list;
    }

}

转载请注明出处:http://www.xfok.net/2009/10/134499.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值