java中File文件的操作

其中包括创建文件,删除文件,删除文件夹,重命名文件,读取文件,复制文本文件,复制其他文件

package com.rj.bd.txt;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * @desc 文件操作类
 * @author ws
 * @time 2018-10-10
 */
public class WenJian {
	/**
	 * @desc 创建文件
	 * @param pathname
	 * @throws IOException
	 */
	public void chuangJian(String pathname) throws IOException {
		File file = new File(pathname);
		if (file.isFile() && file.exists()) {
			System.out.println("文件存在!");
		} else {
			if (file.isDirectory()) {
				System.out.println("文件夹存在!");
                file.createNewFile();// 创建文件
				System.out.println("文件创建成功!");
			} else {
				System.out.println("文件夹不存在!");
				String newPathname = "";
				String[] arr = pathname.split("/");
				for (int i = 0; i < arr.length - 1; i++) {
					newPathname += arr[i] + "/";
				}
				File dir = new File(newPathname);
				dir.mkdirs();// 创建文件夹
				System.out.println("文件夹创建成功!");
				file.createNewFile();// 创建文件
				System.out.println("文件创建成功!");
			}
		}
	}

	/**
	 * @desc 删除文件
	 * @param pathname
	 */
	public void shanChu(String pathname) {
		File file = new File(pathname);
		file.delete();
		System.out.println("文件删除成功!");
	}

	/**
	 * @desc 删除文件夹
	 * @param file
	 */
	public void shanChuWenJianJia(File file) {
		if (file.isFile()) {
			file.delete();
		} else {
			String[] child = file.list();
			for (String everyChild : child) {
				file = new File(file.getAbsoluteFile() + "/" + everyChild);
				shanChuWenJianJia(file);
			}
			file.delete();
			System.out.println("文件夹删除成功!");
		}
	}

	/**
	 * @desc 重命名文件
	 * @param first
	 * @param second
	 */
	public void chongMingMing(String first, String second) {
		String[] arr = first.split("/");
		String newPath = "";
		for (int i = 0; i < arr.length - 1; i++) {
			newPath += arr[i];
		}
		newPath += second;
		File f1 = new File(first);
		File f2 = new File(newPath);
		f1.renameTo(f2);
		System.out.println("重命名成功!");
	}

	/**
	 * @desc 读取文件
	 * @param path
	 * @throws IOException
	 */
	public void duQu(String path) throws IOException {
		File file = new File(path);
		FileInputStream inputStream = new FileInputStream(file);// 将file转为流
		InputStreamReader inputStreamReader = new InputStreamReader(
				inputStream, "GBK");// 将当前流进行编码设置
		BufferedReader bufferedReader = new BufferedReader(inputStreamReader);// 将携带有数据格式的流,装入到缓冲区内
		String everyLine = "";
		while ((everyLine = bufferedReader.readLine()) != null) {
			System.out.println(everyLine);
		}
		bufferedReader.close();
		inputStreamReader.close();
		inputStream.close();
		System.out.println("------读取完毕------");
	}

	/**
	 * @desc 复制文本
	 * @param path
	 * @param path2
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public void FuZhi(String path, String path2) throws FileNotFoundException,
			IOException {
		File file = new File(path);
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(new FileInputStream(file), "GBK"));
		File file2 = new File(path2);
		BufferedWriter bufferedWriter = new BufferedWriter(
				new OutputStreamWriter(new FileOutputStream(file2), "GBK"));
		String everyLine = "";
		while ((everyLine = bufferedReader.readLine()) != null) {
			bufferedWriter.write(everyLine);
		}
		bufferedWriter.close();
		bufferedReader.close();
		System.out.println("复制成功");
	}

	/**
	 * @desc 复制文件
	 * @param path1
	 * @param path2
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public void fuZhiWenJian(String path1, String path2)
			throws FileNotFoundException, IOException {
		// 缓存字节输入流
		BufferedInputStream fis = new BufferedInputStream(new FileInputStream(
				path1));
		// 缓存字节输出流
		BufferedOutputStream fos = new BufferedOutputStream(
				new FileOutputStream(path2));
		// 读取和写入信息
		int len = 0;
		// 创建一个字节数组,当做缓冲区
		byte[] b = new byte[1024];// 构造一个长度为1024的字节数组,相当于是1kb的空间
		while ((len = fis.read(b)) != -1) {
			fos.write(b, 0, len);
		}
		// 关闭流 先开后关 后开先关
		fos.close(); // 后开先关
		fis.close(); // 先开后关
		System.out.println("复制成功!");
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40923411/article/details/83036077