java 文件io操作_java文件IO操作。

public static void main(String[] args) {

String filename = "F:/zhangming_test/test.txt";

String filename1 = "F:/zhangming_test/test1.txt";

String docfileName = "F:/猪猪猫xp系统.rar";

// String filename = "F:/zhangming_test/test_doc.doc";

// readFile(filename);

// copyFileBychar(filename);

copyFileByByte(docfileName);

System.out.println("ddd");

}

/**

* 该方法只支持读取字符文件并复制文件. 按行读取的 文件锁未考虑

*

* @param filename

*/

public static void copyFileBychar(String filename) {

FileReader fileReader = null;

BufferedWriter bufferedWriter = null;

try {

bufferedWriter = new BufferedWriter(new PrintWriter(new File(

"F:/zhangming_test/test2.txt")));

fileReader = new FileReader(new File(filename));

} catch (FileNotFoundException e) {

e.printStackTrace();

}

BufferedReader bufferedReader = new BufferedReader(fileReader);

String linecontent = null;

try {

// 調用readLine方法會移動流的指針

while ((linecontent = bufferedReader.readLine()) != null) {

System.out.println(linecontent);

bufferedWriter.write(linecontent);

}

bufferedWriter.flush();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

bufferedReader.close();

bufferedWriter.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 每次读取一定数量byte的文件流. 文件锁未考虑

* @param inputFileName

*/

public static void copyFileByByte(String inputFileName) {

InputStream inputStream = null;

OutputStream outputStream = null;

try {

inputStream = new FileInputStream(new File(inputFileName));

// ==方法一(一次性把文件的所有内容都添加读取到byte中)==

// inputStream.read(bs);

// outputStream = new FileOutputStream(new File(

// "F:/zhangming_test/test6.doc"));

// outputStream.write(bs);

// ==方法二==

outputStream = new FileOutputStream(new File(

"F:/猪猪猫xp系统1.rar"));

byte[] tmpBytes = new byte[1024];

while (inputStream.read(tmpBytes) != -1) {

outputStream.write(tmpBytes, 0, tmpBytes.length);

}

outputStream.flush();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

} finally {

try {

outputStream.close();

inputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

public static void copyFileWithNio(String srcFile, String destFile) {

try {

FileChannel srcChannel = new FileInputStream(new File(srcFile))

.getChannel();

FileChannel destChannel = new FileOutputStream(new File(destFile))

.getChannel();

try {

destChannel.transferFrom(srcChannel, 0, srcChannel.size());

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

srcChannel.close();

destChannel.close();

} catch (IOException e) {

e.printStackTrace();

}

}

} catch (FileNotFoundException e) {

e.printStackTrace();

}

} //最简单,使用代码最少。

以后开发的时候要用直接拿过来修修改改就OK了。当然可以考虑使用nio读取.

1

4

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2011-08-08 14:39

浏览 1167

评论

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值