[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();

原文地址:https://www.cnblogs.com/Johnsonxiong/p/10204160.html

时间: 2024-08-01 19:45:52

[Java in NetBeans] Lesson 17. File Input/Output.的相关文章

[Java in NetBeans] Lesson 01. Java Programming Basics

这个课程的参考视频在youtube. 主要学到的知识点有: Create new project, choose Java Application. one .jar file/ package(.jar name with the same as package), one package can contains mutiple .java files. Comment mutiple lines by using "/* */"; comment one line by usin

[Java in NetBeans] Lesson 15. Sorting and Searching.

这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Build in functions in java.util.Collections Need to implement a comparator - a special class which returns an integer comparision of two object, if  compare(a,b), if return negative number, a will be before b, otherw

[Java in NetBeans] Lesson 03. More Variables / Type Casting

这个课程的参考视频在youtube. 主要学到的知识点有: It is different from python, that "1" only present string "1", and '1' only presents char '1'. (type) can chang the type , e.g. (int) (totalScore/4.5); will change the result of(totoalScore/4.5) which is a

JAVA学习分享Input Output

IO(Input  Output)流 IO流用来处理设备之间的数据传输 Java对数据的操作是通过流的方式 Java用于操作流的对象都在IO包中 流按操作数据分为两种:字节流与字符流. 流按流向分为:输入流,输出流. IO流常用基类 字节流的抽象基类: InputStream ,OutputStream. 字符流的抽象基类: Reader , Writer. 注:由这四个类派生出来的子类名称都是以其父类名作为子类名的后缀. (InputStream的子类FileInputStream.) (Re

Reading and writing files in Java (Input/Output) - Tutorial

Java Input Output This tutorial explains how to read and write files via Java. Table of Contents 1. Java I/O (Input / Output) for files 1.1. Overview 1.2. Reading a file in Java 1.3. Writing a file in Java 1.4. How to identify the current directory 2

黑马程序员——java基础---IO(input output)流字符流

黑马程序员——java基础---IO(input output)流字符流 ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- io(input output)流特点: 1,io流用来处理数据之间的传输 2,java对数据的操作是通过流的方式: 3,java用于操作流的对象都在io包中: 4,流按操作数据分为两种:字节流和字符流: 5,流按流向分为:输入流和输出流. 注意:流只能操作数据,而不能操作文件. 3.IO流的常用基类: 1)字节流的抽象

java IO流技术 之 File

IO流技术 概念:input - output 输入输出流: 输入:将文件读到内存中: 输出:将文件从内存中写出到其他地方 作用:主要就是解决设备和设备之间的数据传输问题. File :文件类的使用十分重要 (一)file的构造方法 1 public static void test1(){ 2 3 //三种拼接路径方式 4 //1.直接拼接 5 File filePath = new File("C:\\123\\aaa"); 6 System.out.println("是

数据库服务器Input/output error (故障20170122)

描述: 数据库系统出现告警,登陆查看使用系统命令报错,提示Input/output error 模拟故障处理过程: 检查文件系统时,该文件系统必须卸载.当出现错误时fsck会提示是否修复, 可以用-y参数:不提示是否修复 fsck - check and repair a Linux file system # df -h Filesystem            Size  Used Avail Use% Mounted on /dev/mapper/vg_mysql80-lv_root 2

io流(input output)

IO流(数据流) 用来处理设备之间的数据传输,Java对数据的操作(硬盘上的文件,内存中的数据)是通过流的方式Java用于操作流的对象都在IO包中 流按流向分为:输入流,输出流. 流按操作数据分为两种:字节流与字符流 通用:字节流(数据(mp3,mp4等媒体视频音频,图片)都是字节,也就是二进制 ASCII外国人弄的识别英文字母) 我们国家也弄了一个对应中文的表:GBK2312表 国际标准(UNICODE码表(无论什么字符都用16个字节表示), 再进行优化后UTF-8码表(两个相同字母时里面的编