将C盘一个文本copy到D盘;

package 将C盘一个文本copy到D盘;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

/*步骤

* 1.在D盘创建一个文件,用于存储C盘文件中的数据

* 2.定义读取流和c盘文件关联

* 3.通过不断的读写完成数据存储

* 4. (finally中) 关闭资源 

*/

public class CopyText {

public static void main(String[] args) {

// TODO Auto-generated method stub

// copy_1();

copy_2();

}

//方式一  从C盘读一个字符,就往D盘写一个字符

private static void copy_1() {

//创建目的地

FileWriter fw = null;

FileReader fr = null;

try {

fw = new FileWriter("CopyTextTXT.txt");

fr = new FileReader("/Users/denmeiho/Documents/workspace/将C盘一个文本copy到D盘/src/将C盘一个文本copy到D盘/CopyText.java");

int ch = 0;

while ((ch=fr.read())!=-1) {

fw.write(ch);

System.out.println((char)ch);

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

if(fw!=null)

try {

fw.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

if(fr!=null)

try {

fr.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

//方式2  从C盘读一个字符,就往D盘写一个字符

private static void copy_2() {

//创建目的地

FileWriter fw = null;

FileReader fr = null;

try {

fw = new FileWriter("CopyTextTXT.txt");

fr = new FileReader("/Users/denmeiho/Documents/workspace/将C盘一个文本copy到D盘/src/将C盘一个文本copy到D盘/CopyText.java");

char[] buf = new char[1024];

int len = 0;

while ((len=fr.read(buf))!=-1) {

System.out.println(new String(buf,0,len));

fw.write(buf, 0, len);

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

if(fw!=null)

try {

fw.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

if(fr!=null)

try {

fr.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

//获取系统启动时的 属性  并打印到sysinfo.tex文件中

将C盘一个文本copy到D盘;_第1张图片

你可能感兴趣的:(将C盘一个文本copy到D盘;)