这个课程的参考视频和图片来自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. Arm v8 中断处理

    转 https://blog.csdn.net/firefox_1980/article/details/40113637

  2. html 常用标签 a form input 标签 等等等

    前端HTML   HTML介绍 Web服务本质 import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) sk ...

  3. 查看线程的进程id

    import os from threading import Thread def f1(n): print(n) print('%d号线程的id是%s'%(n,os.getpid())) if _ ...

  4. MQTT 单片机端讲解

    有空了和大家分享一下,如何从头架构一个高效mqtt并行客户端,基于传统GPRS等较差网络环境和网关等网络环境好的情景(当然仔细讲解mqtt的基本函数使很有必要的).---这会正忙着搬砖 MQTt协议 ...

  5. 从一条巨慢SQL看基于Oracle的SQL优化(重磅彩蛋+PPT)

    本文根据DBAplus社群第110期线上分享整理而成,文末还有好书送哦~ 讲师介绍 丁俊 新炬网络首席性能优化专家 SQL审核产品经理 DBAplus社群联合发起人.<剑破冰山-Oracle开发 ...

  6. Lombok安装及使用

    为什么要使用Lombok: 虽然一般的IDE(eclipse,Intellij)都有自动生成代码的功能,如:生成setter,getter,toString,equels,hashcode等.但是如果 ...

  7. Will vs Be Going To vs Present Continuous: Talk About the Future in English

    https://www.youtube.com/watch?v=UISiuiPd_FY will 说话的当下决定的将来要做什么,in the moment be going to 有意图去做,但没有计 ...

  8. sed中支持变量的处理方法

    1.eval sed ’s/$a/$b/’ filename2.sed "s/$a/$b/" filename3.sed ’s/’$a’/’$b’/’ filename 4.sed ...

  9. android&sqlsever

    http://blog.csdn.net/chaoyu168/article/details/51910601

  10. python 遍历list并删除部分元素

    python 遍历list并删除部分元素https://blog.csdn.net/afgasdg/article/details/82844403有两个list,list_1 为0-9,list_2 ...