文件的复制(输入输出流)
2014-11-09 20:07
316 查看
字节流(操作字节,也可以操作字符,但是可能会产生乱码)主要有InputStream和OutputStream作为抽象基类,访问时是FileInputStream、FileOutputStream两个实现类;字符流(操作字符)主要由Reader和Writer作为抽象基类,FileReader(读取时可以一次读一行readLine())、FlieWriter作为实现类。
java中复制文件原理是就是打开一个旧文件,用输入流FileInputStream读取数据,然后在打开一个输出流FileOutputStream把数据放进去。可以理解为是两个水池,中间用byte[] 进行取水,每次取一个字节。记得放在try catch块中,关闭流的方法放在finally中。
}
Java 7改写了所有的IO资源类,它们都实现了AutoCloseable接口,因此都可以通过自动关闭资源的try语句来关闭这些IO流
<span style="white-space:pre"> </span>public static void main(String[] args){
File oldFile = new File("c:" + File.separator + "1.ini");
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
if (oldFile.exists()) {
try(<pre name="code" class="java"><span style="white-space:pre"> </span>inputStream = new FileInputStream(oldFile);
outputStream = new FileOutputStream("f:" + File.separator + oldFile.getName()); ){byte[] buffer = new byte[1024];int leath = 0;while ((leath = inputStream.read(buffer)) != -1) {outputStream.write(buffer);}} catch (IOException e) {e.printStackTrace();} }else {System.out.println("复制的文件不存在");}}
java中复制文件原理是就是打开一个旧文件,用输入流FileInputStream读取数据,然后在打开一个输出流FileOutputStream把数据放进去。可以理解为是两个水池,中间用byte[] 进行取水,每次取一个字节。记得放在try catch块中,关闭流的方法放在finally中。
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * 复制文件 * @author zxc * */ public class CopyFile { <pre name="code" class="java"><span style="white-space:pre"> </span>public static void main(String[] args){ File oldFile = new File("c:" + File.separator + "1.ini"); FileInputStream inputStream = null; FileOutputStream outputStream = null; if (oldFile.exists()) { try{ inputStream = new FileInputStream(oldFile); outputStream = new FileOutputStream("f:" + File.separator + oldFile.getName()); byte[] buffer = new byte[1024]; int leath = 0; while ((leath = inputStream.read(buffer)) != -1) { outputStream.write(buffer); } } catch (IOException e) { e.printStackTrace(); } finally{ try { inputStream.close(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }else { System.out.println("复制的文件不存在"); } }
}
Java 7改写了所有的IO资源类,它们都实现了AutoCloseable接口,因此都可以通过自动关闭资源的try语句来关闭这些IO流
<span style="white-space:pre"> </span>public static void main(String[] args){
File oldFile = new File("c:" + File.separator + "1.ini");
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
if (oldFile.exists()) {
try(<pre name="code" class="java"><span style="white-space:pre"> </span>inputStream = new FileInputStream(oldFile);
outputStream = new FileOutputStream("f:" + File.separator + oldFile.getName()); ){byte[] buffer = new byte[1024];int leath = 0;while ((leath = inputStream.read(buffer)) != -1) {outputStream.write(buffer);}} catch (IOException e) {e.printStackTrace();} }else {System.out.println("复制的文件不存在");}}
相关文章推荐
- 输入和输出流__复制文件
- Java输入输出流复制文件所用时间对比
- C++标准程序库的输入输出流(I/O Stream)复制文件(4种方法)
- java 文件输入输出流 文件的复制
- Java输入输出流 复制文件
- JavaIO 总结笔记<三> 基本字节字符输入输出流和文件复制
- java中的文件输入输出流,实现复制粘贴
- java输入输出流复制文件所用时间对比
- J2SE第八章——IO输入输出流(基本知识与IO流实现文件复制)
- 基于缓冲字节输入输出流进行复制文件的操作
- C++标准程序库的输入输出流(I/O Stream)复制文件(多种方法)
- 文件输入输出流(复制文本,视频,音频,图片的多种方式)
- 使用java输入输出流实现文件的复制:
- java 文件输入输出流 文件夹的复制
- 输入输出流-实现复制文件
- 《java中输入输出流 ,java中文件操作是很重要的》
- C++风格的文件输入输出流
- 文件输入输出流->带缓冲的输入输出流(过滤流)->基本数据的输入输出流 链接 增强了文件输入输出流的功能
- IO(1) 文件字节输入输出流
- c++ 文件复制 往文件中输入字符、数字、字符串