java对文件的操作

对文件进行创建,写入数据,阅读数据,删除文件的操作

package pri.lsx.test.file;

import java.io.*;

/**
 * 对文件操作
 * 1:本文参考https://jingyan.baidu.com/article/154b46316536c428ca8f41f5.html;
 * 2:本代码创建的是一个txt文件
 *
 * @author lisx
 * @date 2018-05-04
 */
public class FileOperate {
	public static void main(String[] args) throws IOException {

		// 创建txt文件
		creatFile();

		// 文件中写入内容
		writFile();

		// 读文件中的信息
		readFile();

		// 删除文件
		deleteFile();

	}

	/**
	 * 创建一个文件
	 */
	private static void creatFile() throws IOException {

		// 指定文件路径创建 File 对象
		File theNewFile = new File("D:/NewFileByJava.txt");

		// 判断文件是否存在,如果不存在,则尝试创建文件
		if (!theNewFile.exists()) {
			try {

				// 调用 createNewFile 放创建文件,并返回创建结果
				boolean createResult = theNewFile.createNewFile();
				if (createResult) {
					System.out.println("文件创建成功!");
				} else {
					System.out.println("文件创建失败!");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 往文件中写入内容
	 */
	private static void writFile() throws IOException {

		// 创建一个文件对象,我们刚刚创建这个文件
		File theNewFile = new File("D:/NewFileByJava.txt");

		// 创建一个文件输出流对象
		FileOutputStream fos = new FileOutputStream(theNewFile);

		// 创建带缓冲区的字符流输出对象,通过这个对象,我们的内容输出效率较高
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

		// 输出第一行信息
		bw.write("第一行信息");

		// 输出回车符(\r)换行符(\n)
		// 对于 Windows系统,我们输出这两个字符后,在文件中就表现为"另起一行"
		bw.write("\r\n");
		bw.write("第二行信息");

		// 将缓冲区的内容输出到文件中
		bw.flush();

		// 关闭输入流对象!!这是最佳实践!!
		// 任何文件流操作完毕后,必须将流对象关闭!!
		fos.close();
		bw.close();
		System.out.println("文件输入完毕!");
	}

	/**
	 * 从文件中读取内容
	 */
	private static void readFile() throws IOException {

		// 创建一个文件对象,我们刚刚创建这个文件
		File theNewFile = new File("D:/NewFileByJava.txt");

		// 创建文件输入流对象,注意,这里的输入流和输出流是相对程序而言的!
		// 从程序中往外输出就是输出流,往程序中读入就是输入流
		FileInputStream fis = new FileInputStream(theNewFile);

		// 创建带缓冲区的字符流输入对象
		BufferedReader br = new BufferedReader(new InputStreamReader(fis));
		String line = null;

		// 调用br.readLine(), 逐行读入所有数据,读到文件末尾,返回 null
		while ((line = br.readLine()) != null) {
			System.out.println(line);
		}

		// 操作完毕,关闭所有流对象
		fis.close();
		br.close();
	}

	/**
	 * 删除文件
	 *     1:这个方法同样适用于删除文件夹,在 JAVA 中,一个文件夹也是一个文件,也通过 File 对象来表示!
	 */
	private static void deleteFile() {

		// 创建一个文件对象
		File theNewFile = new File("D:/NewFileByJava.txt");

		// 调用 delete 方法将自己删除,并返回删除结果
		boolean deleteResult = theNewFile.delete();
		if (deleteResult) {
			System.out.println("文件删除成功!");
		} else {
			System.out.println("文件删除失败!");
		}
	}
}

执行结果

文件输入完毕!
第一行信息
第二行信息
文件删除成功!

复制文件(使用nio)

package pri.lsx.test.file;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 使用NIO对文件进行复制操作
 * @author lisx
 * @date 2018-05-05
 */
public class CopyFile {
	public static void main(String[] args) throws IOException {
		copy("D:/resourceFileName.txt", "D:/newFileName.txt");
	}

	/**
	 * @param resourceFilePath 被复制的文件路径;aimsFilePath 新文件的路径
	 */
	private static void copy(String resourceFilePath, String newFilePath) throws IOException {

		// 还是使用io中的方式
		FileOutputStream fileOutputStream = new FileOutputStream(newFilePath);
		FileInputStream fileInputStream = new FileInputStream(resourceFilePath);

		// 分别生成读与写的文件通道;该方式是nio才有的
		FileChannel readChannel = fileInputStream.getChannel();
		FileChannel writChannel = fileOutputStream.getChannel();

		// 得到缓冲,没次缓存1k的量;该方式是nio才有的
		ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

		while (true) {
			byteBuffer.clear(); // 每次都使参数初始化
			int len = readChannel.read(byteBuffer);
			if (len == -1) {
				break;
			}
			byteBuffer.flip();  // 读写转换
			writChannel.write(byteBuffer);
		}
	}
}


猜你喜欢

转载自blog.csdn.net/lsx2017/article/details/80197161