博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java-io
阅读量:6602 次
发布时间:2019-06-24

本文共 4421 字,大约阅读时间需要 14 分钟。

简单的文件读写

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();// 新建文件		}	}}

  

转载于:https://www.cnblogs.com/andicu/archive/2012/09/03/2668657.html

你可能感兴趣的文章
Python正则表达式初识(十)附正则表达式总结
查看>>
APICLOUD 1.1.0 开发环境搭建
查看>>
《Cadence 16.6电路设计与仿真从入门到精通》——导读
查看>>
Confluence 6 如何让我的小组成员知道那些内容是重要的
查看>>
找到一个适合的分布式文件系统之各种分布式文件系统优缺点对比
查看>>
httpd基本配置
查看>>
索引失效的几个原因
查看>>
关于多线程中使用while做循环而不使用if的解释
查看>>
欢迎你,企业基础架构CCIE,RS CCIEv5.0的升级版新时代迎合自动化运维的网工顶级认证...
查看>>
js typoeof用法
查看>>
五险一金,你清楚吗?
查看>>
Ip核_fifo
查看>>
基础 JavaScript 实例
查看>>
自定义pageControl
查看>>
repquota命令--Linux命令应用大词典729个命令解读
查看>>
我的友情链接
查看>>
设置vs解决方案跟随右边cpp
查看>>
Linux Administration
查看>>
如何使版面富有节奏感
查看>>
rabbitmq 管理及常用命令
查看>>