[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 ...
随机推荐
- 记一次treegrid checkbox 选择问题
最后选择 select 方法来实现 checkRow checked 属性也不行 getChecked 方法并不能取到这两种方法的行数据
- [No000011E]Python教程1/9-Python简介
Python是一种计算机程序设计语言.你可能已经听说过很多种流行的编程语言,比如非常难学的C语言,非常流行的Java语言,适合初学者的Basic语言,适合网页编程的JavaScript语言等等. 那P ...
- [No0000B5]C# 类型基础 值类型和引用类型 及其 对象判等 深入研究1
引言 本文之初的目的是讲述设计模式中的 Prototype(原型)模式,但是如果想较清楚地弄明白这个模式,需要了解对象克隆(Object Clone),Clone其实也就是对象复制.复制又分为了浅度复 ...
- Django:环境搭建
django环境配置 1.安装django pip install django #安装指定版本 pip install -v django==1.8.2 通过python shell查看版本,返回版 ...
- shell 文件描述符
/tmp/test.sh > /tmp/test.log 2>&1 这个命令的意思是 前半部分是将shell的输出重定向到/tmp/test/log.默认是标准输出(stdout文 ...
- [GRE] GRE协议介绍
写的一般,主要看下图就行了. https://blog.csdn.net/Mary19920410/article/details/72303641 前半部分介绍还不错,后半部分没看. http:// ...
- python实现单链表反转(经典笔试题)
https://blog.csdn.net/su_bao/article/details/81072849 0.说在前面的话 链表结构,说难不难,说易不易,一定要亲自编程实现一下.其次就是一定要耐心, ...
- 转:手把手教你如何玩转Solr(包含项目实战)
原文地址:手把手教你如何玩转Solr(包含项目实战) 参考原文
- HTML5上传文件显示进度
下面我们使用Html 5的新特性file api实现上传文件,并显示上传文件进度百分比.意图是这样的,当选择文件时,显示当前文件信息.这里我们是结合Asp.net MVC做为服务端,您也可以是其它的服 ...
- Bootstrap3隐藏滑动侧边栏菜单代码特效
链接:https://pan.baidu.com/s/1syV3ZFg-RqfCv0HS5K0vug 提取码:yjex