Java本地文件操作

File类

文件名和文件夹的操作 

方法:

.exists()  判断是否存在

.isFile()  判断是否是一个文件

.isDirectory()  判断是否是一个路径

import java.io.File;
import java.io.IOException;

public class GenerateDemo02 {

	public static void main(String[] args) {
		//注意文件路径,前边不要有/字符
		//。。/是说向上跳转,一个代表一个文件夹
		
		File folder = new File("New Folder(Father)/New Folder(Son)");
		//创建此抽象路径名指定的目录,包括创建必需但不存在的父目录。对创建成功判断。
		folder.mkdirs();
//		//创建此抽象路径名指定的目录。
//		folder.mkdir();
		System.out.println("文件夹创建成功!");
		
			
		File f = new File("text.txt");
		if (f.exists()) {
			
			//文件重命名
			f.renameTo(new File("new test.txt"));
			
			//测试此抽象路径名表示的文件是否是一个目录。
			System.out.println("文件路径存在:" +f.isDirectory());
			//测试此抽象路径名表示的文件是否是一个标准文件。
			System.out.println("文件存在:" + f.isFile());
			f.delete();
			System.out.println("文件删除成功!");
			
			
		}else{
			System.out.println("文件不存在!");
			try {
				f.createNewFile();
				System.out.println("文件创建成功!");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("文件无法创建!");
			}
		}
	
	}
}

文件的属性设置

import java.io.File;
import java.io.IOException;

public class GenerateDemo02 {

	public static void main(String[] args) {
		File file = new File("test.file");
		try {
			file.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("文件创建成功!");
		if (file.exists()) {
			//文件设为可写
			file.setWritable(false);
			//文件设为可读
			file.setReadable(false);
			//文件设为只读
			file.setReadOnly();
			
		}else {
			System.out.println("文件不存在!");
		}
	
	}
}

文件遍历

import java.io.File;

public class GenerateDemo02 {

	public static void main(String[] args) {
		FilerScanner(new File("../fanxingDemo"), 1);

	}

	private static void FilerScanner(File dir, int table) {
		// TODO Auto-generated method stub
		if (dir.isDirectory()) {
			File[] next = dir.listFiles();
			for (int i = 0; i < next.length; i++) {
				for (int j = 0; j < table; j++) {
					System.out.print("|--");
				}
				System.out.println(next[i].getName());
				if (next[i].isDirectory()) {
					FilerScanner(next[i], table + 1);
				}
			}
		}

	}

}

文件读取与写入

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 GenerateDemo02 {

	public static void main(String[] args) {

		// 文件输入
		File file = new File("text.txt");
		if (file.exists()) {
			System.out.println("exist");
			try {
				// 用于文件输入的三个流:
				// FileInputStream;InputStreamReader;BufferedReader
				// 1.首先创建文件的输入流
				// 读入文件,文件的输入流是字节流
				FileInputStream fls = new FileInputStream(file);
				// 2.创建文件输入流的reader
				// 字符流,字节转化为字符需要UTF-8编码,否则出现乱码
				InputStreamReader isr = new InputStreamReader(fls, "UTF-8");
				// 3.创建带有缓冲区的reader
				BufferedReader br = new BufferedReader(isr);

				String line;
				while ((line = br.readLine()) != null) {
					System.out.println(line);
				}
				// 关闭输入流,先开启后关闭,顺序不能错
				br.close();
				isr.close();
				fls.close();

			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		// 文件输出
		File newfile = new File("text01.txt");
		try {
			// newfile.createNewFile();
			// 使用fileOutStream时,如果文件不存在,文件输出流会自动创建文件
			// 写入方法与读取方法基本一致
			FileOutputStream fos = new FileOutputStream(newfile);
			OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
			BufferedWriter bw = new BufferedWriter(osw);

			bw.write("写入第1行\n");
			bw.write("写入第2行\n");
			bw.write("写入第3行\n");
			bw.write("写入第4行\n");
			bw.write("写入第5行\n");

			bw.close();
			osw.close();
			fos.close();
			System.err.println("写入完成!");

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}


你可能感兴趣的:(Java本地文件操作)