File操作(6)------------------- printWriter,bufferedWriter

package demo.test2;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

public class PriterWriterDemoAll {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException{
		p1();
		p2();
		p3();
		p4();
		p5();

	}
	/**
	 * 使用FileOutputStream   字节流输出
	 * ==============使用FileOutputStream  是否追加内容
	 * @throws FileNotFoundException
	 */
	public static void p1() throws FileNotFoundException {
		FileOutputStream fos = new FileOutputStream("a11.txt",true);
		PrintWriter pw = new PrintWriter(fos);

		pw.println("1234567");
		pw.append("1234567");

		pw.close();

	}
	/**
	 * 使用PrintWriter  字符流   
	 * 直接使用PrintWriter 类可以指定字符集
	 * @throws FileNotFoundException
	 * @throws UnsupportedEncodingException
	 */
	public static void p2() throws FileNotFoundException, UnsupportedEncodingException {
		PrintWriter pw = new PrintWriter("pw.txt", "utf-8");
		pw.println("123456");
		pw.println("hello world");
		pw.append("ddd");

		pw.close();
	}
	/**
	 * 使用字符缓冲流输入数据
	 * BufferedWriter bf = new BufferedWriter(new PrintWriter(new FileOutputStream("a33.txt",true)));
	 * @throws IOException
	 */
	public static void p3() throws IOException{
		FileOutputStream fos = new FileOutputStream("a33.txt",true);
		PrintWriter pw = new PrintWriter(fos);
		BufferedWriter bf = new BufferedWriter(pw);
		bf.newLine();	//创建新 的一行
		bf.append("你好  L  BufferedWriter");
		bf.close();
	}
	/**
	 * 使用FileOutputStream 字节流   与  OutputStreamWriter 字符流搭配将内容写入文件
	 * 简写OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("a.txt",true), "gbk");
	 * @throws IOException 
	 */
	public static void p4() throws IOException{
		FileOutputStream fis = new FileOutputStream("a.txt",true);
		OutputStreamWriter osw = new OutputStreamWriter(fis, "gbk");
		
		osw.append("java\\n\\n");
		osw.write("hello world");
		
		osw.close();
	}
	/**
	 * 字符集格式转换
	 * 需要用到  OutputStreamWriter  与   InputStreamReader 两个类
	 * 使用UTF-8编码将每一个字符读取出来,再以GBK编码将字符写入另一个文件即可
	 * @throws IOException 
	 */
	public static void p5() throws IOException{
		
		FileInputStream fis =  new FileInputStream("a11.txt");
		InputStreamReader isr = new InputStreamReader(fis, "utf-8");
		
		FileOutputStream fos = new FileOutputStream("a11_copy.txt");
		OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
		
		char[] ch = new char[1024*5];
		int len = -1;
		while((len =isr.read(ch)) != -1){
			osw.write(ch, 0,len);
		}
		//关闭流
		isr.close();
		osw.close();
	}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值