天天看点

Java操作文件

Java中操作文件主要使用io包中的输入输出流,我这里要说的是操作普通文件,如jpg、pdf等字节流文件和txt等字符流文件。XML文件、Excel和属性文件有专门的工具去操作,这里就不再赘述。

Java中io流可分为字节流和字符流两类。字节流主要用来操作字节类型的文件,如jpg、pdf、rar等,字符流主要用来操作字符类型的文件,如txt、html、jsp等等。根据流的方向又可分为输入流和输出流,输入流是指从文件中将内容读到内存中的流,输出流是指将内存中的内容写到文件中的流。

读取字节文件主要使用FileInputStream类。输出字节文件主要使用FileOutputStream,它有几个重载的构造方法。在这些构造方法中,首先要传入的就是要输出的文件对象或者问价路径,其次是一个Boolean型的append参数,这个参数如果为true,表示将内容追加到文件的后边。如果为false表示用内容替换文件中的字符。

读取字符文件主要使用FileInputStream。由于每个字符文件的编码可能不同,所以使用InputStreamReader将前边的FileInputStream按照指定字符集包装一下,在InputStreamReader的构造方法中会有一个参数来指定字符集。为了提高读取的效率,用BufferedReader将前边的InputStreamReader包转一下,这样一次就可以读入一行,否则一次只能读取一个字符。写入字符文件主要使用FileOutputStream,可以指定是追加到文件中,还是替换文件原有的内容。用OutputStreamWriter将FileOutputStream包装一下,这样就可以指定输出的字符集。为了提高写入的效率,使用BufferedWriter将OutputStreamWriter包装起来。

具体代码如下所示:

package com.test.file;

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.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

public class FileOperation {
	private static final int SIZE = 1024;

	public static void mergeByteFile(String srcFilePath, String targetFilePath) throws IOException {
		File srcFile = new File(srcFilePath);
		File targetFile = new File(targetFilePath);
		mergeByteFile(srcFile, targetFile);
	}

	public static void mergeByteFile(File srcFile, File targetFile) throws IOException {
		byte[] buff = new byte[SIZE];
		FileInputStream in = null;
		FileOutputStream out = null;
		try {
			in = new FileInputStream(srcFile);
			out = new FileOutputStream(targetFile, true);
			// 为了提高效率,将内容读到byte数组中
			int data = in.read(buff);
			while (data != -1) {
				out.write(buff);
				data = in.read(buff);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.close();
			}
		}
	}

	public static void readCharacterFile(String filePath) throws IOException {
		File file = new File(filePath);
		readCharacterFile(file);
	}

	public static void readCharacterFile(File file) throws IOException {
		InputStream inputStream = null;
		try {
			inputStream = new FileInputStream(file);
			// 以指定的编码格式读入文件
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
			// 将其包装为BufferedReader类,以便可以一次读入一行
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			// 读取一行
			String content = bufferedReader.readLine();
			// while (bufferedReader.ready()) {
			while (content != null) {
				System.out.println(content);
				content = bufferedReader.readLine();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} finally {
			if (inputStream != null) {
				inputStream.close();
			}
		}
	}

	public static void mergeCharacterFile(String srcFilePath, String targetFilePath) throws IOException {
		File srcFile = new File(srcFilePath);
		File targetFile = new File(targetFilePath);
		mergeCharacterFile(srcFile, targetFile);
	}

	public static void mergeCharacterFile(File srcFile, File targetFile) throws IOException {
		InputStream inputStream = null;
		OutputStream outputStream = null;
		StringBuffer stringBuffer = new StringBuffer("\r\n");
		try {
			// 以srcFile为源构造文件输入流
			inputStream = new FileInputStream(srcFile);
			// 以指定的编码格式读入文件
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
			// 将其包装为BufferedReader类,以便可以一次读入一行
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			String content = bufferedReader.readLine();

			// 以targetFile为目标构造文件输出流
			outputStream = new FileOutputStream(targetFile, true);
			// 以指定的编码格式输出
			OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
			// 构造缓存输出流
			BufferedWriter bufferedWrite = new BufferedWriter(outputStreamWriter);

			// 内容不为null就一直读,将读出的内容放在StringBuffer中
			while (content != null) {
				// 在读出的每行的后边都加上换行符
				stringBuffer.append(content + "\r\n");
				content = bufferedReader.readLine();
			}

			// 将读出的内容一次性写入到目标文件中
			bufferedWrite.write(stringBuffer.toString());
			bufferedWrite.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} finally {
			if (inputStream != null) {
				inputStream.close();
			}
			if (outputStream != null) {
				outputStream.close();
			}
		}
	}

	public static void main(String[] args) {
		File srcFile = new File("H:/ioTest/Ajax-Web2.pdf");
		File targetFile = new File("H:/ioTest/Ajax.pdf");
		try {
			mergeByteFile(srcFile, targetFile);
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			readCharacterFile("E:/Workspace/MyEclipse6/tms/WebRoot/error.jsp");
			mergeCharacterFile("H:/ioTest/route.txt", "H:/ioTest/study.txt");
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("end......");
	}

}