Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6548238
  • 博文数量: 1005
  • 博客积分: 8199
  • 博客等级: 中将
  • 技术积分: 13071
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-25 20:19
个人简介

脚踏实地、勇往直前!

文章分类

全部博文(1005)

文章存档

2020年(2)

2019年(93)

2018年(208)

2017年(81)

2016年(49)

2015年(50)

2014年(170)

2013年(52)

2012年(177)

2011年(93)

2010年(30)

分类: Java

2013-08-02 14:03:20

    如下代码实现一个文件内容的拷贝,原文件为b.txt,拷贝生成的文件为b.txt_bak.

点击(此处)折叠或打开

  1. package com.hxl;

  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStreamWriter;
  7. import java.io.StringReader;

  8. public class MemoryInput {
  9.     public static void main(String[] args) throws IOException {

  10.         File file = new File("C:\\dir\\b.txt_bak");
  11.         // System.out.println(file.getAbsolutePath());


  12.         FileOutputStream fos = new FileOutputStream("C:\\dir\\b.txt_bak");
  13.         OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
  14.         BufferedWriter out = new BufferedWriter(osw);

  15.         if (!(file.exists())) {
  16.             if (!(file.createNewFile())) {
  17.                 System.out.println("The file created failed!");
  18.             }
  19.             System.out.println(file.getAbsolutePath());
  20.         }
  21.         ;

  22.         StringReader in = new StringReader(
  23.                 BufferedInputFile.read("C:\\dir\\b.txt"));

  24.         int c;
  25.         while ((c = in.read()) != -1) {
  26.             System.out.print((char) c);
  27.             if (c == '\n') {
  28.                 out.newLine();
  29.             } else {
  30.                 out.write(c);
  31.             }
  32.         }
  33.         out.flush();
  34.     }

  35. }
-- The End --

阅读(2668) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~