[Java in NetBeans] Lesson 17. File Input/Output.
生活随笔
收集整理的這篇文章主要介紹了
[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.?
?
3.?Scanner()?: Read from a file.?
- Read string from the file.?
?
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)
- 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.的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 爬取桌面网
- 下一篇: BurpSuite学习第七节--Sequ