黑马程序员技术交流社区
标题: 重要的案例 [打印本页]
作者: 吕杰 时间: 2018-4-17 23:45
标题: 重要的案例
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("---------");
作者: cj1234 时间: 2018-4-18 14:57
加油,把基础都掌握了。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |