codetest

package cn.itcast.iostream;
/*
 * 字符输入流的缓冲区对象
 * BuffereReader
 */
import java.io.*;
public class BufferedReaderDemo {
	public static void main(String[] args) throws IOException{
		//建立字符输入流对象
		FileReader fr = new FileReader("c:\\buffered.txt");
		FileWriter fw = new FileWriter("c:\\xxxx.txt");
		//建立字符输入缓冲区对象
		BufferedReader bfr = new BufferedReader(fr);
		
		//调用缓冲区对象中的readLine()方法,读取一行,并返回字符串
		String line = null;
		
		while((line = bfr.readLine())!=null){
			fw.write(line);
			fw.flush();
		}
		
		bfr.close();
		fw.close();
	}
}


2

package cn.itcast.iostream;
/*
 * 演示字符输出流的缓冲区对象
 * BufferedWriter
 */
import java.io.*;
public class BufferedWriterDemo {
	public static void main(String[] args) throws IOException{
		//使用缓冲区,必须先有输出流对象
		FileWriter fw = new FileWriter("c:\\buffered.txt");
		//建立缓冲区对象,传递字符输出流对象
		BufferedWriter bfw = new BufferedWriter(fw);
		/*
		 * BufferedWriter{
		 *   public void close(){
		 *     fw.close();
		 *   }
		 * }
		 */
		
		bfw.write("abc");
		bfw.newLine();
		bfw.write("qqq");
		bfw.flush();
		bfw.close();
	}
}

CopyTextDemo

package cn.itcast.iostream;
/*
 * 复制文本文件
 * 将C盘中的text.txt文件复制到D盘
 */
import java.io.*;
public class CopyTextDemo {
	public static void main(String[] args) {
		//获取开始时间
		long start = System.currentTimeMillis();
		
		//调用复制文件的方法
		copy_3();
		
		//获取结束时间
		long end = System.currentTimeMillis();
		System.out.println(end - start);
	}
	
	
	//复制文本文件,使用缓冲区对象,实现读一行,写一行,写换行
	private static void copy_3(){
		//声明两个缓冲区对象的引用变量
		BufferedReader bfr = null;
		BufferedWriter bfw = null;
		try{
			bfr = new BufferedReader(new FileReader("c:\\abc.txt"));
			bfw = new BufferedWriter(new FileWriter("d:\\abc.txt"));
			//读一行,写一行,写换行
			String line = null;
			while((line = bfr.readLine())!=null){
				bfw.write(line);
				bfw.newLine();
				bfw.flush();
			}
		}catch(IOException e){
			e.printStackTrace();
			throw new RuntimeException("复制失败");
		}finally{
			try{
				if(bfw!=null)
					bfw.close();
			}catch(IOException e){
				throw new RuntimeException("文件写入关闭失败");
			}
			
			try{
				if(bfr!=null)
					bfr.close();
			}catch(IOException e){
				throw new RuntimeException("文件读取关闭失败");
			}
		}
	}
	
	//复制文件,读取一个数组,写一个数组,带上异常处理
	private static void copy_2(){
		FileReader fr = null;
		FileWriter fw = null;
		try{
			//建立数据源,读取流对象
			fr = new FileReader("c:\\text.txt");
			//建立数据目的,输出流对象
			fw = new FileWriter("d:\\text.txt");
			//开始循环读取源文件
			//读取数组,写数组
			char[] buf = new char[1024];
			int len = 0 ;
			while((len = fr.read(buf))!=-1){
				//读一个,写一个
				fw.write(buf,0,len);
			}
		}catch(IOException e){
			e.printStackTrace();
			throw new RuntimeException("复制失败");
		}finally{
			try{
				if(fw!=null)
					fw.close();
			}catch(IOException e){
				throw new RuntimeException("文件写入关闭失败");
			}
			
			try{
				if(fr!=null)
					fr.close();
			}catch(IOException e){
				throw new RuntimeException("文件读取关闭失败");
			}
		}
	}
	
	
	
	//复制文件,第一个字符,写一个字符,带上异常处理
	private static void copy_1(){
		//声明变量,字符输入流(数据源),字符输出流(数据目的)
		FileReader fr = null;
		FileWriter fw = null;
		try{
			//建立数据源,读取流对象
			fr = new FileReader("c:\\text.txt");
			//建立数据目的,输出流对象
			fw = new FileWriter("d:\\text.txt");
			//开始循环读取源文件
			int len = 0 ;
			while((len = fr.read())!=-1){
				//读一个,写一个
				fw.write((char)len);
			}
		}catch(IOException e){
			e.printStackTrace();
			throw new RuntimeException("复制失败");
		}finally{
			try{
				if(fw!=null)
					fw.close();
			}catch(IOException e){
				throw new RuntimeException("文件写入关闭失败");
			}
			
			try{
				if(fr!=null)
					fr.close();
			}catch(IOException e){
				throw new RuntimeException("文件读取关闭失败");
			}
		}
	}
	
	
}

FileReaderDemo

package cn.itcast.iostream;
/*
 * 字符流读取文件
 */
import java.io.*;
public class FileReaderDemo implements Serializable{
	public static void main(String[] args)throws IOException {
		//建立字符读取流对象
		FileReader fr = new FileReader("c:\\demo.txt");
		//调用FileReader类的父类的方法read,读取单个字符
		//如果读取到了有效字符,read方法,返回ASCII值,如果读取到了文件的末尾,返回-1
		//循环的读取文件
		int len = 0 ;
		
		while((len = fr.read())!=-1){
			System.out.print((char)len);
		}
		fr.close();
	}
}
FileReaderDemo1

package cn.itcast.iostream;
/*
 * 读取数组
 */
import java.io.*;
public class FileReaderDemo1 {
	public static void main(String[] args) throws IOException{
		//建立输入流的对象
		FileReader fr = new FileReader("c:\\demo.txt");
		//读取文件,文件中的字符,存储到字符数组
		char[] buf = new char[1024];// 2kb
		//read(buf)int类型的返回值
		int len = 0 ;
		
		while((len = fr.read(buf))!=-1){
			//将数组变成字符串打印
			System.out.println(len);
			System.out.print(new String(buf,0,len));
		}
		
		
		/*len = fr.read(buf);
		System.out.println("len="+len);//2
		//将数组变成字符串打印
		System.out.println(new String(buf));//ab
		
		len = fr.read(buf);
		System.out.println("len="+len); //2
		System.out.println(new String(buf));//cd
		
		len = fr.read(buf);
		System.out.println("len="+len); // 1
		System.out.println(new String(buf)); //ed
		
		len = fr.read(buf);
		System.out.println("len="+len); // 1
		System.out.println(new String(buf)); //ed
		*/
		fr.close();
		
	}
}

FileWriterDemo2

package cn.itcast.iostream;
/*
 * 续写
 * 
 * 写字符数据
 */
import java.io.*;
public class FileWriterDemo2 {
	public static void main(String[] args) throws IOException{
		FileWriter fw = new FileWriter("c:\\demo.txt");
		//定义一个字符数组
		
		char[] ch = {'a','c','d','e','子','符','流'};
		
		fw.write(ch,0,ch.length);
		fw.write("\r\n");
		fw.write(ch, 0, ch.length);
	
		fw.flush();
		fw.close();
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值