IO流綜合案例
1. 集合到檔案
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
//集合到檔案
/*
1.建立一個字元緩沖輸出流對象
2.建立一個ArrayList集合對象
3.添加元素
4.周遊集合
5.寫入資料 一次寫入一行
*/
public class Test01 {
public static void main(String[] args) throws IOException {
//建立字元緩沖輸出流對象
BufferedWriter bw = new BufferedWriter(new FileWriter("iodemo\\demo.txt"));
//建立集合對象
ArrayList<String> arr = new ArrayList<>();
//添加元素
arr.add("hello");
arr.add("world");
arr.add("java");
//周遊集合
for (String s : arr) {
//寫資料
bw.write(s);
//換行
bw.newLine();
bw.flush();
}
//釋放資源
bw.close();
}
}
2. 檔案到集合
import java.io.*;
import java.util.ArrayList;
//檔案到集合
/*
1.建立字元緩沖輸入流對象
2.建立ArrayList集合
3.讀取檔案資料 一次讀取一行
4.将讀取的資料寫入ArrayList集合中
5.周遊ArrayList集合
*/
public class Test02 {
public static void main(String[] args) throws IOException {
//建立字元緩沖輸入流對象
BufferedReader br = new BufferedReader(new FileReader("iodemo\\demo.txt"));
//建立ArrayList集合對象
ArrayList<String> arr = new ArrayList<>();
//建立一個字元串 用于接收讀取的資料
String line;
while ((line =br.readLine())!= null){
//将資料寫入集合
arr.add(line);
}
//周遊集合
for (String s : arr) {
System.out.println(s);
}
}
}
3. 點名器
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
//點名器
/*
1.建立字元緩沖輸入流對象
2.建立ArrayList集合對象
3.讀取資料 存入集合
4.生成一個随機數 整型接收并且給定範圍
5.通過随機數作為索引輸出集合對應元素
*/
public class Test03 {
public static void main(String[] args) throws IOException {
//建立字元緩沖輸入流對象
BufferedReader br = new BufferedReader(new FileReader("iodemo\\dianming.txt"));
//建立ArrayList集合對象
ArrayList<String> arr = new ArrayList<>();
//讀資料
String line;
while ((line = br.readLine())!= null){
//添加元素到集合
arr.add(line);
}
//随機生成索引
Random r = new Random();
//範圍為arr.size() 如果不給規定範圍會導緻異常 索引越界
int index = r.nextInt(arr.size());
//索引指定元素
String name = arr.get(index);
System.out.println("幸運選手:" + name);
}
}
4. 集合到檔案 (第2版)
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
//集合到檔案 集合類型為 學生類
/*
1.定義學生類
2.建立字元緩沖輸出流對象
3.建立集合對象
4.建立學生對象
5.将學生對象添加到集合
6.周遊集合 得到的是學生對象
7.按要求拼接學生對象的資訊 使用StringBuilder
8.寫資料 一次寫一行 這裡注意把StringBuilder轉為String類型寫入
9.釋放資源
*/
public class Test {
public static void main(String[] args) throws IOException {
//建立字元緩沖輸出流對象
BufferedWriter bw = new BufferedWriter(new FileWriter("iodemo\\student.txt"));
//建立集合對象
ArrayList<Student> arr = new ArrayList<>();
//建立學生對象
Student s1 = new Student("11111", "張三", 18);
Student s2 = new Student("22222", "李四", 19);
Student s3 = new Student("33333", "王五", 20);
//将學生對象添加到集合
arr.add(s1);
arr.add(s2);
arr.add(s3);
//周遊集合 得到的是學生對象
for (Student st : arr) {
StringBuilder sb = new StringBuilder();
sb.append(st.getSid()).append(",").append(st.getName()).append(",").append(st.getAge());
//寫資料
bw.write(sb.toString());
//換行
bw.newLine();
//重新整理
bw.flush();
}
//釋放資源
bw.close();
}
}
public class Student {
private String sid;
private String name;
private int age;
public Student() {
}
public Student(String sid, String name, int age) {
this.sid = sid;
this.name = name;
this.age = age;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
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;
}
}
5. 檔案到集合(第2版)
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
//檔案到集合 第2版
/*
1.定義學生類
2.建立ArrayList集合對象
3.建立字元緩沖輸入流對象
4.讀取資料 一次讀一行
5.使用split();方法對讀取到的字元串進行切片
6.建立學生對象并set值
7.将學生對象添加到集合
8.釋放資源
9.周遊集合
*/
public class Test02 {
public static void main(String[] args) throws IOException {
//建立集合對象
ArrayList<Student> arr = new ArrayList<>();
//建立字元緩沖輸入流對象
BufferedReader br = new BufferedReader(new FileReader("iodemo\\student.txt"));
//讀資料
String line;
while ((line = br.readLine()) != null){
//split();方法 切片
String[] strarr = line.split(",");
//建立學生對象
Student s = new Student();
s.setSid(strarr[0]);
s.setName(strarr[1]);
s.setAge(Integer.parseInt(strarr[2]));
//将學生對象添加到集合
arr.add(s);
}
//釋放資源
br.close();
//周遊集合
for (Student s : arr) {
System.out.println(s.getSid() + "," + s.getName() + "," + s.getAge());
}
}
}
public class Student {
private String sid;
private String name;
private int age;
public Student() {
}
public Student(String sid, String name, int age) {
this.sid = sid;
this.name = name;
this.age = age;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
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;
}
}
6. 集合到檔案 (資料排序版)
- 采用鍵盤錄入資訊
- 寫入檔案的資料按要求排序
- 測試類
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;
//集合到檔案資料排序 (第3版)
/*
1. 定義學生類
2. 建立TreeSet集合,通過比較器排序進行排序
3. 鍵盤錄入學生資料
4. 建立學生對象,把鍵盤錄入的資料對應指派給學生對象的成員變量
5. 把學生對象添加到TreeSet集合
6. 建立字元緩沖輸出流對象
7. 周遊集合,得到每一個學生對象
8. 把學生對象的資料拼接成指定格式的字元串
9. 調用字元緩沖輸出流對象的方法寫資料
10. 釋放資源
*/
public class Test {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("iodemo\\demo.txt"));
TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s1.getsum()-s2.getsum();
int num1 = num == 0 ? s1.getMath()- s2.getMath(): num;
int num2 = num1 == 0 ? s1.getChinese() - s2.getChinese(): num1;
int num3 = num2 == 0 ? s2.getName().compareTo(s1.getName()) : num1;
return num3;
}
});
for (int i = 0; i < 3; i++) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入姓名");
String name = sc.nextLine();
System.out.println("請輸入數學成績");
int math = sc.nextInt();
System.out.println("請輸入國文成績");
int chinese = sc.nextInt();
System.out.println("請輸入英語成績");
int english = sc.nextInt();
Student s = new Student();
s.setName(name);
s.setMath(math);
s.setChinese(chinese);
s.setEnglish(english);
ts.add(s);
}
for (Student t : ts) {
StringBuilder sb = new StringBuilder();
sb.append(t.getName()).append(",").append(t.getMath()).append(",").append(t.getChinese()).append(",").append(t.getEnglish()).append(",").append(t.getsum());
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
bw.close();
}
}
public class Student {
private String name;
private int math;
private int chinese;
private int english;
public Student() {
}
public Student(String name, int math, int chinese, int english) {
this.name = name;
this.math = math;
this.chinese = chinese;
this.english = english;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getsum(){
int sum = this.math + this.chinese + this.english;
return sum;
}
}
7. 複制單級檔案夾
- 複制單級檔案夾到指定目錄下
- 複制這個檔案夾包括檔案夾裡的檔案
import java.io.*;
//複制單級檔案夾
/*
1. 建立資料源目錄File對象,路徑是E:\Java_test\java
2. 擷取資料源目錄File對象的名稱
3. 建立目的地目錄File對象,路徑由(子產品名+第2步擷取的名稱)組成
4. 判斷第3步建立的File是否存在,如果不存在,就建立
5. 擷取資料源目錄下所有檔案的File數組
6. 周遊File數組,得到每一個File對象,該File對象,其實就是資料源檔案
7. 擷取資料源檔案File對象的名稱
8. 建立目的地檔案File對象,路徑由(目的地目錄+第7步擷取的名稱)組成
9. 複制檔案
由于不清楚資料源目錄下的檔案都是什麼類型的,是以采用位元組流複制檔案
采用參數為File的構造方法
*/
public class test05 {
public static void main(String[] args) throws IOException{
//建立File對象
File srcFolder = new File("E:\\Java_test\\java");
//擷取資料源目錄名稱
String srcFolderName = srcFolder.getName();
//建立目的地目錄File對象
File destFolder = new File("E:\\java_test_copy",srcFolderName);
//判斷建立的File是否存在,如果不存在,就建立
if (!destFolder.exists()){
destFolder.mkdir();
}
//擷取資料源目錄下所有檔案的File數組
File[] listFiles = srcFolder.listFiles();
//周遊File數組,得到每一個File對象,該File對象,其實就是資料源檔案
for (File listFile : listFiles) {
//擷取資料源檔案File對象的名稱
String listFileName = listFile.getName();
//建立目的地檔案File對象,路徑由(目的地目錄+擷取的名稱)組成
File destFile = new File(destFolder,listFileName);
//調用複制方法
copy(listFile,destFile);
}
}
//複制方法
private static void copy(File listFile, File destFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(listFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
//一次讀寫一個位元組數組
byte[] bys = new byte[1024];
int len;
while ((len = bis.read(bys))!= -1){
bos.write(bys,0,len);
}
//釋放資源
bis.close();
bos.close();
}
}
8. 複制多級檔案夾
- 複制多級檔案夾到指定目錄下
- 複制檔案夾以及檔案夾裡的内容,檔案夾裡可能包含檔案夾
- 采用遞歸複制多級檔案夾
import java.io.*;
//複制多級檔案
/*
1. 建立資料源File對象,路徑是E:\\Java_test
2. 建立目的地File對象,路徑是E:\\java_test_copy
3. 寫方法實作檔案夾的複制,參數為資料源File對象和目的地File對象
4. 判斷資料源File是否是檔案
是檔案:直接複制,用位元組流
不是檔案:
在目的地下建立該目錄
周遊擷取該目錄下的所有檔案的File數組,得到每一個File對象
回到3繼續(遞歸)
*/
public class Test02 {
public static void main(String[] args) throws IOException{
File srcFile = new File("E:\\Java_test");
File destFile = new File("E:\\java_test_copy");
copyFolder(srcFile,destFile);
}
//複制檔案夾
private static void copyFolder(File srcFile,File destFile) throws IOException {
//判斷是否為檔案夾
if (srcFile.isDirectory()){
//擷取資料源目錄名稱
String srcFileName = srcFile.getName();
File newFolder = new File(destFile,srcFileName);
//判斷目錄是否存在
if (!newFolder.exists()){
newFolder.mkdir();
}
File[] fileArray = srcFile.listFiles();
for (File file : fileArray) {
//采用遞歸調用
copyFolder(file,newFolder);
}
}else {
File newFile = new File(destFile,srcFile.getName());
//調用複制檔案方法
copyFile(srcFile,newFile);
}
}
//複制檔案
private static void copyFile(File srcFile, File newFile) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
byte[] bys = new byte[1024];
int len;
while ((len = bis.read(bys))!=-1){
bos.write(bys,0,len);
}
bos.close();
bis.close();
}
}