[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 ...
随机推荐
- 在PowerShell中使用Vim
1.需要去Vim官网下载并安装一个可运行于Win8系统的执行文件(ftp://ftp.vim.org/pub/vim/pc/gvim74.exe). 2.设置PowerShell环境,使能“allow ...
- IT资源关东煮第一期【来源于网络】
IT资源关东煮第一期[来源于网络] 地址:http://geek.csdn.net/news/detail/128222
- Ubuntu上pip安装uwsgi失败的原因之一(未联网)
ubuntu@ubuntu:~$ sudo pip install uwsgi 报错:The directory '/home/ubuntu/.cache/pip/http' or its paren ...
- python中的os
import sys, os print(__file__) # 绝对路径,实际是文件名 /Users/majianyu/Desktop/test/bin/bin.py print(os.path.a ...
- 关于html中input组件间空隙的去除
有空隙的时候的代码是这样的: <input type="text" name="search" title="请输入要搜索的内容" s ...
- 微信、QQ群短文本聊天语料总结
在文本分类任务中,语料的特性千差万别,我们需要找到适合模型并抓住数据的特性,最终才能得到较好的model.最近在文本类别标注任务,就是给文本打标签确定该文本的类别.这是一个很费人工的过程,需要认真仔细 ...
- 【作业】用栈模拟dfs
题意:一个迷宫,起点到终点的路径,不用递归. 题解: #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdli ...
- [network] netfilter
netfilter 是什么? netfilter.org is home to the software of the packet filtering framework inside the Li ...
- Flink – metrics V1.2
WebRuntimeMonitor .GET("/jobs/:jobid/vertices/:vertexid/metrics", handler(new JobVertexM ...
- LeetCode 852 Peak Index in a Mountain Array 解题报告
题目要求 Let's call an array A a mountain if the following properties hold: A.length >= 3 There exist ...