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

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

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();
	}

	
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • linux下杀死某个进程_shell脚本获取进程号并杀死进程

    linux下杀死某个进程_shell脚本获取进程号并杀死进程在做项目的时候经常会出现程序死机、锁死、无响应等情况,这时候就需要找到程序相应的进程将其杀掉即可。步骤如下:1.定位进程top命令:可以实时动态地查看系统的整体运行情况,是一个综合了多方信息监测系统性能和运行信息的实用工具。通过top命令所提供的互动式界面,用热键可以管理。输入top后可以看到如下的界面,实时显示进程情况。ps命令:processstatus的简称,用于报告当…

  • 安装pycharm教程最新版激活_Mac序列号

    安装pycharm教程最新版激活_Mac序列号激活码+汉化+Python安装包汉化下载汉化包resources_en.jar找到Pycharm,右键点击显示包内容,把resources_en.jar拷贝到/Applications/PyCharmCE.app/Contents/lib/里面重启Pycharm激活目前比较好用的Python开发工具是PyCharm,但是正版软件需要购买,要不然只能试…

  • android 反射NoSuchMethodException异常

    android 反射NoSuchMethodException异常android反射NoSuchMethodException异常因为方法的参数是int类型,使用反射调用时使用Integer类型的参数。应该使用getDeclaredMethod(“****”,int.class);

  • ubuntu杀死进程命令_ubuntu系统安装教程图解

    ubuntu杀死进程命令_ubuntu系统安装教程图解在用ubuntu的时候遇到几次程序卡死,但是不知道怎么关闭,心想有没有跟window一样的程序管理器存在?所以就去网上找了下解决方式记录一下:1、打开系统监视器:gnome-ststem-monitor然后就可以选择进程结束掉啦。2、如果知道程序名称可以命令杀死:killallXXX杀掉所有的进程killxxx(PID)杀死进程号xxx的进程,查看所有运行进程的命令:ps-aux…

  • Network 之二 Ethernet(以太网)中的 MAC、MII、PHY 详解[通俗易懂]

    Network 之二 Ethernet(以太网)中的 MAC、MII、PHY 详解[通俗易懂]结构  从硬件的角度看,以太网接口电路主要由MAC(MediaAccessControl)控制器和物理层接口PHY(PhysicalLayer,PHY)两大部分构成。如下图所示  但是,在实际的设计中,以上三部分并不一定独立分开的。由于,PHY整合了大量模拟硬件,而MAC是典型的全数字器件。考虑到芯片面积及模拟/数字混合架构的原因,通常,将MAC集成进微控制器而将PHY留在片外…

  • 你平时在哪找免费的可商业使用的图片素材?「建议收藏」

    你平时在哪找免费的可商业使用的图片素材?

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号