Java学习笔记(三)——文件读写操作

package systemManager;

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.IOException;

/**
 * 
 * @author chencong 文件管理系统
 */
public class FileManager {

    /**
     * 在指定文件路径下创建文件夹或者是在项目根目录下创建文件夹
     * 
     * @param dirName
     * @return 返回创建文件夹的路径
     */
    public String createDir(String dirName) {
        if (dirName == null) {
            return null;
        }
        File file = new File(dirName);
        if (file.exists()) {
            file.delete();
        }
        file.mkdir();
        return file.getAbsolutePath();
    }

    /**
     * 在指定位置创建文件 返回文件的相对路径
     * 
     * @param fileName
     * @return 返回文件的绝对路径
     */
    public String createFile(String fileName) {
        if (fileName == null) {
            return null;
        }
        File file = new File(fileName);
        if (file.exists()) {
            file.delete();
        }
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file.getAbsolutePath();
    }

    /**
     * 向指定文件中写入内容,如果该文件不存在,则在指定位置创建该文件再写入
     * 
     * @param fileName
     *            指定文件的路径名称
     * @param value
     *            将要向该文件中写入的内容
     * @throws Exception
     *             抛出异常
     */
    public void setSomeByFile(String fileName, String value) throws Exception {
        if (fileName == null || value == null) {
            return;
        }
        File file = new File(fileName);
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream outputStream = new FileOutputStream(file, true);
        byte[] bytes = value.getBytes("utf-8");
        for (byte b : bytes) {
            outputStream.write(b);
        }
        outputStream.close();
    }

    /**
     * 返回指定位置文件的内容,如果该文件不存在或者该文件为文件夹直接返回null
     * 
     * @param fileName
     *            将要获得内容的文件路径
     * @return 返回null 或者 是文件的内容
     */
    public String getSomeByFile(String fileName) {
        if (fileName == null) {
            return null;
        }
        File file = new File(fileName);
        // 如果指定路径文件不存在 直接返回空 ,传入路径为目录则直接返回空
        if (!file.exists() || file.isDirectory()) {
            return null;
        }
        // 否则获取该文件中的内容
        StringBuffer sBuffer = new StringBuffer();
        try {
            String msg = null;
            // 创建字符流
            FileReader fileReader = new FileReader(file);
            // 创建字符缓冲流
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            while ((msg = bufferedReader.readLine()) != null) {
                sBuffer.append(msg);
            }
            bufferedReader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return sBuffer.toString();
    }

    /**
     * 将源文件内容复制到目的地址文件当中。
     * <P>
     * 如果源文件名为null 或者目的地址为null 则统一放回null
     * <p>
     * 如果原地址不为空且为文件则将其内容返回并copy进入目的地址
     * <p>
     * 如果目的地址文件不存在且目的地址正确为文件名,则创建一个文件
     * 
     * @param sourceName
     *            源文件名
     * @param destName
     *            目的文件名
     * @return 返回源文件当中的内容
     * @throws Exception
     */
    public String copyFileByName(String sourceName, String destName) throws Exception {
        if (sourceName == null || destName == null) {
            return null;
        }
        File sourceFile = new File(sourceName);
        // 获得源文件当中的内容
        String sourceMsg = null;
        if (!sourceFile.exists() || sourceFile.isDirectory()) {
            return null;
        }
        // 或得到源文件当中的内容 并且将其复制进目的文件当中
        sourceMsg = getSomeByFile(sourceFile.getAbsolutePath());
        File destFile = new File(destName);
        // 如果这个目的地址不存在 则创建这个目的地址
        if (!destFile.exists()) {
            destFile.createNewFile();
        }
        if (destFile.isDirectory()) {
            return null;
        }
        // 向目的地址当中写入内容
        FileOutputStream outputStream = new FileOutputStream(destFile);
        byte[] bytes = sourceMsg.getBytes("utf-8");
        for (byte b : bytes) {
            outputStream.write(b);
        }
        outputStream.close();
        return sourceMsg;
    }

    /**
     * 将指定地方的图片复制到目的地址,同上操作判断是否为空,返回boolean
     * <p>
     * 根据操作是否成功返回boolean
     * 
     * @param sourceName
     *            原地址路径
     * @param descName
     *            目的地址路径
     * @return 返回操作是否成功
     */
    public boolean copyImageByName(String sourceName, String descName) {
        if (sourceName == null || descName == null) {
            return false;
        }
        File sourceFile = new File(sourceName);
        if (!sourceFile.exists() || sourceFile.isDirectory()) {
            return false;
        }
        File descFile = new File(descName);
        if (descFile.exists() || descFile.isDirectory()) {
            return false;
        }
        try {
            FileInputStream inputStream = new FileInputStream(sourceFile);
            FileOutputStream outputStream = new FileOutputStream(descFile);
            byte[] buffer = new byte[100];
            int num = inputStream.read(buffer);
            while (num > 0) {
                outputStream.write(buffer, 0, num);
                num = inputStream.read(buffer);
            }
            inputStream.close();
            outputStream.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值