Java 基礎(chǔ) 45 字符流相關(guān)練習(xí)

1.1 字符流的練習(xí)之5種方式復(fù)制文本文件

1.1.1 字符流復(fù)制文本文件的五種方式

  • 基本字符流一次讀寫一個(gè)字符
  • 基本字符流一次讀寫一個(gè)字符數(shù)組
  • 緩沖字符流一次讀寫一個(gè)字符
  • 緩沖字符流一次讀寫一個(gè)字符數(shù)組
  • 緩沖字符串一次讀寫一個(gè)字符串

1.1.2 案例代碼

public class CopyFileTest {
    public static void main(String[] args) throws IOException {
        method1();
        // method2();
        // method3();
        // method4();
        // method5();
    }
    
    //緩沖字符流一次讀寫一個(gè)字符串
    private static void method5() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("d:\\林青霞.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("窗里窗外.txt"));
        
        String line;
        while((line=br.readLine())!=null) {
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        
        bw.close();
        br.close();
    }
    
    //緩沖字符流一次讀寫一個(gè)字符數(shù)組
    private static void method4() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("d:\\林青霞.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("窗里窗外.txt"));
        
        char[] chs = new char[1024];
        int len;
        while((len=br.read(chs))!=-1) {
            bw.write(chs, 0, len);
        }
        
        bw.close();
        br.close();
    }
    
    //緩沖字符流一次讀寫一個(gè)字符
    private static void method3() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("d:\\林青霞.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("窗里窗外.txt"));
        
        int ch;
        while((ch=br.read())!=-1) {
            bw.write(ch);
        }
        
        bw.close();
        br.close();
    }
    
    // 基本字符流一次讀寫一個(gè)字符數(shù)組
    private static void method2() throws IOException {
        FileReader fr = new FileReader("d:\\林青霞.txt");
        FileWriter fw = new FileWriter("窗里窗外.txt");

        char[] chs = new char[1024];
        int len;
        while((len=fr.read(chs))!=-1) {
            fw.write(chs, 0, len);
        }

        fw.close();
        fr.close();
    }

    // 基本字符流一次讀寫一個(gè)字符
    private static void method1() throws IOException {
        FileReader fr = new FileReader("d:\\林青霞.txt");
        FileWriter fw = new FileWriter("窗里窗外.txt");

        int ch;
        while ((ch = fr.read()) != -1) {
            fw.write(ch);
        }

        fw.close();
        fr.close();
    }
}

1.2 字符流的練習(xí)之把集合中的字符串?dāng)?shù)據(jù)存儲(chǔ)到文本文件

1.2.1 案例分析

  • 創(chuàng)建集合對象
  • 往集合中添加字符串元素
  • 創(chuàng)建字符緩沖輸出流對象
  • 遍歷集合,得到每一個(gè)字符串元素,把字符串元素作為數(shù)據(jù)寫入到文本文件
  • 釋放資源

1.2.2 案例代碼


public class ArrayListToFileTest {
    public static void main(String[] args) throws IOException {
        //創(chuàng)建集合對象
        ArrayList<String> array = new ArrayList<String>();
        
        //往集合中添加字符串元素
        array.add("hello");
        array.add("world");
        array.add("java");
        
        //創(chuàng)建字符緩沖輸出流對象
        BufferedWriter bw = new BufferedWriter(new FileWriter("array.txt"));
        
        //遍歷集合,得到每一個(gè)字符串元素,把字符串元素作為數(shù)據(jù)寫入到文本文件
        for(String s : array) {
            bw.write(s);
            bw.newLine();
            bw.flush();
        }
        
        //釋放資源
        bw.close();
    }
}

1.3 字符流的練習(xí)之把文本文件中的字符串?dāng)?shù)據(jù)讀取到集合

1.3.1 案例分析

需求:

??從文本文件中讀取數(shù)據(jù)到ArrayList集合中,并遍歷集合
每一行數(shù)據(jù)作為一個(gè)字符串元素

分析:

  • 創(chuàng)建字符緩沖輸入流對象
  • 創(chuàng)建集合對象
  • 讀取數(shù)據(jù),每一次讀取一行,并把該數(shù)據(jù)作為元素存儲(chǔ)到集合中
  • 釋放資源
  • 遍歷集合

1.3.2 案例代碼

public class FileToArrayListTest {
    public static void main(String[] args) throws IOException {
        //創(chuàng)建字符緩沖輸入流對象
        BufferedReader br = new BufferedReader(new FileReader("array.txt"));
        
        //創(chuàng)建集合對象
        ArrayList<String> array = new ArrayList<String>();
        
        //讀取數(shù)據(jù),每一次讀取一行,并把該數(shù)據(jù)作為元素存儲(chǔ)到集合中
        String line;
        while((line=br.readLine())!=null) {
            array.add(line);
        }
        
        //釋放資源
        br.close();
        
        //遍歷集合
        for(String s : array) {
            System.out.println(s);
        }
    }
}

