首页 > 其他分享 >按指定编码读写文件

按指定编码读写文件

时间:2022-11-04 17:34:25浏览次数:72  
标签:编码 java 读写 指定 IOException io new import public

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.io.UnsupportedEncodingException;



public class EncodeTest {

public static void main(String[] args) throws IOException {

//writeText();

readText();

}



public static void readText() throws IOException{

InputStreamReader isr=new InputStreamReader(new FileInputStream("G:\\gbk.txt"),"gbk");

char[] buf=new char[1024];

int len=0;

while((len=isr.read(buf))!=-1){

String str=new String(buf,0,len);

System.out.println(str);

}

isr.close();



}

public static void writeText() throws IOException{

OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("G:\\utf.txt"),"utf-8");//使用utf-8编码



osw.write("你好");

osw.close();



}

}

标签:编码,java,读写,指定,IOException,io,new,import,public
From: https://blog.51cto.com/u_10028442/5824121

相关文章