JAVA I/O之文件复制
有没有大佬告诉我这个不要了的代码插入区(就现在这句话的区域)怎么删掉。。。。。。。
//一个字节一个字节的复制
public static void fun() throws IOException {
FileInputStream fis = new FileInputStream("F:/abc.txt");
FileOutputStream fos = new FileOutputStream("F:/字节流复制(一个字节一个字节).txt");
int by = 0;
while ((by=fis.read()) != -1) {
fos.write(by);
}
fis.close();
fos.close();
}
//1024字节数组复制(加入数组缓冲区)
public static void fun1() throws IOException {
FileInputStream fis = new FileInputStream("F:/abc.txt");
FileOutputStream fos = new FileOutputStream("F:/字节流复制(1024字节数组).txt");
int len = 0;
byte[] bytes =new byte[1024];
while ((len=fis.read(bytes)) != -1) {
fos.write(bytes,0,len);
}
fis.close();
fos.close();
}
// 一个字节一个字节复制并用了缓冲流
public static void fun2() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:/abc.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:/字节缓冲流复制(一个字节一个字节).txt")); int by = 0;
while ((by=bis.read()) != -1) {
bos.write(by);
}
bos.close();
bis.close();
}
// 1024字节数组复制并用了缓冲流 (加入数组缓冲区)
public static void fun3() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:/abc.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:/字节缓冲流复制(1024字节数组).txt"));
int len = 0;
byte[] bytes =new byte[1024];
while ((len=bis.read(bytes)) != -1) {
bos.write(bytes,0,len);
}
bos.close();
bis.close();
}
//字符缓冲流复制文(一行一行可保留格式)
public static void fun4() throws IOException {
FileInputStream fileInputStream = new FileInputStream("F:/abc.txt");
//出现乱码问题的原因:文件的编码,系统的编码,java的默认编码有冲突。
//假如我们用FileReader这些类来读取系统文件,它调用的字符编码是java默认的UTF-8,但是一般WINDOW的TXT默认是ANSI,而自己本机的中文编码是GBK
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"GBK");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader); FileOutputStream fileOutputStream = new FileOutputStream("F:/字符缓冲流复制文件.txt");
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"GBK");
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter); String line = null;
while (null != (line = bufferedReader.readLine())) {
bufferedWriter.write(line);
bufferedWriter.newLine();
} if (bufferedWriter != null || outputStreamWriter != null || fileOutputStream != null) {
bufferedWriter.close();
outputStreamWriter.close();
fileOutputStream.close();
}
if (bufferedReader != null || inputStreamReader != null || fileInputStream != null) {
bufferedReader.close();
inputStreamReader.close();
fileInputStream.close();
}
} //字节流(ByteArrayInputStream)当你资源不足够用时,选择BufferedOutputStream是最佳的选择, 当你选择快速完成一个作业时,可以选择ByteArrayOutputStream之类的输出流https://www.cnblogs.com/yixiu868/p/8144670.html
public static void fun5() throws IOException {
FileInputStream fileInputStream = new FileInputStream("F:/abc.txt");
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = fileInputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
byte[] bytes = bos.toByteArray();
FileOutputStream fileOutputStream = new FileOutputStream("F:/字节流(ByteArrayInputStream).txt");
fileOutputStream.write(bytes); if (fileOutputStream != null) {
fileOutputStream.close();
}
if (fileInputStream != null) {
fileInputStream.close();
}
}
public static void main(String[] arg) throws IOException{ long start,end;
start = System.currentTimeMillis();
fun();
end = System.currentTimeMillis();
System.out.println("一个字节一个字节的复制(字节流)花费时间:" + (end - start) + "ms"); start = System.currentTimeMillis();
fun1();
end = System.currentTimeMillis();
System.out.println("1024字节数组复制(字节流)花费时间:" + (end - start) + "ms"); start = System.currentTimeMillis();
fun2();
end = System.currentTimeMillis();
System.out.println("一个字节一个字节的复制(缓冲流)花费时间:" + (end - start) + "ms"); start = System.currentTimeMillis();
fun3();
end = System.currentTimeMillis();
System.out.println("1024字节数组复制(缓冲流)花费时间:" + (end - start) + "ms"); start = System.currentTimeMillis();
fun4();
end = System.currentTimeMillis();
System.out.println("字符缓冲流复制文件花费时间:" + (end - start) + "ms"); start = System.currentTimeMillis();
fun5();
end = System.currentTimeMillis();
System.out.println("字节流(ByteArrayInputStream)花费时间:" + (end - start) + "ms"); }
执行结果:
下图为IO中的字节流和字符流,每一种又分为相应的输入流和输出流。需了解的基础概念:字符流(Reader,Writer),字节流,缓冲流(Buffered);使用时按照各自需求去搜寻对应相关资料即可。
JAVA I/O之文件复制的更多相关文章
- JAVA File方法各类文件复制操作
import java.io.*; public class AllFile { public static void main(String[] args) throws Exception {// ...
- Java实现简单的文件复制
public class FileCopy { public static void main(String[] args) { String path = "d:\\1.txt" ...
- Java中创建操作文件和文件夹的工具类
Java中创建操作文件和文件夹的工具类 FileUtils.java import java.io.BufferedInputStream; import java.io.BufferedOutput ...
- Java实现文件复制的四种方式
背景:有很多的Java初学者对于文件复制的操作总是搞不懂,下面我将用4中方式实现指定文件的复制. 实现方式一:使用FileInputStream/FileOutputStream字节流进行文件的复制操 ...
- Java基础之读文件——使用通道复制文件(FileBackup)
控制台程序,除了使用Files类中使用copy()方法将文件复制外,还可以使用FileChannel对象复制文件,连接到输入文件的FileChannel对象能直接将数据传输到连接到输出文件的FileC ...
- java中的IO流之文件复制
O(∩_∩)O哈哈~ 1.综述 一门成熟的语言肯定具备的几个模块:IO,通信,线程,UI...... Java作为一门成熟的程序语言,其IO流是比较复杂的.上个图大家感受下: 简单分析一下,IO分为两 ...
- 黑马程序员——java基础之文件复制
---------------------- ASP.Net+Unity开发..Net培训.期待与您交流!---------------------- <a href="http:// ...
- java IO之字节流和字符流-Reader和Writer以及实现文件复制拷贝
接上一篇的字节流,以下主要介绍字符流.字符流和字节流的差别以及文件复制拷贝.在程序中一个字符等于两个字节.而一个汉字占俩个字节(一般有限面试会问:一个char是否能存下一个汉字,答案当然是能了,一个c ...
- java学习之实现文件的复制
package com.io; import java.io.*; import java.text.SimpleDateFormat; import java.util.Date; /** * 文件 ...
随机推荐
- [办公应用]如何打印较小边距的PPT讲义(或者每页打印16页)
关键词:打印 PPT 讲义 4张 边距 今天同事问我如何打印PowerPoint的讲义.她自己使用PowerPoint打印讲义,设置每页4张,但是页边距太大:觉得浪费很大. 经过网上查阅后,现将方 ...
- the largest value you actually can transmit between the client and server is determined by the amount of available memory and the size of the communications buffers.
the largest value you actually can transmit between the client and server is determined by the amoun ...
- 【Dairy】2016.10.24 - made 嘲讽垃圾
//这个还找不到我的博客,但在看到原文 //这个还找不到我的博客,但在看到原文 //这个还找不到我的博客,但在看到原文 //这个还在我前面,访问多 一波嘲讽,woc 今天百度一下文章,发现有一篇和我一 ...
- java笔记线程电影院卖票改进版
通过加入延迟后,就产生了连个问题: * A:相同的票卖了多次 * CPU的一次操作必须是原子性的 * B:出现了负数票 * 随机性和延迟导致的 public class SellTicketD ...
- html表格合并单元格
th标签 合并列 colspan="k" 合并行 rowspan="k" 例子<th colspan="2", rowspan=& ...
- bzoj 1715: [Usaco2006 Dec]Wormholes 虫洞【spfa判负环】
tag是假的,用了及其诡异的方法判负环 正权无向边和负权有向边的图 #include<iostream> #include<cstdio> #include<cstrin ...
- Java中的锁机制,你真的了解吗?
学到锁说明你已经学过多线程了,只有在多线程并发的情况下才会涉及到锁,相信大家用的最多的要数synchronized了,因为这个也是最简单的,直接加在方法上就可以使一个方法同步.那么除了synchron ...
- mycat登录报错Host 'XXX' is blocked because of many connection errors的另一种解决思路
报错时机 使用了mycat,而不是单纯使用了mysql. 报错信息 ERROR 1129 (HY000): Host '1.23.22.18' is blocked because of many c ...
- java HashMap和LinkedHashMap区别
我们用的最多的是HashMap,在Map 中插入.删除和定位元素,HashMap 是最好的选择.但如果您要按自然顺序或自定义顺序遍历键,那么TreeMap会更好.如果需要输出的顺序和输入的相同,那么用 ...
- [CREC2007/CQOI2014]robotic sort
Description 一个实验室里有n个长短不一的试管.你的任务是编写一段程序,用机器臂把它们按照高度从小到大的顺序排列. 对于高度相同的试管,排序前后的相对位置应保持不变.排序方法如图所示. 排序 ...