import java.io.*;
public class Test{
private static void copy(){
try{
InputStreamReader isr = new InputStreamReader(new FileInputStream("C:\\Users\\Lenovo\\Desktop\\1.txt"),"UTF-8");
//是字节流通向字符流的桥梁,封装了InputStream在里头,它以较高级的方式一次读取一个一个字符,以文本格式输入/输出,可以指定编码格式;
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("C:\\Users\\Lenovo\\Desktop\\1(copy).txt"),"UTF-8");
int ch;
while((ch = isr.read())!=-1){
System.out.println();
osw.write((char)ch);
}
osw.close();
isr.close();
}catch(Exception e){
e.printStackTrace();
}
}
//写
private static void method1() throws IOException {
String s = "我爱学习,我爱Java";
//byte[] bys = s.getBytes();//通过默认编码转换成数组
byte[] bys = s.getBytes("UTF-8");
FileOutputStream fos = new FileOutputStream("C:\\Users\\Lenovo\\Desktop\\1.txt");
fos.write(bys);
fos.close();
}
//读
private static void method2() throws IOException {
FileInputStream fis = new FileInputStream("C:\\Users\\Lenovo\\Desktop\\1.txt");
byte[] bys = new byte[1024];
int len = fis.read(bys);
System.out.println(new String(bys,0,len,"UTF-8"));
}
public static void main(String args[]) throws IOException {
// copy();
method1();
method2();
}
}

构造方法(字节数组转字符串):
String() 初始化一个新创建的String对象,使其表示一个空字符序列
String(byte[] bytes) 使用平台的默认字符集解码指定的byte数组,构造一个新的String
String(byte[] bytes, Charset charset) 使用指定的charset解码指定的byte数组,构造一个新的String
成员方法(字符串转字节数组)
getBytes() 使用平台的默认字符集将此String编码为byte序列,并将结果存储到一个新的byte数组中
getBytes(Charset charset)使用给定的charset将此String编码到byte序列,并将结果存储到新的byte数组
​​​几种常见的编码格式​