处理文本
最好来说:
实现图片复制
package demo10;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class IOTest {
public static void main(String[] args) throws IOException {
File src = new File("1.png");
File dest = new File("2.png");
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
byte[] buffer = new byte[5];
int len;
while ((len = fis.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fis.close();
fos.close();
}
}
实现了图片复制。
指定路径下文件的复制
package demo10;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class IOTest {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
copyFile("1.mp4","2.mp4");
long end = System.currentTimeMillis();
System.out.println((end - start)/1000.0);
}
public static void copyFile(String srcPath,String destPath) throws IOException {
File src = new File(srcPath);
File dest = new File(destPath);
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
byte[] buffer = new byte[5];
int len;
while ((len = fis.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fis.close();
fos.close();
}
}
7.635
复制一个21s的mp4文件花了7.635s。
将buffer数组改为100大小
0.607
缓冲流字节型实现非文本文件复制
作用:提高流的读取和写入的速度。
注意到缓冲流是处理流,也就是要作用到节点流,所以要先来一个节点流。
package demo10;
import java.io.*;
public class BufferTest {
public static void main(String[] args) throws IOException {
//1.造文件
File src = new File("1.png");
File dest = new File("2.png");
//2.造流
//2.1造节点流
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
//2.2造处理流也就是缓冲流
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
//3.复制细节:读取和写入
byte[] b = new byte[1024];
int len;
while ((len = bis.read(b))!=-1){
bos.write(b,0,len);
}
//4.资源关闭 先关外面的也就是处理流,再关里面的也就是节点流
//其实关闭外层流,内层流自动就关了,所以没必要再写。
bis.close();
bos.close();
/*fis.close();
fos.close();*/
}
}
缓冲流与节点流速度对比
package demo10;
import java.io.*;
public class BufferTest {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
//1.造文件
File src = new File("1.mp4");
File dest = new File("2.mp4");
//2.造流
//2.1造节点流
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
//2.2造处理流也就是缓冲流
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
//3.复制细节:读取和写入
byte[] b = new byte[1024];
int len;
while ((len = bis.read(b))!=-1){
bos.write(b,0,len);
}
//4.资源关闭 先关外面的也就是处理流,再关里面的也就是节点流
//其实关闭外层流,内层流自动就关了,所以没必要再写。
bis.close();
bos.close();
long end = System.currentTimeMillis();
System.out.println(end - start);
/*fis.close();
fos.close();*/
}
}
24
而之前的节点流是0.607s也就是607ms,是将近30倍,这里我们用的都是1024大小的byte数组。
提高读写速度的原因:内部提供了一个缓冲区。
缓冲流对文本文件的复制
package demo10;
import java.io.*;
public class BufferTest {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(new File("hello.txt")));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("hi.txt")));
char[] c = new char[1024];
int len;
while ((len = br.read(c)) != -1){
bw.write(c,0,len);
}
br.close();
bw.close();
}
}
上述方式仍然使用char[]数组
方式二:使用String和BufferedReader里的readLine()
方法。
package demo10;
import java.io.*;
public class BufferTest {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(new File("hello.txt")));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("hi.txt")));
String data;
while ((data = br.readLine()) != null){
bw.write(data);//data中不包含换行符
}
br.close();
bw.close();
}
}
所以这里要加一个换行符
或者调用newLine()
方法
总结
此外,处理流中还有flush方法,调用它可以把缓冲区的内容写出去,只不过缓冲流自动调用它,所以在这里不用管