[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();
[Java in NetBeans] Lesson 17. File Input/Output.的更多相关文章
- [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 ...
- [Java in NetBeans] Lesson 16. Exceptions.
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: We want to handle the bad Error. (e.g bad input / bugs in program) ...
- [Java in NetBeans] Lesson 07. JavaDoc and Unit Tests
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: 1. organize code into packages Create a package if want to make th ...
- [Java in NetBeans] Lesson 04. Class / Objects
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Class: Blueprint for an object. (e.g. dog is a class) Object: cust ...
- [Java in NetBeans] Lesson 01. Java Programming Basics
这个课程的参考视频在youtube. 主要学到的知识点有: Create new project, choose Java Application. one .jar file/ package(.j ...
- [Java in NetBeans] Lesson 15. Sorting and Searching.
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Build in functions in java.util.Collections Need to implement a co ...
- [Java in NetBeans] Lesson 09. Switch / If-Else Ladder
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: 1. Nested If-else statement (if-else ladder) /** * 90 and above == ...
- [Java in NetBeans] Lesson 08. If: conditional statement
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: 1. If-else statement if (x > 5) { System.out.println("Inpu ...
- [Java in NetBeans] Lesson 06. Custom classes
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Constructors: A special method called when an object of the class ...
随机推荐
- MyISAM和InnoDB区别 及选择
MySQL默认采用的是MyISAM. MyISAM不支持事务,而InnoDB支持.InnoDB的AUTOCOMMIT默认是打开的,即每条SQL语句会默认被封装成一个事务,自动提交,这样会影响速度,所以 ...
- python 中 try ...except
捕捉异常 try: 下的代码段 即为 需要捕捉异常的代码段: except: 捕获某一模块的异常,须带异常模块名称,可带原因参数:except 下代码为该异常发生时,所执行的代码:一个try可对应多 ...
- Fis3构建迁移Webpack之路
Webpack从2015年9月第一个版本横空初始至今已逾2载.它的出现,颠覆了一大批主流构建如Ant.Grunt和Gulp等等.腾讯NOW直播IVWEB团队之前一直采用Fis构建,本篇文章主要介绍从F ...
- 宝宝刷 leetcode
12/3 1.Two Sum Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, retur ...
- cc2650-cc2640蓝牙开发准备手记
1.安装 ti关键库,首先BLE 协议栈,安装那种协议栈,首先你要用哪种库(源代码,官方例子基于哪种用哪个就会好,不然会出现不兼容), 2.然后安装flash_programmer2(有1,.2个版 ...
- jdbc--------JdbcUtilDao 类
2018-12-14 目标:做成一个比较通用的 sql 操作 import com.ljs.util.JDBCUtil; 类名:JdbcUtilDao 1: 更新操作, 针对任何表,增加,删除,更新操 ...
- shell监控之列出1小时内cpu占用最多的10个进程
脚本内容如下: -------------------------------------------------------------------------------------------- ...
- LeetCode 897 Increasing Order Search Tree 解题报告
题目要求 Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the r ...
- Java之旅_高级教程_实例_打印图形
1.打印菱形 public class MainClass{ public static void main(String[] args){ printStar(10); } public stati ...
- mysql报错Establishing SSL connection without server's identity verification is not recommended
使用mysql数据库时报错:Establishing SSL connection without server's identity verification is not recommended ...