1、File类
1.1、File类概述和构造方法
import java.io.File;
public class work12 {
public static void main(String[] args){
File file1 = new File("E:\\File测试\\java2.text");
System.out.println(file1); //E:\File测试\java2.text
File file2 = new File("E:\\File测试","java2.text");
System.out.println(file2); //E:\File测试\java2.text
File f3 = new File("E:\\File测试");
File f4 = new File(f3,"java2.text");
System.out.println(f4); //E:\File测试\java2.text
}
}
1.2、File创建文件,目录,多层目录
import java.io.File;
import java.io.IOException;
public class work12 {
public static void main(String[] args) throws IOException {
File file1 = new File("E:\\File测试\\java.text");
System.out.println(file1.createNewFile());
File file2 = new File("E:\\File测试\\itheima");
System.out.println(file2.mkdir());
File file3 = new File("E:\\File测试\\javase\\javaee");
System.out.println(file3.mkdirs());
}
}
1.3File类的删除功能
1.4、File类判断和获取功能
import java.io.File;
import java.io.IOException;
public class work12 {
public static void main(String[] args) throws IOException {
File file = new File("E:\\File测试\\java.text");
System.out.println(file.isDirectory()); //false
System.out.println(file.isFile()); //true
System.out.println(file.exists()); //true
System.out.println("-----------");
System.out.println(file.getAbsolutePath()); //E:\File测试\java.text
System.out.println(file.getPath()); //E:\File测试\java.text
System.out.println(file.getName()); //java.text
System.out.println("-----------");
File file2 = new File("E:\\File测试");
String[] arry = file2.list();
for(String s : arry){
System.out.println(s); //itheima java.text javase
}
System.out.println("-----------");
File[] files = file2.listFiles();
for(File f : files){
// System.out.println(f);
// System.out.println(f.getName());
if(f.isFile()){
System.out.println(f.getName()); //java.text
}
}
}
}
案例:求一个数的阶乘(递归实现)
import java.util.Scanner;
public class work12 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数字:");
int a = sc.nextInt();
int b = jc(a);
System.out.println(a+"的阶乘为:"+b);
}
public static int jc(int n){
if(n == 1){
return 1;
}else {
return n*jc(n-1);
}
}
}
案例:遍历所有目录及输出目录下的所有文件(递归实现)
import java.io.File;
public class work12 {
public static void main(String[] args){
File file = new File("E:\\File测试");
getAllatribuate(file);
}
public static void getAllatribuate(File file){
File[] files = file.listFiles();
if(files != null){
for(File f : files){
if(f.isDirectory()){
getAllatribuate(f);
}else {
System.out.println(f.getAbsolutePath());
}
}
}
}
}
2、字节流
2.1、IO流概述和分类
2.2 字节流写入数据
public class work12 {
public static void main(String[] args) throws Exception{
FileOutputStream fileOutputStream = new FileOutputStream("E:\\File测试\\java.text");
/*
FileOutputStream有三个功能:
A:调用系统功能创建了文件
B:创建了字节输出流对象
C:让字节输出流对象指向创建好的对象
*/
fileOutputStream.write(97);
fileOutputStream.close();
}
}
2.3字节流写输入的三种方式
import java.io.File;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
public class work12 {
public static void main(String[] args) throws Exception{
FileOutputStream fileOutputStream = new FileOutputStream("E:\\File测试\\java.text");
/*
FileOutputStream有三个功能:
A:调用系统功能创建了文件
B:创建了字节输出流对象
C:让字节输出流对象指向创建好的对象
*/
// fileOutputStream.write(97);
// fileOutputStream.write(98);
// fileOutputStream.write(99);
// fileOutputStream.write(100);
// byte[] b = {97,98,99,100};
// fileOutputStream.write(b);
byte[] c = "abcd".getBytes();
// fileOutputStream.write(c);
fileOutputStream.write(c,1,2);
fileOutputStream.close();
}
}
2.4解决两个小问题
2.5、字节流加异常处理
import java.io.FileOutputStream;
import java.io.IOException;
public class work12 {
public static void main(String[] args){
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream("E:\\File测试\\java.text");
fileOutputStream.write("hello".getBytes());
}catch (IOException e){
e.printStackTrace();
}finally {
if(fileOutputStream != null){
try {
fileOutputStream.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
}
2.6.1字节流读数据(一次读一个字节数据)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class work12 {
public static void main(String[] args) throws IOException{
FileInputStream fileInputStream = new FileInputStream("E:\\File测试\\java.text");
int b;
while (( b =fileInputStream.read() )!= -1){
System.out.print((char)b);
}
fileInputStream.close();
}
}
案例:字节流复制文本文件
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class work12 {
public static void main(String[] args) throws IOException{
FileInputStream fileInputStream = new FileInputStream("E:\\File测试\\java.text");
FileOutputStream fileOutputStream = new FileOutputStream("E:\\File测试\\javase\\javaee\\javase.text");
int b;
while ((b = fileInputStream.read()) != -1){
fileOutputStream.write(b);
}
fileInputStream.close();
fileOutputStream.close();
}
}
2.6.2、字节流读数据(一次读一个字节数组数据)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class work12 {
public static void main(String[] args) throws IOException{
FileInputStream fileInputStream = new FileInputStream("E:\\File测试\\java.text");
byte[] bytes = new byte[1024];
int len;
while ((len = fileInputStream.read(bytes)) != -1){
System.out.println(new String(bytes,0,len));
}
fileInputStream.close();
}
}
案例:字节流复制图片
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class work12 {
public static void main(String[] args) throws IOException{
FileInputStream fileInputStream = new FileInputStream("E:\\File测试\\mn.jpg");
FileOutputStream fileOutputStream = new FileOutputStream("E:\\File测试\\itheima\\new.jpg");
byte[] bytes = new byte[1024];
int len;
while ((len = fileInputStream.read(bytes)) != -1){
fileOutputStream.write(bytes);
}
fileInputStream.close();
fileOutputStream.close();
}
}
2.7、字节缓冲流
import java.io.*;
public class work12 {
public static void main(String[] args) throws IOException{
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("E:\\File测试\\file.text"));
bufferedOutputStream.write("hello\r\n".getBytes());
bufferedOutputStream.write("world\r\n".getBytes());
bufferedOutputStream.close();
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("E:\\File测试\\file.text"));
//一次读取一个字节
int by;
while ((by = bufferedInputStream.read()) != -1){
System.out.print((char)by);
}
System.out.println("--------");
//一次读取一个字节数组
byte[] bytes = new byte[1024];
int len;
while ((len = bufferedInputStream.read(bytes)) != -1){
System.out.print(new String(bytes,0,len));
}
bufferedInputStream.close();
}
}
案例:字节流复制视频
import java.io.*;
public class work12 {
public static void main(String[] args) throws IOException{
//记录开始时间
long start = System.currentTimeMillis();
//method1(); //29750毫秒
//method2(); //47毫秒
//method3(); //134毫秒
method4(); //20毫秒
//记录结束时间
long end = System.currentTimeMillis();
long time = end - start;
System.out.println(time+"毫秒");
}
//基本字节流一次读写一个字节
public static void method1() throws IOException {
FileInputStream fileInputStream = new FileInputStream("E:\\File测试\\8f43b04323d298dcaab2900e2488f2cb.mp4");
FileOutputStream fileOutputStream = new FileOutputStream("E:\\File测试\\itheima\\8f43b04323d298dcaab2900e2488f2cb.mp4");
int by;
while ((by = fileInputStream.read()) != -1){
fileOutputStream.write(by);
}
fileInputStream.close();
fileOutputStream.close();
}
//基本字节流一次读写一个字节数组
public static void method2() throws IOException {
FileInputStream fileInputStream = new FileInputStream("E:\\File测试\\8f43b04323d298dcaab2900e2488f2cb.mp4");
FileOutputStream fileOutputStream = new FileOutputStream("E:\\File测试\\itheima\\8f43b04323d298dcaab2900e2488f2cb.mp4");
byte[] bytes = new byte[1024];
int len;
while ((len = fileInputStream.read(bytes)) != -1){
fileOutputStream.write(bytes);
}
fileInputStream.close();
fileOutputStream.close();
}
//字节缓冲流一次读写一个字节
public static void method3() throws IOException{
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("E:\\File测试\\8f43b04323d298dcaab2900e2488f2cb.mp4"));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("E:\\File测试\\itheima\\8f43b04323d298dcaab2900e2488f2cb.mp4"));
int by;
while ((by = bufferedInputStream.read()) != -1){
bufferedOutputStream.write(by);
}
bufferedInputStream.close();
bufferedOutputStream.close();
}
//字节缓冲流一次读写一个字节数组
public static void method4() throws IOException{
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("E:\\File测试\\8f43b04323d298dcaab2900e2488f2cb.mp4"));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("E:\\File测试\\itheima\\8f43b04323d298dcaab2900e2488f2cb.mp4"));
byte[] bytes = new byte[1024];
int len;
while ((len = bufferedInputStream.read(bytes)) != -1){
bufferedOutputStream.write(bytes,0,len);
}
bufferedInputStream.close();
bufferedOutputStream.close();
}
}
3、字符流
3.1、为什么要出现字符流
一个汉字存储:
(1)如果用GBK编码,占用两个字节
(2)如果用UTF-8存储,占用三个字节
// byte[] bytes = s.getBytes(); //[-28, -72, -83, -27, -101, -67]
// byte[] bytes1 = s.getBytes("UTF-8"); //[-28, -72, -83, -27, -101, -67]
// byte[] bytes2 = s.getBytes("GBK"); //[-42, -48, -71, -6]
3.2、编码表
3.2.1、ASCII字符集
3.2.2、GBXXX字符集
3.2.3、Unicode字符集
3.3、字符串中的编码解码问题
import java.io.*;
public class work12 {
public static void main(String[] args) throws IOException{
String s = "中国";
//使用平台默认的字符集格式编码
byte[] bytes = s.getBytes(); //[-28, -72, -83, -27, -101, -67]
// byte[] bytes1 = s.getBytes("UTF-8"); //[-28, -72, -83, -27, -101, -67]
// byte[] bytes2 = s.getBytes("GBK"); //[-42, -48, -71, -6]
//使用平台默认的字符集解码
String ss = new String(bytes);
// String ss = new String(bytes,"UTF-8");
// String ss = new String(bytes,"GBK");
System.out.println(ss);
}
}
3.4、字符流中的编码解码问题
import java.io.*;
public class work12 {
public static void main(String[] args) throws IOException{
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("E:\\File测试\\file.text"),"GBK");
outputStreamWriter.write("中国");
outputStreamWriter.close();
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("E:\\File测试\\file.text"),"GBK");
int by;
while ((by = inputStreamReader.read()) != -1){
System.out.print((char)by);
}
inputStreamReader.close();
}
}
3.5、字符流写入数据的五种方式
import java.io.*;
public class work12 {
public static void main(String[] args) throws IOException{
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("E:\\File测试\\file.text"));
//写入一个字符
outputStreamWriter.write(97);
//刷新流
outputStreamWriter.flush();
//写入一个字符数组
char[] chars = {'a','b','c'};
outputStreamWriter.write(chars);
//写入字符数组的一部分
char[] chars1 = {'a','b','c'};
outputStreamWriter.write(chars1,0,chars1.length);
//写入一个字符串
outputStreamWriter.write("abcde");
//写入一个字符串的一部分
outputStreamWriter.write("abcde",1,3);
//关闭流,关闭前先刷新
outputStreamWriter.close();
}
}
3.6、字符流读数据的两种方式
import java.io.*;
public class work12 {
public static void main(String[] args) throws IOException{
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("E:\\File测试\\java.text"));
//一次读取一个字符数据
int by;
while((by = inputStreamReader.read()) != -1){
System.out.print((char) by);
}
//一次读取一个字符数组
char[] chars = new char[1024];
int len;
while ((len = inputStreamReader.read(chars)) != -1){
System.out.print(new String(chars,0,len));
}
inputStreamReader.close();
}
}
案例:字符流复制java文件
import java.io.*;
public class work12 {
public static void main(String[] args) throws IOException{
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("C:\\Users\\Administrator\\IdeaProjects\\work\\work1.java"));
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\\Users\\Administrator\\IdeaProjects\\work\\work2.java"));
//一次读取一个字符数据
int by;
while((by = inputStreamReader.read()) != -1){
outputStreamWriter.write(by);
}
//一次读取一个字符数组
char[] chars = new char[1024];
int len;
while ((len = inputStreamReader.read(chars)) != -1){
outputStreamWriter.write(chars,0,len);
}
inputStreamReader.close();
outputStreamWriter.close();
}
}
改进版字符流复制java文件
import java.io.*;
public class work12 {
public static void main(String[] args) throws IOException{
FileReader fileReader = new FileReader("C:\\Users\\Administrator\\IdeaProjects\\work\\work1.java");
FileWriter fileWriter = new FileWriter("C:\\Users\\Administrator\\IdeaProjects\\work\\work2.java");
//一次读取一个字符数据
int by;
while((by = fileReader.read()) != -1){
fileWriter.write(by);
}
//一次读取一个字符数组
char[] chars = new char[1024];
int len;
while ((len = fileReader.read(chars)) != -1){
fileWriter.write(chars,0,len);
}
fileReader.close();
fileWriter.close();
}
}
3.7、字符缓冲流
import java.io.*;
public class work12 {
public static void main(String[] args) throws IOException{
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\File测试\\java.text"));
bw.write("中国\r\n");
bw.write("奥运会\r\n");
bw.write("加油");
bw.close();
BufferedReader br = new BufferedReader(new FileReader("E:\\File测试\\java.text"));
//一次读取一个字符数据
int by;
while ((by = br.read()) != -1){
System.out.print((char)by);
}
//一次读取一个字符数组数据
char[] chars = new char[1024];
int len;
while ((len = br.read(chars)) != -1){
System.out.print(new String(chars,0,len));
}
br.close();
}
}
案例:字符缓冲流复制java文件
import java.io.*;
public class work12 {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Administrator\\IdeaProjects\\work\\work1.java"));
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\Administrator\\IdeaProjects\\work\\work2.java"));
//一次读取一个字符数据
int by;
while ((by = br.read()) != -1){
bw.write(by);
}
//一次读取一个字符数组数据
char[] chars = new char[1024];
int len;
while ((len = br.read(chars)) != -1){
bw.write(chars,0,len);
}
br.close();
bw.close();
}
}
3.8、字符缓冲流的特有功能
import java.io.*;
public class work12 {
public static void main(String[] args) throws IOException{
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\\\File测试\\\\java.text"));
BufferedReader br = new BufferedReader(new FileReader("E:\\\\File测试\\\\java.text"));
bw.write("中国");
bw.newLine();
bw.write("必胜");
String line;
while ((line = br.readLine()) != null){
System.out.println(line);
}
br.close();
bw.close();
}
}
案例:字符缓冲流特有功能复制java文件
import java.io.*;
public class work12 {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Administrator\\IdeaProjects\\work\\work1.java"));
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\Administrator\\IdeaProjects\\work\\work2.java"));
String line;
while ((line = br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
}
3.9、IO流小结
案例:集合到文件
import java.io.*;
import java.util.ArrayList;
public class work12 {
public static void main(String[] args) throws IOException{
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("hello");
arrayList.add("world");
arrayList.add("java");
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\File测试\\java.text"));
for(String s : arrayList){
bw.write(s);
bw.newLine();
bw.flush();
}
bw.close();
}
}
案例:文件到集合
import java.io.*;
import java.util.ArrayList;
public class work12 {
public static void main(String[] args) throws IOException{
ArrayList<String> arrayList = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader("E:\\File测试\\java.text"));
String line;
while ((line = br.readLine()) != null){
arrayList.add(line);
}
br.close();
for(String s : arrayList){
System.out.println(s);
}
}
}
案例:随机点名器
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
public class work12 {
public static void main(String[] args) throws IOException{
ArrayList<String> arrayList = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader("E:\\File测试\\java.text"));
String line;
while ((line = br.readLine()) != null){
arrayList.add(line);
}
br.close();
Random r = new Random();
int index = r.nextInt(arrayList.size());
String name = arrayList.get(index);
System.out.println("本期的幸运儿是:" + name );
}
}
案例:集合到文件进阶版
//测试类
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
public class work12 {
public static void main(String[] args) throws IOException{
ArrayList<student> arrayList = new ArrayList<>();
student s1 = new student("01","李白",18,"打野");
student s2 = new student("02","西施",16,"中单");
student s3 = new student("03","凯",18,"上单");
student s4 = new student("04","伽罗",18,"射手");
arrayList.add(s1);
arrayList.add(s2);
arrayList.add(s3);
arrayList.add(s4);
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\File测试\\java.text"));
//用字符串拼接
String x = "";
for(student s : arrayList){
x = s.getId() + "," + s.getName() + "," + s.getAge() + "," + s.getAddress();
bw.write(x);
bw.newLine();
bw.flush();
}
//用StringBuilder拼接
for(student s : arrayList){
StringBuilder sb = new StringBuilder();
sb.append(s.getId()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddress());
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
bw.close();
}
}
//学生类
public class student {
private String id;
private String name;
private int age;
private String address;
public student() {
}
public student(String id,String name,int age,String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
案例:文件到集合进阶版
//测试类
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
public class work12 {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new FileReader("E:\\File测试\\java.text"));
ArrayList<student> arrayList = new ArrayList<>();
String line;
while((line = br.readLine()) != null){
student s = new student();
String[] strry = line.split(",");
s.setId(strry[0]);
s.setName(strry[1]);
s.setAge(Integer.parseInt(strry[2]));
s.setAddress(strry[3]);
arrayList.add(s);
}
br.close();
for(student s: arrayList){
System.out.println(s.getId()+","+s.getName()+","+s.getAge()+","+s.getAddress());
}
}
}
//学生类
public class student {
private String id;
private String name;
private int age;
private String address;
public student() {
}
public student(String id,String name,int age,String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
案例:集合到文件排序进阶版
import java.io.*;
import java.util.*;
public class work12 {
public static void main(String[] args) throws IOException{
TreeSet<student> treeSet = new TreeSet<>(new Comparator<student>() {
@Override
public int compare(student o1, student o2) {
int num = o1.getChinese()+o1.getMath()+o1.getEnglish() - o2.getChinese()-o2.getMath()-o2.getEnglish();
int num1 = num == 0?o1.getName().compareTo(o2.getName()):num;
return num1;
}
});
for(int i=0;i<4;i++){
student s = new student();
Scanner sc = new Scanner(System.in);
System.out.println("请输入第"+(i+1)+"个学生数据:");
System.out.println("请输入姓名:");
String name = sc.nextLine();
System.out.println("请输入语文成绩:");
int chinese = sc.nextInt();
System.out.println("请输入数学成绩:");
int math = sc.nextInt();
System.out.println("请输入英语成绩:");
int english = sc.nextInt();
s.setName(name);
s.setChinese(chinese);
s.setMath(math);
s.setEnglish(english);
treeSet.add(s);
}
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\File测试\\java.text"));
for(student s : treeSet){
StringBuilder sb = new StringBuilder();
int ss = s.getChinese() + s.getMath() + s.getEnglish();
sb.append(s.getName()+","+s.getChinese()+","+s.getMath()+","+s.getEnglish()+","+ss);
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
bw.close();
}
}
案例:复制单级目录
import java.io.*;
public class work12 {
public static void main(String[] args) throws IOException{
File start = new File("E:\\File测试\\itheima");
String name = start.getName();
File ends = new File("E:\\File测试\\javase",name);
if(!ends.exists()){
ends.mkdir();
}
File[] files = start.listFiles();
if(files != null) {
for (File f : files) {
String names = f.getName();
File file = new File(ends, names);
copy(f, file);
}
}
}
private static void copy(File f, File file) throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
byte[] bytes = new byte[1024];
int len;
while ((len = bis.read(bytes)) != -1){
bos.write(bytes,0,len);
}
bis.close();
bos.close();
}
}
案例:复制多级目录
//自己的方法
import java.io.*;
public class work12 {
public static void main(String[] args) throws IOException {
File start = new File("E:\\File测试\\itheima");
File ends = new File("E:\\File测试\\javase");
copyFolder(start,ends);
}
private static void copyFolder (File start, File ends) throws IOException {
String name = start.getName();
File file = new File(ends,name);
if(!file.exists()){
file.mkdir();
}
File[] files = start.listFiles();
for(File f : files){
if(f.isDirectory()){
copyFolder(f,file);
}else{
String names = f.getName();
File file1 = new File(file,names);
copyFile(f,file1);
}
}
}
private static void copyFile(File f, File file1) throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file1));
byte[] bytes = new byte[1024];
int len;
while ((len = bis.read(bytes)) != -1){
bos.write(bytes,0,len);
}
bis.close();
bos.close();
}
}
//老师的方法
import java.io.*;
public class work12 {
public static void main(String[] args) throws IOException {
File start = new File("E:\\File测试\\itheima");
File ends = new File("E:\\File测试\\javase");
copyFolder(start,ends);
}
private static void copyFolder (File start, File ends) throws IOException {
if (start.isDirectory()) {
String name = start.getName();
File file = new File(ends, name);
if (!file.exists()) {
file.mkdir();
}
File[] files = start.listFiles();
for (File f : files) {
copyFolder(f, file);
}
}else{
File file = new File(ends,start.getName());
copyFile(start,file);
}
}
private static void copyFile(File f, File file1) throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file1));
byte[] bytes = new byte[1024];
int len;
while ((len = bis.read(bytes)) != -1){
bos.write(bytes,0,len);
}
bis.close();
bos.close();
}
}
3.10、复制文件的异常处理
//try....catch....finally
import java.io.*;
public class work12 {
public static void main(String[] args){
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader("E:\\File测试\\java.txt");
fw = new FileWriter("E:\\File测试\\javaee.txt");
char[] chars = new char[1024];
int len;
while ((len = fr.read(chars)) != -1){
fw.write(chars,0,len);
}
}catch (IOException e){
e.printStackTrace();
}finally {
try {
fr.close();
}catch (IOException e){
e.printStackTrace();
}
try {
fw.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
//JDK7改进方案
import java.io.*;
public class work12 {
public static void main(String[] args){
try (FileReader fr = new FileReader("E:\\File测试\\java.txt");
FileWriter fw = new FileWriter("E:\\File测试\\javaee.txt");){
char[] chars = new char[1024];
int len;
while ((len = fr.read(chars)) != -1){
fw.write(chars,0,len);
}
}catch (IOException e){
e.printStackTrace();
}
}
}
//JDK9改进方案
import java.io.*;
public class work12 {
public static void main(String[] args) throws IOException{
FileReader fr = new FileReader("E:\\File测试\\java.txt");
FileWriter fw = new FileWriter("E:\\File测试\\javaee.txt");
try (fr;fw){
char[] chars = new char[1024];
int len;
while ((len = fr.read(chars)) != -1){
fw.write(chars,0,len);
}
}catch (IOException e){
e.printStackTrace();
}
}
}
4、特殊操作流
4.1标准输入输出流
4.2打印流
案例:用打印流改进复制java文件
import java.io.*;
public class work12 {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Administrator\\IdeaProjects\\work\\work1.java"));
// BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\Administrator\\IdeaProjects\\work\\work2.java"));
PrintWriter pw = new PrintWriter(new FileWriter("C:\\Users\\Administrator\\IdeaProjects\\work\\work2.java"));
String line;
while ((line = br.readLine()) != null){
// bw.write(line);
// bw.newLine();
// bw.flush();
pw.println(line);
}
br.close();
// bw.close();
pw.close();
}
}
4.3对象序列化流和对象反序列化流
例: (学生类要实现Serializable接口)
例:
4.3.1对象序列化流中的问题
4.4、Properties
import java.util.Properties;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Properties pro = new Properties();
pro.put("001","王昭君");
pro.put("002","西施");
pro.put("003","貂蝉");
pro.put("004","杨玉环");
Set<Object> set = pro.keySet();
for(Object o : set){
Object x = pro.get(o);
System.out.println(o+","+x);
}
}
}
4.4.1、Properties特有方法
import java.util.Properties;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Properties pro = new Properties();
pro.setProperty("001","王昭君");
pro.setProperty("002","西施");
pro.setProperty("003","貂蝉");
pro.setProperty("004","杨玉环");
// System.out.println(pro); //{001=王昭君, 002=西施, 003=貂蝉, 004=杨玉环}
// System.out.println(pro.getProperty("001")); //王昭君
// System.out.println(pro.getProperty("002")); //西施
// System.out.println(pro.getProperty("003")); //貂蝉
// System.out.println(pro.getProperty("004")); //杨玉环
Set<String> names = pro.stringPropertyNames();
for(String s : names){
String values = pro.getProperty(s);
System.out.println(s+","+values);
}
/*
001,王昭君
002,西施
003,貂蝉
004,杨玉环
*/
}
}
4.4.2、Properties和IO流结合
字符流
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class Main {
public static void main(String[] args) throws IOException{
mystore();
myload();
}
private static void myload() throws IOException{
Properties pro = new Properties();
FileReader fr = new FileReader("E:\\File测试\\java.txt");
pro.load(fr);
fr.close();
System.out.println(pro); //{001=王昭君, 002=西施, 003=貂蝉, 004=杨玉环}
}
private static void mystore() throws IOException {
Properties pro = new Properties();
pro.setProperty("001","王昭君");
pro.setProperty("002","西施");
pro.setProperty("003","貂蝉");
pro.setProperty("004","杨玉环");
FileWriter fw = new FileWriter("E:\\File测试\\java.txt");
pro.store(fw,"Pro集合"); //第二个参数为描述信息,没有描述信息写null
fw.close();
}
}
字节流
import java.io.*;
import java.util.Properties;
public class Main {
public static void main(String[] args) throws IOException{
mystore();
myload();
}
private static void myload() throws IOException{
Properties pro = new Properties();
FileInputStream fis = new FileInputStream("E:\\File测试\\java.txt");
pro.load(fis);
fis.close();
System.out.println(pro); //{001=王昭君, 002=西施, 003=貂蝉, 004=杨玉环}
}
private static void mystore() throws IOException {
Properties pro = new Properties();
pro.setProperty("001","王昭君");
pro.setProperty("002","西施");
pro.setProperty("003","貂蝉");
pro.setProperty("004","杨玉环");
FileOutputStream fos = new FileOutputStream("E:\\File测试\\java.txt");
pro.store(fos,"Pro集合"); //第二个参数为描述信息,没有描述信息写null
fos.close();
}
}
案例:游戏次数
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException{
Properties pro = new Properties();
FileReader fr = new FileReader("E:\\File测试\\java.txt");
pro.load(fr);
fr.close();
int count = Integer.parseInt(pro.getProperty("count"));
if(count >= 3){
System.out.println("你的次数已经用完,请登陆进行充值(www.baidu.com)");
}else {
GuessGame();
count ++;
pro.setProperty("count",String.valueOf(count));
FileWriter fw = new FileWriter("E:\\File测试\\java.txt");
pro.store(fw,null);
fw.close();
}
}
public static void GuessGame(){
Random r = new Random();
int guess = r.nextInt(100);
boolean b = true;
while (b){
Scanner sc = new Scanner(System.in);
System.out.println("请输入你猜测的数字:");
int num = sc.nextInt();
if(num == guess){
System.out.println("恭喜你猜对了!");
b = false;
}else if(num > guess){
System.out.println("你猜的数大了!");
}else {
System.out.println("你猜的数小了!");
}
}
}
}