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

      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();
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值