这个课程的参考视频和图片来自youtube

主要学到的知识点有:

We want to handle the bad Error. (e.g bad input / bugs in program)

1. File() : A Java representation of a file.

  1. File file = new File("test.txt");

2. PrintWriter() : Write to a file.

  • Write string into the file.
  1. // Write 2 lines(Johnson, 26) into the test.txt file. (If not exist then create one, otherwise will overwrite it.)
  2. PrintWriter output = new PrintWriter(file);
  3. output.println("Johnson");
  4. output.println("26");
  5. output.close();

3. Scanner() : Read from a file.

  • Read string from the file.
  1. Scanner input = new Scanner(file);
  2. String name = input.nextLine();
  3. 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)
  1. public class Student implements Serializable {
  • FileInputStream & ObjectInputStream

       Read from a file as bytes and deserialize a data input stream back into an object

  1. // deserialize the collection of students
  2. FileinputStream fi = new FileinputSream(file);
  3. ObjectInputStream input = new ObjectOutputStream(fi);
  4. while(input.hasNext()){
  5. Student s =(Student) input.readObject();
  6. students.add(s);
  7. }
  8. input.close();// Important when writing a file as operating system will deny other programs access until the file is closed
  9. fi.close();
  • FileOutputStream & ObjectOutputStream

       Write to a file as bytes and serialize an object into a data input stream

  1. // serialize the collection of students
  2. FileOutputStream fo = new FileOutputSream(file);
  3. ObjectOutputStream output = new ObjectOutputStream(fo);
  4. for(Student s: students){
  5. output.writeObject(s);
  6. }
  7. output.close();// Important when writing a file as operating system will deny other programs access until the file is closed
  8. 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. MyISAM和InnoDB区别 及选择

    MySQL默认采用的是MyISAM. MyISAM不支持事务,而InnoDB支持.InnoDB的AUTOCOMMIT默认是打开的,即每条SQL语句会默认被封装成一个事务,自动提交,这样会影响速度,所以 ...

  2. python 中 try ...except

    捕捉异常 try: 下的代码段 即为 需要捕捉异常的代码段: except:  捕获某一模块的异常,须带异常模块名称,可带原因参数:except 下代码为该异常发生时,所执行的代码:一个try可对应多 ...

  3. Fis3构建迁移Webpack之路

    Webpack从2015年9月第一个版本横空初始至今已逾2载.它的出现,颠覆了一大批主流构建如Ant.Grunt和Gulp等等.腾讯NOW直播IVWEB团队之前一直采用Fis构建,本篇文章主要介绍从F ...

  4. 宝宝刷 leetcode

    12/3 1.Two Sum Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, retur ...

  5. cc2650-cc2640蓝牙开发准备手记

    1.安装  ti关键库,首先BLE 协议栈,安装那种协议栈,首先你要用哪种库(源代码,官方例子基于哪种用哪个就会好,不然会出现不兼容), 2.然后安装flash_programmer2(有1,.2个版 ...

  6. jdbc--------JdbcUtilDao 类

    2018-12-14 目标:做成一个比较通用的 sql 操作 import com.ljs.util.JDBCUtil; 类名:JdbcUtilDao 1: 更新操作, 针对任何表,增加,删除,更新操 ...

  7. shell监控之列出1小时内cpu占用最多的10个进程

    脚本内容如下: -------------------------------------------------------------------------------------------- ...

  8. 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 ...

  9. Java之旅_高级教程_实例_打印图形

    1.打印菱形 public class MainClass{ public static void main(String[] args){ printStar(10); } public stati ...

  10. mysql报错Establishing SSL connection without server's identity verification is not recommended

    使用mysql数据库时报错:Establishing SSL connection without server's identity verification is not recommended ...