Java-io

简单的文件读写

package com.file;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class ReadWriteFile {
	public static void main(String[] args) throws Exception {
		ReadWriteFile file = new ReadWriteFile();
		file.prepareReadFile("f:/java/javaee/luo", "age.txt");

		File file2 = new File("f:/java/javaee/luo/age2.txt");
		/**
		 * System.getProperty("file.encoding") 获得当前系统的字符编码
		 */
		System.out.println(file.readFile(file2, System
				.getProperty("file.encoding")));

		file.writeFile(new File("f:/java/javaee/luo/age.txt"), System
				.getProperty("file.encoding"), file.readFile(file2, System
				.getProperty("file.encoding")));

		file.deleteFile(new File("f:/java/javaee/luo/age3.txt"));

		file.binaryReadWrite("F:/java/Kalimba.mp3", "F:/java/javaee/luo",
				"music", "Kalimba1.mp3");
		file.fileReadWrite("f:/java/javaee/luo/aa.txt", "f:/java/javaee/luo",
				"luo.txt");
	}

	/**
	 * 读取并复制二进制文件
	 * 
	 * @throws IOException
	 */
	public void binaryReadWrite(String file, String file2, String music,
			String filename) throws IOException {
		InputStream inputStream = null;
		OutputStream outputStream = null;
		File file3 = new File(file2 + "/" + music);
		file3.mkdir();
		try {
			inputStream = new FileInputStream(new File(file));
			byte[] b = new byte[4 * 1024];
			outputStream = new FileOutputStream(file3 + "/" + filename);
			while (inputStream.read(b) != -1) {
				outputStream.write(b);
			}

		} catch (IOException e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
			System.out.println(e);
		} finally {
			outputStream.flush();
			outputStream.close();
			inputStream.close();
		}
	}

	/**
	 * 读取文件
	 * 
	 * @param file
	 * @param encoding
	 * @return
	 * @throws IOException
	 */
	public String readFile(File file, String encoding) throws IOException {
		BufferedReader bufferedReader = null;
		try {
			bufferedReader = new BufferedReader(new InputStreamReader(
					new FileInputStream(file), encoding));
			StringBuffer buffer = new StringBuffer();
			char[] chars = new char[4096];
			int lenght = 0;
			while (0 < (lenght = bufferedReader.read(chars))) {
				buffer.append(chars, 0, lenght);
			}
			return buffer.toString();
		} finally {
			try {
				if (bufferedReader != null) {
					bufferedReader.close();
				}
			} catch (Exception e) {
				// TODO: handle exception
				throw new RuntimeException(e);
			}
		}
	}

	/**
	 * 写入文件
	 * 
	 * @param file
	 * @param encoding
	 * @param reader
	 * @throws IOException
	 */
	public void writeFile(File file, String encoding, String reader)
			throws IOException {
		BufferedWriter writes = null;
		try {
			writes = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(file, false), encoding));
			writes.write(reader);
		} finally {
			if (writes != null) {
				writes.close();
			}
		}

	}

	/**
	 * 删除文件
	 * 
	 * @param file
	 */
	public void deleteFile(File file) {
		file.delete();
	}

	/**
	 * 读取写入文件
	 * 
	 * @param file1
	 * @param file2
	 * @param filename
	 * @throws IOException
	 */
	public void fileReadWrite(String file1, String file2, String filename)
			throws IOException {
		BufferedReader bufReader = null;
		InputStream iStream = null;
		BufferedOutputStream bWriter = null;
		File file = new File(file2, filename);
		if (!file.exists()) {
			file.createNewFile();
		}
		try {
			iStream = new FileInputStream(new File(file1));
			bufReader = new BufferedReader(new InputStreamReader(iStream));
			bWriter = new BufferedOutputStream(new FileOutputStream(file));
			String line = null;
			if ((line = bufReader.readLine()) != null) {
				bWriter.write(line.getBytes());
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			bWriter.flush();
			bWriter.close();
			bufReader.close();
			iStream.close();
		}
	}

	/**
	 * 查看目录文件是否存在并新建
	 * 
	 * @param path
	 * @param filename
	 * @throws Exception
	 */
	public void prepareReadFile(String path, String filename) throws Exception {
		File folder = new File(path);
		File file = new File(path, filename);
		if (!folder.exists()) {
			System.out.println("新建目录和文件");
			folder.mkdirs();// 新建目录及其父目录
			file.createNewFile();// 新建文件
		} else if (!file.exists()) {
			System.out.println("新建文件");
			file.createNewFile();// 新建文件
		}
	}
}

  

posted @ 2012-09-03 13:33  前天  阅读(216)  评论(0编辑  收藏  举报