[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 ...
随机推荐
- windows 开启热点的命令行工具
hotspot.bat @echo off @echo. setlocal enabledelayedexpansion if "%1" == "set" ( ...
- ERP项目实施记录01
工厂8月中与某ERP服务商签约,至今已经过去4个月,顾问服务了13人天,进行了10次培训. 内部产生项目文档1个:物料编码方案 制度:0个 流程:无 因ERP服务商不是针对本行业的,在BOM和生产计划 ...
- Oracle的字符连接函数 concat 和 || 的区别
总结:concat 只能连接两个字符串,|| 可以连接多个
- WPF .NET 4.0 OpenClipboard 失败 (异常来自 HRESULT:0x800401D0 (CLIPBRD_E_CANT_OPEN)) BUG解决
在项目 App.xaml 文件下 => Application 节点=> 添加 DispatcherUnhandledException="Application_Dispa ...
- html学习_认识html
1.HTML骨架 <html>----根标签 <head>---头标签 </head> <body>---主体标签 </body> < ...
- vm tools安装linux ubuntu和主机不能复制
点击图中的安装vm tools ,因为我的已经安装过了,所以显示的是重新安装. 点击以后会出来一个虚拟光驱,打开虚拟光驱,复制出来 vm toolsxxx.tar.gz 文件建立一个临时文件夹,复制 ...
- tensorflow使用pb文件进行模型预测
- SQL之层次查询
层次查询是一种确定数据行间关系的一种操作手段.层次查询遍历的是一个树形结构.基本语法如下,以下语法嵌入到标准SQL中即可达到层次查询的目的: level,... ...[注释:伪列,用于select子 ...
- Installing Precise (12.04.2) using netboot onto a Marvell ArmadaXP Development Board
https://wiki.ubuntu.com/ARM/Server/Install https://wiki.ubuntu.com/ARM/Server/Install/ArmadaXP Arm ...
- [https][ssl] keyless SSL
HTTP Server 集群前的负载设备,或内容审计设备等,在处理https的时候,需要用户配置提供证书. 但是考虑到安全问题,HTTP Server并不愿意把证书配置到其他设备上. 这个时候,就有个 ...