java中IO流实例

package com.phome.demo1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 输入输出流测试
 *
 **/
public class IOTest {
	public static void main(String[] args) throws IOException {
		/*
		 * 以日期作为文件夹路径
		 * 通过SimpleDateFormat类获取系统时间并做转换
		 */
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		// 日期目录
		String dir = sdf.format(date);
		// 路径
		String path = "C:\\Users\\ChenLiYun\\Desktop\\testImage\\"+dir;
		// 创建路径
		IOTest io = new IOTest();
/*		boolean isok = io.CreatePath(path);
		if(isok){
			// 写入数据
			String data = "要写入的数据";
			io.WriteDataToFile(path+"\\test.txt",data);
			System.out.println("写入数据完成");
		}else{
			System.out.println("写入数据失败");
		}*/
		
		/*
		 * 读取文件数据
		 */
		/*io.ReadData(path+"\\test.txt");*/
		
		/*
		 * 将a文件中的数据读取到b文件中
		 */
		String path1 = path +"\\test.txt";
		//String path2 = path +"\\test2.txt";
		String path2 = path +"\\test3.txt";
		io.ReadFileToFile(path1, path2);
		
	}
	/**
	 * 创建文件路径
	 * @param path 文件路径地址
	 * @return 返回结果为true:创建成功,false:创建失败
	 */
	public boolean CreatePath(String path){
		// 创建日期文件夹
		File file = new File(path);
		if(file.exists()){
			System.out.println("路径存在");
			return true;
		}else{
			file.mkdir();
			boolean yes = file.exists();
			if(yes){
				System.out.println("目录创建完成");
				return true;
			}else{
				System.out.println("目录创建失败");
				return false;
			}
		}
	}
	/**
	 * 将数据写入到文件中
	 * @param path 文件地址
	 * @throws IOException 
	 */
	public void WriteDataToFile(String path,String data) throws IOException{
		/*
		 * 1的这种写入方法会将原来的数据直接覆盖
		 * 2的这种方法会在原来的数据后面接着写入
		 */
//		FileOutputStream fos = new FileOutputStream(path);// 1
		FileOutputStream fos = new FileOutputStream(path,true);// 2
		fos.write(data.getBytes());
		fos.write("\r\n".getBytes());// 换行;每个操作系统的换行符不一样,windows:\r\n,linux:\n,mac:\r
		fos.close();// 一定要关闭此文件的输出流释放资源
	}
	
	/**
	 * 读取文件数据
	 * @param name 文件路径
	 * @throws IOException 异常
	 */
	public void ReadData(String name) throws IOException{
		/*
		 * FileinputStream读取到的是二进制数据需要进行转换,如果有汉字则输出是乱码
		 */
//		FileInputStream fis = new FileInputStream(name);
//		int by = 0;
//		while(by != -1){
//			System.out.print("字节流读取文件数据:"+(char)by);
//			by = fis.read();
//		}
//		byte[] bt = new byte[1024];//每次读取1M,相比上一种方式效率
//		int by = 0;
//		while((by = fis.read(bt))!=-1){
//			System.out.println(new String(bt,0,by));//将数据转化为字符串
//     	}
//		fis.close;
		/*
		 *用BufferedReader的方式读取文件 
		 */
		BufferedReader br = new BufferedReader(new FileReader(name));
		String str = "";
		while((str = br.readLine())!=null){
			System.out.println("BufferedReader读取文件数据:"+str);
		}
		br.close();
		
	}
	/**
	 * 将a文件中的数据读写到b文件中
	 * @param path1 a文件的路径
	 * @param path2 b文件的路径
	 * @throws IOException 抛出异常
	 */
	public void ReadFileToFile(String path1, String path2) throws IOException{
		/*
		 * 1、字节流读写数据
		 * 2、用字节流读写文件完成后为什么在写入的文件中汉字没有乱码,因为在读写过程中每读取一个字节就马上写入到文件中不会有其他的操作所以行子没有乱码。
		// 读取的文件
		FileInputStream fis = new FileInputStream(path1);
		// 写入的文件
		FileOutputStream fos = new FileOutputStream(path2);
		int str = 0;
		while((str= fis.read())!=-1){
			fos.write(str);
		}
		System.out.println("读写文件完成");
		// 关闭读写流
		fis.close();
		fos.close();*/
		/*
		 * 2、字符流读写数据
		 */
		// 读取的文件
		BufferedReader br = new BufferedReader(new FileReader(path1));
		// 写文件
		BufferedWriter bw = new BufferedWriter(new FileWriter(path2));
		String str = "";
		while((str = br.readLine())!=null){
			bw.write(str);
			bw.write("\r\n"); // 换行
		}
		System.out.println("写入文件完成");
		// 关闭读写流
		br.close();
		bw.close();
	}
}

猜你喜欢

转载自blog.csdn.net/u013804636/article/details/55052097