Java 本地文件常用操作

Java  本地常用的操作


package LmtPackage03;

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;
import java.io.UnsupportedEncodingException;

public class Lmt03 {

	public static void main(String[] args) {
		File file = new File("hello.txt");// 当前目录创建文件
		// new File("../hello.txt");上一级创建文件
		// new File("../../hello.txt");上两级创建文件
		// new File("x/hello.txt");当前文件夹下的X文件夹下创建文件
		// 创建文件
		try {
			file.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("文件创建失败。");
		}
		// 文件重命名
		File nameto1 = new File("new hello.txt");
		file.renameTo(nameto1);
		// 跨目录重命名文件:文件夹结构必须处于同一个分区
		File nameto2 = new File("src/new hello.txt");
		file.renameTo(nameto2);
		// 判断文件是否存在
		file.exists();
		// 判断是否是路径(文件夹)
		file.isDirectory();
		// 读取文件名称:创建文件时指定的名称
		System.out.println("文件名称:" + file.getName());
		// 读取文件路径
		System.out.println("文件路径:" + file.getPath());
		// 读取文件绝对路径
		System.out.println("文件绝对路径:" + file.getAbsolutePath());
		// 获取文件父级路径
		System.out.println("父级路径:" + new File(file.getAbsolutePath()).getParent());
		// 读取文件大小
		System.out.println("文件大小:" + file.length() + "byte" + (float) file.length() / 1000 + "KB");

		System.out.println("判断文件是否被隐藏:" + file.isHidden());

		System.out.println("判断文件是否为文件夹:" + file.isDirectory());

		System.out.println("判断文件是否可读:" + file.canRead());
		// 文件设置为可读
		file.setReadable(true);
		System.out.println("判断文件是否可写:" + file.canWrite());
		// 文件设置为可写
		file.setWritable(true);
		// 文件设置为只读
		file.setReadOnly();

		// 读取文件内容
		if (file.exists()) {
			System.err.println("file exist");
			try {

				// 获取文件输入流
				FileInputStream fileInputStream = new FileInputStream(file);// 字节流
				// 创建文件输入流的Reader
				InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");// 字符流
				// 创建带有缓冲区的Reader
				BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
				String Line;
				while ((Line = bufferedReader.readLine()) != null) {
					System.out.println(Line);
				}
				// 关闭输入流,注意关闭顺序,先打开后关闭,后打开先关闭
				bufferedReader.close();
				inputStreamReader.close();
				fileInputStream.close();

			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		try {
			// 写文件
			File newfile = new File("new file.txt");
			// 创建文件输出流,文件流创建之后,如果文件不存在,文件输出流则自动创建文件
			FileOutputStream fileOutputStream = new FileOutputStream(newfile);
			// 创建文件输出流的Writer
			OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
			// 创建带有缓冲区的Writer
			BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);

			// 写入数据,覆盖方式写入数据
			bufferedWriter.write("第一行\n");
			bufferedWriter.write("第二行\n");

			// 关闭输出流,与输入流关闭的顺序一样
			bufferedWriter.close();
			outputStreamWriter.close();
			fileOutputStream.close();

			System.out.println("写入完成。");

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		
		
		//创建文件夹
		File folder = new File("folder");
		//File folder = new File("folder/one/two/three/main");
		if (folder.mkdir()) {
			/*创建多级文件目录
			 * folder1.mkdirs();
			 */
			System.out.println("文件夹创建成功!");
		}
		else {
			System.out.println("文件夹创建失败!");//一:文件已存在  二:不存在的多级目录结构
		}
		
		//文件重命名,文件处在同一个分区,如果目标文件已存在,重命名失败
		File newfolder = new File("new folder");
		/*
		 * "剪切"-"粘贴"文件夹
		 * File newfolder = new File("new folder/two");
		 * File newfolder = new File("new folder/three");
		 */
		if (folder.renameTo(newfolder)) {
			System.out.println("文件夹重命名成功!");
		}
		else {
			System.out.println("文件夹重命名失败!");
		}
		
		//文件夹删除
		folder.delete();//return true or false,空文件夹才能删除
		
		//遍历文件夹
		printFile(new File("C:/Users/LinMT/Desktop"), 1);
	}
	
	public static void printFile(File file , int tab) {
		if (file.isDirectory()) {//判断是否是文件夹
			File next[] = file.listFiles();
			for (int i = 0; i < next.length; i++) {
				for (int j = 0; j < tab; j++) {
					System.out.print("|--");//输出缩进
				}
				System.out.println(next[i].getName());
				if (next[i].isDirectory()) {//判断是否是文件夹
					printFile(next[i], tab+1);
				}
			}
		}
	}

}


猜你喜欢

转载自blog.csdn.net/Coloured_Glaze/article/details/52161871
今日推荐