前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java向文件中写入内容,字节流,字符流,缓冲,复制文件,设置字符编码 实例

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

作者头像
全栈程序员站长
发布2022-06-30 13:10:00
9000
发布2022-06-30 13:10:00
举报
文章被收录于专栏:全栈程序员必看
代码语言:javascript
复制
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();
	}

	
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/106296.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年7月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档