@Test
public void copy() {
String str1 = new String("HelloWorld.txt");
String str2 = new String("HelloWorld2.txt");
File file1 = new File(str1);
File file2 = new File(str2);
FileReader fr = null;
FileWriter fw = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
fr = new FileReader(file1);
fw = new FileWriter(file2);
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
//此方法可以将读取到的数据换行
/*char[] c = new char[1024];
int len;
while((len = br.read(c)) != -1){
String str = new String(c,0,len);
bw.write(str);
bw.flush();
}*/
//此方法不能将读取到的数据换行
String str ;
while((str = br.readLine()) != null){
bw.write(str);
System.out.println(str);
bw.flush();
}
} catch (Exception e) {
// TODO: handle exception
}finally{
if(bw != null){
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(br != null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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();
}
}
}
}
关于IO流
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 转换流 输入字节流的转换流:InputStreamReader 是字节流通向字符流的桥InputStreamRea...
- 一、IO流的概念 Java的IO流是实现输入/输出的基础,它可以方便地实现数据的输入/输出操作,在Java中把不同...