本文主要介绍 Java IO 中几个常见的操作,读取字符,写入字符,复制文件,复制文件夹等。
直接上代码啦。
- package com.liuyanzhao.io;
- import java.io.*;
- import java.util.Date;
- public class FileDemo {
- /**
- * 输出文件基本信息
- * @param filePath 文件的路径
- */
- public static void getFileInfo(String filePath) {
- File file = new File(filePath);
- System.out.println("文件名称:"+file.getName());
- System.out.println("文件是否存在:"+file.exists());
- System.out.println("文件相对路径:"+file.getPath());
- System.out.println("文件绝对路径:"+file.getAbsolutePath());
- System.out.println("是否可执行文件:"+file.canExecute());
- System.out.println("文件可以读取:"+file.canRead());
- System.out.println("文件可以写入:"+file.canWrite());
- System.out.println("文件上级路径:"+file.getParent());
- System.out.println("文件大小:"+file.length()+"B");
- System.out.println("文件最后修改时间:"+ new Date(file.lastModified()));
- System.out.println("是否文件类型:"+file.isFile());
- System.out.println("是否文件夹类型:"+file.isDirectory());
- }
- /**
- * 读取文件字符
- * @param filePath 文件路径
- * @throws IOException
- */
- public static void readFile(StringBuilder sb,String filePath) throws IOException {
- File file = new File(filePath);
- if(file.exists()) {
- FileReader fileReader = new FileReader(file);//创建文件字符输入流
- char[] data = new char[20];//表示一次循环读取20个字符
- int rs = 0;
- //fileReader.read()将字符读入data数组中,并返回读取字符的数量
- while((rs=fileReader.read(data))>0) {
- sb.append(data,0,rs);
- }
- fileReader.close();
- } else {
- System.out.println("File Not Found!");
- }
- }
- /**
- * 写入字符到文件中
- * @param sb 写入的内容
- * @param filepath 被写入的文件路径
- * @throws IOException
- */
- public static void writeFile(StringBuilder sb,String filepath) throws IOException {
- File file = new File(filepath);
- if(file.exists()) {
- FileWriter fileWriter = new FileWriter(file);//创建文件字符输出流
- fileWriter.write(sb.toString());
- fileWriter.close();
- } else {
- System.out.println("File Not Found!");
- }
- }
- /**
- * 新建文件
- * @param filepath 文件路径
- * @throws IOException
- */
- public static void createFile(String filepath) throws IOException {
- File file = new File(filepath);
- file.createNewFile();
- }
- /**
- * 复制文件
- * @param sfpath 源文件路径
- * @param dfpath 目标文件路径
- * @throws IOException
- */
- public static void fileCopy(String sfpath,String dfpath) throws IOException {
- File sourFile = new File(sfpath);
- File desFile = new File(dfpath);
- if(!sourFile.exists()) {
- System.out.println("Source File Not Found");
- System.exit(0);
- }
- desFile.createNewFile(); //新建文件
- FileInputStream fin = new FileInputStream(sourFile);
- FileOutputStream fout = new FileOutputStream(dfpath);
- byte[] data = new byte[512];
- int rs = -1;
- while((rs=fin.read(data))>0) {
- fout.write(data,0,rs);
- }
- fout.close();
- fin.close();
- System.out.println("文件复制成功!");
- }
- /**
- * 复制源文件夹的内容到目标文件夹中,需要递归操作
- * @param sourFiles 源文件夹下的子文件数组
- * @param desFile 目标文件
- * @throws IOException
- */
- public static void copyFolder(File[] sourFiles,File desFile) throws IOException {
- if(!desFile.exists()) {
- desFile.mkdir();
- }
- //复制操作
- for(int i=0;i<sourFiles.length;i++) {
- //如果文件是文件类型,则复制文件
- if(sourFiles[i].isFile()) {
- FileInputStream fin = new FileInputStream(sourFiles[i]);
- FileOutputStream fout = new FileOutputStream(
- new File(desFile.getPath()+
- File.separator+sourFiles[i].getName())
- );
- int count = fin.available();
- byte[] data = new byte[count];
- if((fin.read(data) != -1)) {
- fout.write(data);
- }
- fout.close();
- fin.close();
- }
- //如果文件是文件夹类型,则新建文件夹,并递归操作
- if(sourFiles[i].isDirectory()) {
- File des = new File(desFile.getPath()+
- File.separator+sourFiles[i].getName());
- des.mkdir();
- copyFolder(sourFiles[i].listFiles(),des);//递归调用方法本身
- }
- }
- }
- /**
- * 删除文件
- * @param filepath 文件路径
- */
- public static void deleteFile(String filepath) {
- File file = new File(filepath);
- if(!file.exists()) {
- System.out.println("File Not Found");
- }
- boolean rs = file.delete();
- if(rs) {
- System.out.println("Delete Successfully");
- } else {
- System.out.println("Delete Failed");
- }
- }
- public static void main(String args[]) throws IOException {
- //获得文件信息测试
- // String filepath = "/Users/liuyanzhao/Desktop/Test.txt";//默认路径为项目根目录
- // getFileInfo(filepath);
- //读取文件字符测试
- // String filepath2 = "/Users/liuyanzhao/Desktop/Test.txt";
- // StringBuilder sb = new StringBuilder();
- // readFile(sb,filepath2);
- // System.out.println(sb);
- //写入字符测试
- // StringBuilder sb2 = new StringBuilder("你好啊,我是言曌!");
- // String filepath3 = "/Users/liuyanzhao/Desktop/Test.txt";
- // writeFile(sb2,filepath3);
- //新建文件测试
- // String filepath4 = "/Users/liuyanzhao/Documents/temp/1.txt";
- // createFile(filepath4);
- //复制文件测试
- // String sfilepath = "/Users/liuyanzhao/Desktop/uploads.zip";
- // String dfilepath = "/Users/liuyanzhao/Desktop/uploads2.zip";
- // fileCopy(sfilepath,dfilepath);
- //复制文件夹测试
- // String soufolderpath = "/Users/liuyanzhao/Desktop/uploads";
- // String desfolderpath = "/Users/liuyanzhao/Documents/temp";
- // File souFolder = new File(soufolderpath);
- // File desFolder = new File(desfolderpath);
- // copyFolder(souFolder.listFiles(),desFolder);
- //删除文件测试
- // String filepath5 = "/Users/liuyanzhao/Documents/temp/1.txt";
- // deleteFile(filepath5);
- }
- }
上面的代码只介绍了一些基本的简单的操作,以后遇到其他的再补充吧。最后再上一张结构图吧