1.使用字节的输入流和输出流来进行复制
字节流不但可以写文本还可以写图片,音频
public static void main(String[] args){
FileInputStream f = new FileInputStream("/Users/lanou/Desktop/test/11.png");
FileInputStream f2 = new FileInputStream("/Users/lanou/Desktop/test/112233.png");
byte[] b = new byte[1024;]
int len = 0;
while((len = f1.read(b))!= -1){
f2.write(b,0,len)
}
f1.close();
f2.close();
}
首先绑定两个文件
然后用数组的方式读写
最后关闭数据流
需求:将一个文件夹复制到另一个文件夹
参数 原文件夹 filestart
要被添加的文件夹 File fileend
public static void fun(File filestart,File fileend) throws IOException {
File newfile = new File(filestart, fileend.getName());
newfile.mkdir();
File[] listFiles = filestart.listFiles();
for (File subfile : listFiles) {
if(subfile.isFile()) {
FileInputStream fileInputStream = new FileInputStream(subfile);
File file3 = new File(newfile, subfile.getName());
FileOutputStream fileOutputStream =new FileOutputStream(file3);
int len = 0;
byte[] b =new byte[1024];
while((len = fileInputStream.read(b))!=-1) {
fileOutputStream.write(b, 0, len);
}
}else {
fun(subfile, newfile);
}
}
}
字符流
也是一个字符一个字符的读
和字节流的区别就是字符流只能操作文本,不能写图片,音频。
write是所有字符输出流的父类,并且是抽象类
FileWriter
构造方法
1.文件
2.字符串
字符流的写入
public static void main(String[] args) throws IOException {
FileWriter fileWriter = new FileWriter("/Users/lanou/Desktop/Xtest/haha.txt");
fileWriter.write(100);
fileWriter.flush();
char[] c = {'l','w','s','d'};
fileWriter.write(c);
fileWriter.flush();
fileWriter.write(c, 1, 2);
fileWriter.write("床前明月光\n疑是地上霜\n");
fileWriter.flush();
fileWriter.write("白日依山尽", 2, 3);
fileWriter.flush();
fileWriter.close();
}
public static void main(String[] args) throws IOException {
FileReader fileReader = new FileReader("/Users/lanou/Desktop/Xtest/haha.txt");
char[] c = new char[1024];
int len = 0;
while ((len = fileReader.read(c)) != -1) {
System.out.println(new String(c, 0, len));
}
}
转换流
OutputStreamWriter
可以根据不同的编码形式写入 需要使用到FIleOutputStream 类
inputStreamReader(字节流转向字符流)
可以读取不同编码格式文件
需要使用到FIleInputStream 类
利用转换流写文件 默认方式查UTF-8
public static void getUTF8() throws IOException {
FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/Xtest/utf8.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write("丁鹏");
osw.close();
}
使用GBK编码来写入文件
private static void getGBK() throws IOException {
FileOutputStream f = new FileOutputStream("/Users/lanou/Desktop/Xtest/GBK.txt");
OutputStreamWriter o = new OutputStreamWriter(f, "gbk");
o.write("丁鹏");
o.close();
}
以utf- 8读取
private static void readUtf() throws IOException {
FileInputStream fInputStream = new FileInputStream("/Users/lanou/Desktop/Xtest/utf8.txt");
InputStreamReader i = new InputStreamReader(fInputStream);
char[] c =new char[1024];
int len = 0;
while((len = i.read(c))!= -1) {
System.out.println(new String(c, 0, len));
}
i.close();
}
以gbk读取
private static void readgbk() throws IOException {
FileInputStream fInputStream = new FileInputStream("/Users/lanou/Desktop/Xtest/utf8.txt");
InputStreamReader i = new InputStreamReader(fInputStream,"gbk");
char[] c =new char[1024];
int len = 0;
while((len = i.read(c))!= -1) {
System.out.println(new String(c, 0, len));
}
i.close();
}