java向文件中写入内容,字节流,字符流,缓冲,复制文件,设置字符编码 实例

本文提供了一个Java程序示例,展示了如何使用不同的IO流进行文件的读取和写入操作,包括字节流、字符流及其缓冲版本,并介绍了如何指定字符集编码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.liuxin.test;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Write {

	public static void main(String[] args)throws Exception {
		//2017年9月30日 下午1:48:23
		String contentString="shide 的大的呃呃";
		String fileName="D://3.txt";
		System.out.println("----------一段字符串以字节流写入文件------------");
		writeByte(contentString,fileName);
		System.out.println("----------一段字符串以字符流写入文件------------");
		writeChar(contentString,fileName);
		System.out.println("----------一段字符串通过缓冲流以字节流写入文件------------");
		writeByteBuffer(contentString,fileName);
		System.out.println("----------一段字符串通过缓冲流以字符流写入文件------------");
		writeCharBuffer(contentString,fileName);
		System.out.println("----------一段字符串通过缓冲流以字符流写入文件,并这只字体编码------------");
		writeCharSetEncode(contentString,fileName);
		System.out.println("------------复制文件------------------");
		readAndWrite();
		
		
	}

	private static void readAndWrite() throws Exception{
		//2017年9月30日 下午3:20:00
		FileInputStream is=new FileInputStream("D://1.txt");
		InputStreamReader isr=new InputStreamReader(is, "gbk"); //设置编码
		BufferedReader br=new BufferedReader(isr);
		File file=new File("D://4.txt");
		
		if(file.exists()){//文件存在着先删除
			file.delete();
		}
		FileOutputStream os=new FileOutputStream("D://4.txt",true);//ture表示追加写入。如果不需要追加写入就直接去掉这个参数就行
		OutputStreamWriter osw=new OutputStreamWriter(os,"gbk");//设置编码
		BufferedWriter bw=new BufferedWriter(osw);
		String tempReadString=null;
		while((tempReadString=br.readLine())!=null){
			bw.write(tempReadString);
			bw.newLine();//换行
		}
		br.close();
		isr.close();
		is.close();
		bw.close();
		osw.close();
		os.close();
	}

	private static void writeCharSetEncode(String contentString, String fileName) throws Exception {
		//2017年9月30日 下午3:10:55
		FileOutputStream fw=new FileOutputStream(fileName,true);//ture表示追加写入。如果不需要追加写入就直接去掉这个参数就行
        OutputStreamWriter osw=new OutputStreamWriter(fw,"gbk");
		BufferedWriter bw=new BufferedWriter(osw);
		bw.newLine();//换行
		bw.write(contentString);
		bw.close();
		fw.close();
	}

	private static void writeCharBuffer(String contentString, String fileName) throws Exception {
		//2017年9月30日 下午3:06:06
		FileWriter fw=new FileWriter(fileName,true);//ture表示追加写入。如果不需要追加写入就直接去掉这个参数就行
		BufferedWriter bw=new BufferedWriter(fw);
		bw.newLine();
		bw.write(contentString);
		bw.close();
		fw.close();
	}

	private static void writeByteBuffer(String contentString, String fileName) throws Exception {
		//2017年9月30日 下午2:30:11
		FileOutputStream os=new FileOutputStream(fileName,true);//ture表示追加写入。如果不需要追加写入就直接去掉这个参数就行
		BufferedOutputStream bos=new BufferedOutputStream(os);
		bos.write(contentString.getBytes());
		bos.write("\r\n".getBytes());  //换行追加
		bos.write("一段字符串通过缓冲流以字节流写入文件".getBytes());
		bos.write("追加内容".getBytes());
		bos.close();
		os.close();
	}

	private static void writeChar(String contentString,String fileName)throws Exception {
		//2017年9月30日 下午2:18:20
		FileWriter fw=new FileWriter(fileName,true);//ture表示追加写入。如果不需要追加写入就直接去掉这个参数就行
		fw.write(contentString);
		fw.close();
	}

	private static void writeByte(String contentString,String fileName) throws Exception{
		//2017年9月30日 下午1:55:39
		FileOutputStream os=new FileOutputStream(fileName);
		os.write(contentString.getBytes());
		os.close();
	}

	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老马识途2.0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值