package com.gao.IO;
import javax.xml.stream.FactoryConfigurationError;
import java.io.*;
public class Test06 {
public static void main(String[] args) throws FactoryConfigurationError, UnsatisfiedLinkError, IOException {
//文件-->程序:
//1.有一个源文件:
File f = new File("D:\\download\\idea\\练习\\Test.txt");
//2.需要一个输入的字节流接触文件:
FileInputStream fis = new FileInputStream(f);
//3.加入一个转化器,将字节流转换为字符流:(转换流属于一个处理流)
//将字节转换为字节符地时候,需要指定一个编码,这个编码跟文件本身编码格式统一
//如果编码格式不统一地话,那么在控制台上展示地效果就会出现乱码
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
//如果不写UTF-8,则自动获取程序本身地编码
//4.开始动作,将文件中内容显示在控制台
char[] ch = new char[20];
int len = isr.read(ch);
while (len!=-1){
//将缓冲数组转为字符串在控制台上打印出来
System.out.println(new String(ch,0,len));
len = isr.read(ch);
//关闭流
isr.close();
}
}
}
复制文件
package com.gao.IO;
import java.io.*;
public class Test07 {
public static void main(String[] args) throws IOException {
//1.有一个源文件:
File f1 = new File("D:\\download\\idea\\练习\\Test.txt");
//2.有一个目标文件:
File f2 = new File("D:\\download\\idea\\练习\\Demo.txt");
//3.输入方向:
FileInputStream fis = new FileInputStream(f1);
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
//4.输出方向:
FileOutputStream fos = new FileOutputStream(f2);
OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
//5.开始动作
char[] ch = new char[20];
int len = isr.read(ch);
while (len!=-1){
osw.write(ch,0,len);
len = isr.read(ch);
}
//6.关闭流
osw.close();
isr.close();
}
}
标签:ch,OutputStreamWriter,isr,len,File,new,FileInputStream
From: https://www.cnblogs.com/gaoxiaocuo/p/16707438.html