java中IO流实例

来源:互联网 发布:php 开源 文档 编辑:程序博客网 时间:2024/06/03 21:57
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);// 1FileOutputStream fos = new FileOutputStream(path,true);// 2fos.write(data.getBytes());fos.write("\r\n".getBytes());// 换行;每个操作系统的换行符不一样,windows:\r\n,linux:\n,mac:\rfos.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();//}/* *用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();}}

0 0
原创粉丝点击