您的位置:首页 > 编程语言 > Java开发

Java io初探(1),使用java流复制文件

2015-01-05 13:42 441 查看
Java IO包是十分强大的工具。Java IO包的说明是:Provides for system input and output through data streams, serialization and the file system.为系统提供了通过数据流、序列化和文件系统进行输入输出。也就是java对文件、内存到CPU、等数据流向进行操作的包。现在为了学习,测试了从一个文件拷贝数据到另一个文件的java实现,贴代码如下

package javaIO;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.Reader;

import java.io.Writer;

public class CopyFile {

private String srcFileName; //源文件名(使用完整路径)

private String tarFileName; //目的文件名(使用完整路径)

private File srcFile; //源文件

private File tarFile; //目的文件

/**

* 构造函数,对文件进行预处理和包装

* @param srcFileName 源文件名(使用完整路径)

* @param tarFileName 目的文件名(使用完整路径)

*/

CopyFile(String srcFileName,String tarFileName)

{

this.srcFileName = srcFileName;

this.tarFileName = tarFileName;

tarFileUtil(tarFileName); //先对母的文件进行检查、生成

//使用File()进行包装

srcFile = new File(srcFileName);

tarFile = new File(tarFileName);

}

public void copyStreamType()

{

try

{

//InputStream是abstract类,不可以实例化,需要使用FileInputStream

InputStream is = new FileInputStream(srcFile);

OutputStream os = new FileOutputStream(tarFile);

byte [] b = new byte[2048];

while(is.read(b)!=-1)

{

os.write(b);

}

os.close();

is.close();

}

catch(Exception e)

{

e.getStackTrace();

System.out.println(e.toString());

}

}

public void copyCharacterType()

{

try {

FileReader fr = new FileReader(srcFile);

//这里注意要使用append的方式写入,否则只有最后的写入

FileWriter fw = new FileWriter(tarFile, true);

char[] tmp = new char[1024];

while(fr.read(tmp)!=-1)

{

fw.write(tmp);

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

//使用带缓冲区的流读写方式复制文件

public void copyBufferedStream()

{

try{

InputStream is = new FileInputStream(srcFile);

OutputStream os = new FileOutputStream(tarFile);

BufferedInputStream bis = new BufferedInputStream(is);

BufferedOutputStream bos = new BufferedOutputStream(os);

byte[] tmp = new byte[1024];

while(bis.read(tmp)!= -1)

{

bos.write(tmp);

}

bos.close();

bis.close();

os.close();

is.close();

}catch(Exception e)

{

e.printStackTrace();

}

}

public void copyBufferCharacter()

{

try{

Reader myReader = new FileReader(srcFile);

Writer myWriter = new FileWriter(tarFile);

BufferedReader br = new BufferedReader(myReader);

BufferedWriter bw = new BufferedWriter(myWriter);

char[] tmp = new char[1024];

while(br.read(tmp)!=-1)

{

bw.write(tmp);

}

bw.close();

br.close();

myReader.close();

myWriter.close();

}catch(Exception e)

{

e.printStackTrace();

}

}

/**

* 目的文件的处理类,如果目的文件没有检测到则生成目的文件

* @param tarFile 目的文件的完整路径

* @return 如果文件建立成功或本身有该文件返回true,否则返回false

*/

public void tarFileUtil(String tarFile)

{

File file = new File(tarFile); //先进行包装

if(file.isFile())

{

}

else //如果没有文件,则需对路径进行检查,看是否需要建立目录

{

String path = file.getParent();

//得到该文件的父目录检查该父目录的有效性,若无效则声称父目录

if(new File(path).isDirectory())

{

try {

file.createNewFile();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

else

{

new File(path).mkdirs(); //这里对父目录进行生成,mkdirs()方法生成多级目录

try {

file.createNewFile();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

package javaIO;

public class Test {

public static void main(String[] args)

{

CopyFile myCopyFile = new CopyFile("f:\\a.txt","f:\\java_text1\\a.txt");

//myCopyFile.copyBufferCharacter();

//myCopyFile.copyCharacterType();

myCopyFile.copyBufferedStream();

//myCopyFile.copyStreamType();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: