日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

[Java in NetBeans] Lesson 17. File Input/Output.

發布時間:2025/4/14 java 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Java in NetBeans] Lesson 17. File Input/Output. 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這個課程的參考視頻和圖片來自youtube。

? ? 主要學到的知識點有:

We want to handle the bad Error. (e.g bad input / bugs in program)

1. File() : A Java representation of a file.

File file = new File("test.txt");

?

2.?PrintWriter()?: Write to a file.?

  • Write string into the file.?
// Write 2 lines(Johnson, 26) into the test.txt file. (If not exist then create one, otherwise will overwrite it.) PrintWriter output = new PrintWriter(file); output.println("Johnson"); output.println("26"); output.close();

?

3.?Scanner()?: Read from a file.?

  • Read string from the file.?
Scanner input = new Scanner(file); String name = input.nextLine(); int age = input.nextLine();

?

4. Object Serialization : convert an object into a series of bytes so they can be written to disk?

  • Serialization: Object to Disk
  • Deserialization: Disk to Object
  • In order to use the seialization, we need to add implement Serializable in the class definition (the contens will be in a binary format)
public class Student implements Serializable {
  • FileInputStream & ObjectInputStream

  ? ? ?Read from a file as bytes and deserialize a data input stream back into an object

// deserialize the collection of students FileinputStream fi = new FileinputSream(file); ObjectInputStream input = new ObjectOutputStream(fi); while(input.hasNext()){Student s =(Student) input.readObject();students.add(s); } input.close();// Important when writing a file as operating system will deny other programs access until the file is closed fi.close();

?

  • FileOutputStream & ObjectOutputStream

  ? ? ?Write to a file as bytes and serialize an object?into?a data input stream

// serialize the collection of students FileOutputStream fo = new FileOutputSream(file); ObjectOutputStream output = new ObjectOutputStream(fo); for(Student s: students){output.writeObject(s); } output.close();// Important when writing a file as operating system will deny other programs access until the file is closed fo.close();

?

轉載于:https://www.cnblogs.com/Johnsonxiong/p/10204160.html

總結

以上是生活随笔為你收集整理的[Java in NetBeans] Lesson 17. File Input/Output.的全部內容,希望文章能夠幫你解決所遇到的問題。

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