这个课程的参考视频和图片来自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();

转载于:https://www.cnblogs.com/Johnsonxiong/p/10204160.html

[Java in NetBeans] Lesson 17. File Input/Output.相关推荐

  1. docker中java应用new FileOutputStream直接报Input/output error

    为什么80%的码农都做不了架构师?>>>    一个docker容器中跑着java应用,是一个定时任务,每天拉取远端文件包存到本机,命名为:decrypt-20181020 就在这一 ...

  2. [Java in NetBeans] Lesson 01. Java Programming Basics

    这个课程的参考视频在youtube. 主要学到的知识点有: Create new project, choose Java Application. one .jar file/ package(.j ...

  3. [Java in NetBeans] Lesson 06. Custom classes

    这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Constructors: A special method called when an object of the class ...

  4. Eal:Error reading from file descriptor 33: Input/output error

    问题描述 VMWARE 虚机中,82545EM 虚拟网卡绑定 igb_uio 后,运行 dpdk 程序,dpdk 程序一直有如下报警信息: Eal:Error reading from file de ...

  5. linux-x86_64 error,Linux-x86_64 Error: 5: Input/output error的问题

    今天朋友的测试库报错, ORA-27072: File I/O error, Linux-x86_64 Error: 5: Input/output error,数据块已经不能正常启动,看到 Linu ...

  6. 【Java I/O流】File、字符集、字节流、字符流、缓冲流、数据流、对象流、序列化、try-with-resources语句

    I/O流 File File 常用方法 File 练习:搜索.删除.剪切 字符集(Character Set) 字符编码(Character Encoding) 字符编码比较 乱码 try-with- ...

  7. GIO (Gnome Input/Output)

    Table of Contents 维基百科 Features See also KIO – an analogous KDE library References External links 百度 ...

  8. glusterfs 脑裂文件 input output error

    脑裂文件问题处理 10.0.2.33 10.0.2.34 ### 宿主机 ### [root@k8s tracker]# mount.glusterfs 10.0.2.35:vol_8a47fbed9 ...

  9. Bootstrap File Input 真正 解决跨域问题

    今天一个项目需要 做文件上传 ,图方便 就使用了Bootstrap File Input   插件 问题来了 文件放置服务器和 活动服务器 是分开的 于是涉及到了 一个ajax 跨域请求问题  后端使 ...

最新文章

  1. 无需用户输入,Adobe提出自动生成高质量合成图像新方法
  2. python语言接口_Python-接口自动化(一)
  3. Eclipse中使用SVN
  4. 与火狐浏览器对应版本_你下载的是原版的火狐浏览器吗?如果不清楚,这篇文章会让你明白...
  5. c语言float判断相等,c/c++ 比较两个浮点数相等
  6. python post json 解析失败_python中json对象转换出错解决方法
  7. matlab思考,关于Matlab编程的思考(待续)
  8. idea连接svn_搭建本地虚拟(centos)svn服务器
  9. linux查看服务器mib,Linux MIB目录的打开和查看
  10. Linux:CentOS中SFTP工具的使用
  11. IT30:12年前电脑笔记,回忆杀(1)
  12. PS像素,分辨率的概念
  13. swagger文档增强工具knife4j使用详解
  14. CADD之分子对接二:简单autodockvina对接流程——redocking
  15. 从零开始了解5G 纤亿通浅谈5G
  16. PAT A1129 重载小于号快速解答
  17. 如何在shell脚本中定义数组及遍历
  18. 前端字体图标的使用(阿里、icomoon)
  19. 调试时出现:undefined Expecting 'EOF','}',',',']', got STRING以下错误的解决方法
  20. 树莓派初次开机配置教程

热门文章

  1. jQuery中wrap、wrapAll和wrapInner用法以及区别
  2. Python 库安装方法:pip安装tar.gz压缩包,pip安装whl文件
  3. STM32 RTC BKP备份数据区数据丢失问题的讨论
  4. [学习笔记]信号与系统
  5. CTFshow php特性 web139
  6. 【HDU4497 GCD and LCM】
  7. 读取位置 0x00000028 时发生访问冲突该怎么解决
  8. 第一章:1.2.3 LTI系统研究方法与本章小结
  9. 微型计算机常用的总线类型,常见总线有哪些类型
  10. C#操作NPOI插件的HSSFWorkBook,常见的那些坑