日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

缓冲流、转换流、序列化流代码练习

發布時間:2025/3/20 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 缓冲流、转换流、序列化流代码练习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

一、高效字節輸出流寫出字節數據

二、高效字節輸出流寫出字節數組數據

三、高效字符流和集合的綜合使用

四、轉換輸出流的使用

五、轉換輸入流的使用

七、高效字符流讀寫數據

八、對象的序列化,對象輸出流的使用


一、高效字節輸出流寫出字節數據

利用高效字節輸出流往C盤下的d.txt文件輸出一個字節數。

public static void main(String[] args) throws IOException {BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day10_緩沖流、轉換流\\a.txt"));bos.write(97);bos.close();}

二、高效字節輸出流寫出字節數組數據

利用高效字節輸出流往C盤下的e.txt文件寫出一個字節數組數據,如寫出:”i love java”。

public static void main(String[] args) throws IOException {BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day10_緩沖流、轉換流\\c.txt"));String s = "i love you";bos.write(s.getBytes());bos.close();}

三、高效字符流和集合的綜合使用

分析以下需求,并用代碼實現
?? ?實現一個驗證碼小程序,要求如下:
?? ?1. 在項目根目錄下新建一個文件:data.txt,鍵盤錄入3個字符串驗證碼,
?? ? ? ?并存入data.txt中,要求一個驗證碼占一行;
?? ?2. 鍵盤錄入一個需要被校驗的驗證碼,如果輸入的驗證碼在data.txt中存在:
?? ? ? ?在控制臺提示驗證成功,如果不存在控制臺提示驗證失敗。

public static void main(String[] args) throws IOException {File file = new File("day10_緩沖流、轉換流\\data.txt");//createVerify(file);boolean login = login();if (login) {System.out.println("登陸成功");}else{System.out.println("驗證失敗");}}public static void createVerify(File file) throws IOException {Scanner sc = new Scanner(System.in);System.out.println("請輸入要保存字符串驗證碼");String verify = sc.next();BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));bos.write(verify.getBytes());bos.close();}public static boolean login() throws IOException {Scanner sc = new Scanner(System.in);System.out.println("請輸入驗證碼:");String verify = sc.next();BufferedReader br = new BufferedReader(new FileReader("day10_緩沖流、轉換流\\data.txt"));String s = br.readLine();br.close();return s.equals(verify);}

四、轉換輸出流的使用

描述:現有一字符串:”我愛Java”。將該字符串保存到當前項目根目錄下的text05.txt文件中。
要求:使用gbk編碼保存。

public static void main(String[] args) throws Exception {String s = "我愛Java";OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("day10_緩沖流、轉換流\\test05.txt"), "GBK");osw.write(new String(s.getBytes(), "GBK").toCharArray());osw.close();}

五、轉換輸入流的使用

描述:利用轉換輸入流將當前項目根目錄下使用gbk編碼的text05.txt文件的內容讀取出來,并打印在控制臺上。
要求:不能出現亂碼的情況。

public static void main(String[] args) throws Exception {InputStreamReader isr = new InputStreamReader(new FileInputStream("day10_緩沖流、轉換流\\test06.txt"), "GBK");char[] chs = new char[1024];int len = 0;while ((len = isr.read(chs)) != -1) {System.out.println(chs);}isr.close();}

六、描述:從鍵盤錄入一行字符串,利用字節打印流將該行字符串保存到當前項目根目錄下的d.txt文件中。

public static void main(String[] args) throws FileNotFoundException {Scanner sc = new Scanner(System.in);System.out.println("請輸入一行字符串");PrintStream ps = new PrintStream("day10_緩沖流、轉換流\\d.txt");ps.println(sc.next());}

七、高效字符流讀寫數據

利用IO流的知識讀取text.txt文件的內容反轉后寫入text1.txt文件中,內容如下:
? ? ? ?123456
? ? ? 我愛java

public static void main(String[] args) throws Exception {BufferedReader br = new BufferedReader(new FileReader("day10_緩沖流、轉換流\\text.txt"));ArrayList<String> list = new ArrayList<>();char[] bys = new char[1024];String line = null;while ((line = br.readLine()) != null) {list.add(line);}Collections.reverse(list);BufferedWriter bw = new BufferedWriter(new FileWriter("day10_緩沖流、轉換流\\text1.txt"));for (String s : list) {bw.write(s);bw.newLine();}bw.close();br.close();}

八、對象的序列化,對象輸出流的使用

描述:
定義一個學生類,成員變量有姓名,年齡,性別,提供setters和getters方法以及構造方法
定義一個測試類,在測試類創建多個學生對象保存到集合中,然后將集合存儲到當前項目根目錄下的stus.txt文件中。

public class Student implements Serializable {private static final long serialVersionUID = 6226316605901205593L;private String name;private int age;private String sex;public Student() {}public Student(String name, int age, String sex) {this.name = name;this.age = age;this.sex = sex;}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 getSex() {return sex;}public void setSex(String sex) {this.sex = sex;} } public static void main(String[] args) throws Exception {//創建集合用來保存Student對象List<Student> list = new ArrayList<>();list.add(new Student("張三", 20, "男"));list.add(new Student("東方不敗", 18, "女"));list.add(new Student("小龍女", 16, "女"));//創建序列化流ObjectOutputStream obs = new ObjectOutputStream(new FileOutputStream("stus.txt"));//寫入list對象obs.writeObject(list);//關閉序列化流obs.close();//創建反序列化流讀取list對象ObjectInputStream ois = new ObjectInputStream(new FileInputStream("stus.txt"));//遍歷Student對象List<Student> list1 = (List) ois.readObject();for (Student s : list) {System.out.println(s.getName() + "---" + s.getAge() + "---" + s.getSex());}//關閉反序列化流ois.close();}

?

總結

以上是生活随笔為你收集整理的缓冲流、转换流、序列化流代码练习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。