java IO

1、File常用方法
 file.createNewFile();创建文件
 file.delete();删除文件
 file.mkdir();创建目录
 file.Directory();判断一个指定的路径是否为目录

2、OutputStreramWriter 和InputStreamReader类
 整个IO类中除了字节流和字符流还包括字节和字符转换流。
 OutputStreramWriter将输出的字符流转化为字节流
 InputStreamReader将输入的字节流转换为字符流
 但是不管如何操作,最后都是以字节的形式保存在文件中的。

 Writer out=new OutputStreamWriter(new FileOutputStream(file));将字节输出流转化为字符输出流
 Reader read=new InputStreamReader(new FileInputStream(file));将字节输入流变为字符输入流

3、Scanner类,略
4、SequenceInputStream主要用来将2个流合并在一起,比如将两个txt中的内容合并为另外一个txt。
5、ObjectOutputStream类,对象需要序列化,实现Serializable接口
 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
 oos.writeObject(obj);

 ObjectInputStream input = new ObjectInputStream(new FileInputStream(file));

 Object obj = input.readObject();


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;


public class IOTest {
	
	private static String pathFile = "d:" + File.separator + "hello.txt";
	private static String pathDir = "d:" + File.separator + "hello";
	
	public static void main(String[] args) {
//		createFileTxt();
//		deleteFile();
//		createMkdir();
//		fileList();
//		File file = new File("d:" + File.separator + "coolstor" + File.separator);
//		searchAllFile(file);
//		writeString("你好!");
//		readString();
//		writeStr("字符流!");
		readStr();
		fileCopy();
	}
	
	/**
	 * 创建文件
	 */
	private static void createFileTxt(){
		File file = new File(pathFile);
		try {
			file.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除文件
	 */
	private static void deleteFile(){
		File file = new File(pathFile);
		if(file.exists()){
			file.delete();
		}else{
			System.out.println("文件不存在!");
		}
	}
	
	/**
	 * 创建目录
	 */
	private static void createMkdir(){
		File file = new File(pathDir);
		file.mkdir();
	}
	
	/**
	 * 使用list列出指定目录的全部文件
	 * listFiles输出的是完整路径
	 */
	private static void fileList(){
		String path = "d:" + File.separator + File.separator;
		File file = new File(path);
		if(file.isDirectory()){
			//String[] strList = file.list();//这里输出的是文件名
			File[] strList = file.listFiles();//输出完整路径
			for (int i = 0; i < strList.length; i++) {
				System.out.println("---fileList()-----" + strList[i]);
			}
		}else{
			System.out.println("该目录不存在!");
		}
	}
	/**
	 * 搜索指定目录的全部内容
	 * @param file
	 */
	private static void searchAllFile(File file){
		if(file != null){
			if(file.isDirectory()){
				File[] arrayFile = file.listFiles();
				if(arrayFile != null){
					for (int i = 0; i < arrayFile.length; i++) {
						searchAllFile(arrayFile[i]);//递归调用
					}
				}
			}else{
				System.out.println(file);
			}
		}
	}
	
	/**
	 * 字节流
	 * 向文件中写入字符串
	 * @param str
	 */
	private static void writeString(String str){
		String fileName = "d:" + File.separator + "hello.txt";
		File file = new File(fileName);
		try {
			//OutputStream os = new FileOutputStream(file);//自动创建文件
			OutputStream os = new FileOutputStream(file, true);//在文件中追加内容
			byte[] b = str.getBytes();
			os.write(b);//一次性写入
			//个字节一个字节的写入
			//for (int i = 0; i < b.length; i++) {
	        //    os.write(b[i]);
	        //}
			os.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 字节流
	 * 向文件中read字符串
	 */
	private static void readString(){
		String fileName = "d:" + File.separator + "hello.txt";
		File file = new File(fileName);
		try {
			InputStream is = new FileInputStream(file);
			byte[] b = new byte[((int)file.length())];//((int)file.length())这里为了节省空间,计算file大小
			is.read(b);
			is.close();
			System.out.println("---readString()-----" + new String(b));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 字符流,写入字符
	 * 这个例子上之前的例子没什么区别,只是你可以直接输入字符串,而不需要你将字符串转化为字节数组。
	 * Writer out =new FileWriter(f,true);文件中追加内容
	 */
	private static void writeStr(String str){
		String fileName = "d:" + File.separator + "hello.txt";
		File file = new File(fileName);
		try {
			Writer out = new FileWriter(file);
			out.write(str);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 字符流
	 * 从文件中读出内容
	 * 当然最好采用循环读取的方式,因为我们有时候不知道文件到底有多大。
	 */
	private static void readStr(){
		String fileName = "d:" + File.separator + "hello.txt";
		File file = new File(fileName);
		char[] ch = new char[1024];
		int temp = 0;
		int count = 0;
		try {
			Reader read = new FileReader(file);
			while((temp = read.read()) != (-1)){
				ch[count++] = (char)temp;
			}
			read.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("---readStr()-----" + new String(ch,0,count));
	}
	
//	关于字节流和字符流的区别
//	实际上字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的,但是字符流在操作的 时候下后是会用到缓冲区的,是通过缓冲区来操作文件的。
//	读者可以试着将上面的字节流和字符流的程序的最后一行关闭文件的代码注释掉,然后运行程序看看。你就会发现使用字节流的话,文件中已经存在内容,但是使用字符流的时候,文件中还是没有内容的,这个时候就要刷新缓冲区。
//	使用字节流好还是字符流好呢?
//	答案是字节流。首先因为硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容。但是字符只是在内存中才会形成的,所以在开发中,字节流使用广泛。
	
	/**
	 * 文件复制
	 * 基本思路是从一个文件中读入内容,边读边写入另一个文件
	 */
	private static void fileCopy(){
		String fileName1 = "d:" + File.separator + "hello.txt";
		String fileName2 = "d:" + File.separator + "hello_copy.txt";
		File file1 = new File(fileName1);
		File file2 = new File(fileName2);
		if(!file1.exists()){
			return;
		}
		
		try {
			InputStream is = new FileInputStream(file1);
			OutputStream os = new FileOutputStream(file2);
			if(is != null && os != null){
				int temp = 0;
				while((temp = is.read()) != (-1)){
					os.write(temp);
				}
			}
			is.close();
			os.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}

http://www.cnblogs.com/rollenholt/archive/2011/09/11/2173787.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值