这个课程的参考视频和图片来自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();

[Java in NetBeans] Lesson 17. File Input/Output.的更多相关文章

  1. [Java in NetBeans] Lesson 00. Getting Set-up for Learning Java

    这个课程的参考视频在youtube. 主要学到的知识点有: set up needs Java SE JDK, NetBeans IDE class name should be the same l ...

  2. [Java in NetBeans] Lesson 16. Exceptions.

    这个课程的参考视频和图片来自youtube. 主要学到的知识点有: We want to handle the bad Error. (e.g bad input / bugs in program) ...

  3. [Java in NetBeans] Lesson 07. JavaDoc and Unit Tests

    这个课程的参考视频和图片来自youtube. 主要学到的知识点有: 1. organize code into packages Create a package if want to make th ...

  4. [Java in NetBeans] Lesson 04. Class / Objects

    这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Class: Blueprint for an object. (e.g. dog is a class) Object: cust ...

  5. [Java in NetBeans] Lesson 01. Java Programming Basics

    这个课程的参考视频在youtube. 主要学到的知识点有: Create new project, choose Java Application. one .jar file/ package(.j ...

  6. [Java in NetBeans] Lesson 15. Sorting and Searching.

    这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Build in functions in java.util.Collections Need to implement a co ...

  7. [Java in NetBeans] Lesson 09. Switch / If-Else Ladder

    这个课程的参考视频和图片来自youtube. 主要学到的知识点有: 1. Nested If-else statement (if-else ladder) /** * 90 and above == ...

  8. [Java in NetBeans] Lesson 08. If: conditional statement

    这个课程的参考视频和图片来自youtube. 主要学到的知识点有: 1. If-else statement if (x > 5) { System.out.println("Inpu ...

  9. [Java in NetBeans] Lesson 06. Custom classes

    这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Constructors: A special method called when an object of the class ...

随机推荐

  1. 写写我的硕士三年【zz】

    昨天我们组的10bit-40M ADC测出来了,自己终于能松口气,可以无牵无挂的毕业了.晚上老板bg全组毕业生,喝了很多,我对老板说:"这3年在组里,我是把它当作事业来做的!"是的 ...

  2. 夺冠概率|2012年蓝桥杯B组题解析第九题-fishers

    (17')夺冠概率 足球比赛具有一定程度的偶然性,弱队也有战胜强队的可能. 假设有甲.乙.丙.丁四个球队.根据他们过去比赛的成绩,得出每个队与另一个队对阵时取胜的概率表: 甲 乙 丙 丁 甲 - 0. ...

  3. Android开发-基本概念(申明:来源于网络)

    Android开发-基本概念(申明:来源于网络) 地址:http://blog.csdn.net/iwanghang/article/details/53505926

  4. js常用校验

    //验证金钱数字obj.regexMoney = function (money) { var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)| ...

  5. content-type对照表

  6. [No000012B]WPF(3/7)有趣的边框和画刷[译]

    介绍 边框是每个WPF程序的主要构成块.在我现在的程序中,我使用了很多的边框来装饰界面.从把边框直接放到窗口中到把边框放到控件模板和列表项中,边框在创建一个好的应用界面上扮演了一个非常重要的角色.在这 ...

  7. python selenium配置

    写该博客时环境 mac 10.14.1 (18B75) python 3.7 pip (不用这个就是了,用pip3) $ pip --version pip 10.0.1 from /Users/wj ...

  8. iOS-静态库,动态库,framework浅析(二)

    创建.a静态库 第一步,新建工程.     一般使用工程名就使用库的名称,比如我这里用FMDB来创建静态库,我的工程名就取名为FMDB,创建的.a静态库就是libFMDB.a.             ...

  9. adg的数据传输应用三大模式转换

    1.最大可用性模式(Maximum Availability) 1)该模式提供了仅次于"最大保护模式"的数据保护能力: 2)要求至少一个物理备库收到重做日志后,主库的事务才能够提交 ...

  10. Qt网络模块如何使用(表格)

    1.网络模块介绍 类名 说明 中文 QAbstractNetworkCache The interface for cache implementations 缓存实现的接口 QNetworkCach ...