1.不采用缓存,读一次写一次
- public static void copy1(String input,String output){
- FileInputStream fis = null;
- FileOutputStream fos = null;
- try {
- fis = new FileInputStream(new File(input));
- fos = new FileOutputStream(new File(output));
- int in = 0;
- while((in=fis.read())!=-1){
- fos.write(in);
- // System.out.println(in);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- fis.close();
- fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- long startTime = System.currentTimeMillis();
- String input = "d:/linkMap.txt";
- String output = "d:/output.txt";
- copy(input,output);
- long endTime = System.currentTimeMillis();
- System.out.println((endTime-startTime)+"ms");
- }
2、采用缓存字节数组
- public static void copy(String input, String output) {
- FileInputStream fis = null;
- FileOutputStream fos = null;
- try {
- byte[] buffer = new byte[1024];
- fis = new FileInputStream(new File(input));
- fos = new FileOutputStream(new File(output));
- int len = 0;
- while ((len = fis.read(buffer)) != -1) {
- fos.write(buffer, 0, len);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- fis.close();
- fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
可见采用缓存来读写文件的效率是惊人的。