package com.itheima_06;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
* 复制文本文件(5种方式)
*
* 数据源:
* FileWriterDemo.java
* 目的地:
* Copy.java
*/
public class CopyFileTest {
public static void main(String[] args) throws IOException {
/*
method1("FileWriterDemo.java","Copy.java");
method2("FileWriterDemo.java","Copy.java");
method3("FileWriterDemo.java","Copy.java");
method4("FileWriterDemo.java","Copy.java");
method5("FileWriterDemo.java","Copy.java");
*/
String srcFileName = "FileWriterDemo.java";
String destFileName = "Copy.java";
// method1(srcFileName,destFileName);
// method2(srcFileName,destFileName);
method3(srcFileName,destFileName);
// method4(srcFileName,destFileName);
// method5(srcFileName,destFileName);
}
//缓冲流一次读写一个字符串
public static void method5(String srcFileName,String destFileName) throws IOException {
//创建输入缓冲流对象
BufferedReader br = new BufferedReader(new FileReader(srcFileName));
//创建输出缓冲流对象
BufferedWriter bw = new BufferedWriter(new FileWriter(destFileName));
//一次读写一个字符串
String line;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
bw.close();
br.close();
}
publicclass StringDemo { publicstatic void main(String[] args) { //方式1 //String(Stringoriginal):把字符串数据封装成字符串对象 Strings1 = new String("hello"); System.out.println("s1:"+s1); System.out.println("---------"); //方式2 //String(char[]value):把字符数组的数据封装成字符串对象 char[]chs = {'h','e','l','l','o'}; Strings2 = new String(chs); System.out.println("s2:"+s2); System.out.println("---------");
|