package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestIO {
public static void main(String[] args){
//创建fileInputStream 对象
FileInputStream fileinput=null;
try {
//实例化FileInputStream对象
fileinput = new FileInputStream(new File("D://个人文档//一网通办测试.txt"));
//设置每次从流中读取的字节数量
byte[] bt=new byte[1024];
int n = 0;
String str="";
//对读取文件的流做循环判断,如果还有数据则继续,否则退出循环
while((n=fileinput.read(bt))!=-1){
//从流中获取开始到结束的内容,将字符编码设置成UTF-8防止数据乱码
str+=new String(bt,0,n,"utf-8");
System.out.println(str);
}
//创建FileOutputStream类的对象,并初始化文件位置
FileOutputStream out=new FileOutputStream("D://个人文档//测试.txt");
String str1=str;
//将字符串变为byte类型数组。
byte[] bts=str1.getBytes();
//使用FileOutputStream 类对象调用write方法将byte数组中的数据写到实例化的文件中。
out.write(bts);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fileinput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}