1.4 字符流的練習(xí)之把集合中的學(xué)生對象數(shù)據(jù)存儲(chǔ)到文本文件

1.4.1案例分析

需求:

  • 把ArrayList集合中的學(xué)生數(shù)據(jù)存儲(chǔ)到文本文件
  • 每一個(gè)學(xué)生數(shù)據(jù)作為文件中的一行數(shù)據(jù)
學(xué)號 姓名 年齡 所在城市
it001 張曼玉 35 北京
it002 王祖賢 33 上海
it003 林青霞 30 西安

定義一個(gè)學(xué)生類

學(xué)號 姓名 年齡 所在城市
it001 張曼玉 35 北京
it002 王祖賢 33 上海
it003 林青霞 30 西安

分析:

  • 創(chuàng)建集合對象
  • 創(chuàng)建學(xué)生對象
  • 把學(xué)生對象添加到集合中
  • 創(chuàng)建字符緩沖輸出流對象
  • 遍歷集合,得到每一個(gè)學(xué)生對象,然后把該對象的數(shù)據(jù)拼接成一個(gè)指定格式的字符串寫到文本文件
  • 釋放資源

1.4.2 案例代碼

public class ArrayListToFileTest {
    public static void main(String[] args) throws IOException {
        // 創(chuàng)建集合對象
        ArrayList<Student> array = new ArrayList<Student>();

        // 創(chuàng)建學(xué)生對象
        Student s1 = new Student("it001", "張曼玉", 35, "北京");
        Student s2 = new Student("it002", "王祖賢", 33, "上海");
        Student s3 = new Student("it003", "林青霞", 30, "西安");

        // 把學(xué)生對象添加到集合中
        array.add(s1);
        array.add(s2);
        array.add(s3);

        // 創(chuàng)建字符緩沖輸出流對象
        BufferedWriter bw = new BufferedWriter(new FileWriter("students.txt"));

        // 遍歷集合,得到每一個(gè)學(xué)生對象,然后把該對象的數(shù)據(jù)拼接成一個(gè)指定格式的字符串寫到文本文件
        for (Student s : array) {
            // it001,張曼玉,35,北京
            StringBuilder sb = new StringBuilder();
            sb.append(s.getSid()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",")
                    .append(s.getCity());
            bw.write(sb.toString());
            bw.newLine();
            bw.flush();
        }
        
        //釋放資源
        bw.close();
    }
}

1.5 字符流的練習(xí)之把文本文件中的學(xué)生對象數(shù)據(jù)讀取到集合

1.5.1 案例分析

需求:
- 從文本文件中讀取學(xué)生數(shù)據(jù)到ArrayList集合中,并遍歷集合
- 每一行數(shù)據(jù)作為一個(gè)學(xué)生元素

    it001,張曼玉,35,北京

這里我們要使用String類中的一個(gè)方法:split()

分析:

  • 創(chuàng)建字符緩沖輸入流對象
  • 創(chuàng)建集合對象
  • 讀取數(shù)據(jù),每一次讀取一行數(shù)據(jù),把該行數(shù)據(jù)想辦法封裝成學(xué)生對象,并把學(xué)生對象存儲(chǔ)到集合中
  • 釋放資源
  • 遍歷集合

1.5.2 案例代碼

public class FileToArrayListTest {
    public static void main(String[] args) throws IOException {
        // 創(chuàng)建字符緩沖輸入流對象
        BufferedReader br = new BufferedReader(new FileReader("students.txt"));

        // 創(chuàng)建集合對象
        ArrayList<Student> array = new ArrayList<Student>();

        // 讀取數(shù)據(jù),每一次讀取一行數(shù)據(jù),把該行數(shù)據(jù)想辦法封裝成學(xué)生對象,并把學(xué)生對象存儲(chǔ)到集合中
        String line;
        while ((line = br.readLine()) != null) {
            // it001,張曼玉,35,北京
            String[] strArray = line.split(",");

            Student s = new Student();
            s.setSid(strArray[0]);
            s.setName(strArray[1]);
            s.setAge(Integer.parseInt(strArray[2]));
            s.setCity(strArray[3]);

            array.add(s);
        }

        // 釋放資源
        br.close();

        // 遍歷集合
        for (Student s : array) {
            System.out.println(s.getSid() + "---" + s.getName() + "---" + s.getAge() + "---" + s.getCity());
        }
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容