IO流
一、字符流
FileReader和FileWriter
*通过拷贝30M的视频来演示
import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class IODemo9_1 { public static void main(String[] args) { copy("F:\\新建文件夹\\aaa.mp4", "F:\\新建文件夹\\视频\\aaa.mp4"); } private static void copy(String from, String to) { long time = System.currentTimeMillis(); FileWriter fw = null; FileReader fr = null; try { fr = new FileReader(from); fw = new FileWriter(to); char[] buf = new char[512]; int len = -1; while ((len = fr.read(buf)) != -1) { fw.write(buf, 0, len); fw.flush(); } } catch (Exception e) { e.printStackTrace(); } finally { try { // 可以将这两个方法提取成一个工具类 fw.close(); fr.close(); } catch (IOException e) { e.printStackTrace(); } } long time1 = System.currentTimeMillis() - time; System.out.println("耗时" + time1 / 1000); } }
运行结果: 耗时29
* BufferedReader : 增强读取性能的
* BufferedWriter : 增强写性能的
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class IoDemo16_1 { public static void main(String[] args) { copy("F:\\新建文件夹\\aaa.mp4", "F:\\新建文件夹\\视频\\aaa.mp4"); } private static void copy(String from, String to) { long time=System.currentTimeMillis(); BufferedReader br=null; BufferedWriter bw=null; try { br=new BufferedReader(new FileReader(from)); bw=new BufferedWriter(new FileWriter(to)); String len =null; while((len=br.readLine())!=null){ bw.write(len); bw.flush(); } } catch (Exception e) { e.printStackTrace(); }finally { try { bw.close(); br.close(); } catch (IOException e) { e.printStackTrace(); } } long time1=System.currentTimeMillis()-time; System.out.println("耗时"+time1/1000); } }
*运行结果
耗时:23
* FileReader和BufferedReader的区别
- FileReader:不能一行一行的读数据
- BufferedReader : 可以一次读一行数据 速度比FileReader快
二、字节流
* FileInputStream : 读取文件
* FileOutputStream : 写文件
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class IODemo3_1 {
public static void main(String[] args) {
copy("F:\\新建文件夹\\aaa.zip", "F:\\新建文件夹\\视频\\a.zip");
}
private static void copy(String from, String to) {
long time = System.currentTimeMillis();
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File file = new File(from);
long length = file.length(); // 文件的大小
long record = 0; // 用来记录复制了多少
fis = new FileInputStream(file);
fos = new FileOutputStream(to);
byte[] buf = new byte[512];
int len = -1;
while ((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);
fos.flush();
record += len;
System.out.println("当前进度为:" + (record * 100 / length) + "%");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
long time1 = System.currentTimeMillis() - time;
System.out.println("耗时:" + time1 / 1000);
}
}
*运行结果
当前进度为:99%
当前进度为:99%
当前进度为:100%
耗时:26
* BufferedInputStream 增强读取性能的
* BufferedOutputStream 增强写性能的
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class IODemo4_1 {
public static void main(String[] args) {
copy("F:\\新建文件夹\\aaa.zip", "F:\\新建文件夹\\视频\\a.zip");
}
private static void copy(String from, String to) {
long time = System.currentTimeMillis();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
File file = new File(from);
long length = file.length();
long record = 0; // 记录复制多少
bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(new FileOutputStream(to));
byte[] buf = new byte[512];
int len = -1;
while ((len = bis.read(buf)) != -1) {
bos.write(buf, 0, len);
record += len;
System.out.println("当前进度为" + (record * 100) / length + "%");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
long time1 = System.currentTimeMillis() - time;
System.out.println("耗时:" + time1 / 1000);
}
}
*运行结果
当前进度为99%
当前进度为99%
当前进度为100%
耗时:22
FileInputStream和BufferedInputStream的区别
- FileInputStream是字节流 BufferedInputStream 是字符缓存流 使用BufferedInputStream读资源比使用FileInputStream读取的效率高
* InputStreamReader : 将字节码转换成字符码
* OutputStreamWriter : 将字符码转换成字节码
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
public class TestDemo1_1 {
public static void main(String[] args) throws IOException {
URL url=new URL("http://www.baidu.com/");
InputStream openStream = url.openStream();
//将字节码转换成字符码
InputStreamReader isr=new InputStreamReader(openStream, "UTF-8");
char[] buf=new char[512];
StringBuilder sb=new StringBuilder();
int len=-1;
while((len=isr.read(buf))!=-1){
sb.append(buf, 0, len);
}
//将字符码转成字节码
FileOutputStream fos=new FileOutputStream("index.html");
OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
osw.write(sb.toString().toCharArray());
osw.flush();
osw.close();
System.out.println(sb.toString());
}
}
*文件的切割和合并
文件的切割
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class TestDemo1_1 { public static void main(String[] args) { File file = new File("F:\\新建文件夹\\aaa.mp4"); cutFile(file); } private static void cutFile(File file) { if (file.isDirectory()) { throw new IllegalAccessError("参数必须是文件"); } long length = file.length(); long singleLength = 1024 * 1024 * 10; int singlecount = (int) (length % singleLength == 0 ? length / singleLength : (length / singleLength) + 1); FileInputStream fis = null; try { fis = new FileInputStream(file); for (int i = 0; i < singlecount; i++) { FileOutputStream fos = null; try { fos = new FileOutputStream(new File("F:\\新建文件夹\\视频\\", "part" + i + ".mp4")); byte[] buf = new byte[512]; int len = -1; int count = 0; while ((len = fis.read(buf)) != -1) { fos.write(buf, 0, len); fos.flush(); count += len; if (count == 1024 * 1024 * 10) { break; } } } catch (Exception e) { e.printStackTrace(); } finally { fos.close(); } } } catch (IOException e) { e.printStackTrace(); } finally { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } }
文件的合并
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.IOException; public class TestDemo2_1 { public static void main(String[] args) { File file = new File("F:\\新建文件夹\\视频\\"); cpFile(file); } private static void cpFile(File file) { File[] listFiles = file.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { // TODO Auto-generated method stub return name.startsWith("part"); } }); FileOutputStream fos = null; try { fos = new FileOutputStream(new File(file, "合并后.mp4")); for (int i = 0; i < listFiles.length; i++) { File file2 = listFiles[i]; FileInputStream fis = null; try { fis = new FileInputStream(file2); byte[] buf = new byte[1024]; int len = -1; while ((len = fis.read(buf)) != -1) { fos.write(buf, 0, len); fos.flush(); } } catch (Exception e) { // TODO: handle exception } finally { try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }