java语言 百分网手机站

计算机二级JAVA考试要点复习

时间:2020-10-08 13:10:48 java语言 我要投稿

计算机二级JAVA考试要点复习

  知识就是靠积累起来的,经验也是积累起来的。以下是百分网小编整理的计算机二级JAVA考试要点复习,欢迎学习!

计算机二级JAVA考试要点复习

  一、对象流

  串行化:对象通过写出描述自己状态的数值来记述自己的过程叫串行话

  对象流:能够输入输出对象的流

  将串行化的对象通过对象流写入文件或传送到其他地方

  对象流是在普通流上加了传输对象的'功能,所以构造对象流时要先构造普通文件流

  注意:只有实现了Serializable接口的类才能被串行化

  例子:

  import java.io.*;

  class Student implements Serializable{

  private String name;

  private int age;

  public Student(String name,int age){

  this.name=name;

  this.age=age;

  }

  public void greeting(){

  System.out.println("hello ,my name is "+name);

  }

  public String toString(){

  return "Student["+name+","+age+"]";

  }

  }

  public class ObjectOutTest{

  public static void main(String args[]){

  ObjectOutputStream oos=null;

  try{

  oos=new ObjectOutputStream(

  new FileOutputStream("student.txt"));

  Student s1=new Student("Jerry",24);

  Student s2=new Student("Andy",33);

  oos.writeObject(s1);

  oos.writeObject(s2);

  }catch(Exception e){

  e.printStackTrace();

  }finally{

  if(oos!=null)

  try{

  oos.close();

  }catch(Exception e){

  e.printStackTrace();

  }

  }

  }

  }

  import java.io.*;

  public class ObjectInTest{

  public static void main(String args[]){

  ObjectInputStream ois=null;

  Student s=null;

  try{

  ois=new ObjectInputStream(

  new FileInputStream("student.txt"));

  System.out.println("--------------------");

  s=(Student)ois.readObject();

  System.out.println(s);

  s.greeting();

  System.out.println("--------------------");

  s=(Student)ois.readObject();

  System.out.println(s);

  s.greeting();

  }catch(Exception e){

  e.printStackTrace();

  }finally{

  if(ois!=null)

  try{

  ois.close();

  }catch(Exception e){

  e.printStackTrace();

  }

  }

  }

  }

  二、字符流 InputStreamReader/OutputStreamWriter

  上面的几种流的单位是 byte,所以叫做字节流,写入文件的都是二进制字节,我们无法直接看,下面要学习的是字节流

  Java采用 Unicode 字符集,每个字符和汉字都采用2个字节进行编码,ASCII 码是 Unicode 编码的自集

  InputStreamReader 是 字节流 到 字符桥的桥梁 ( byte->char 读取字节然后用特定字符集编码成字符)

  OutputStreamWriter是 字符流 到 字节流的桥梁 ( char->byte )

  他们是在字节流的基础上加了桥梁作用,所以构造他们时要先构造普通文件流

  我们常用的是:

  BufferedReader 方法:readLine()

  PrintWriter 方法:println()

  例子:

  import java.io.*;

  public class PrintWriterTest{

  public static void main(String args[]){

  PrintWriter pw=null;

  try{

  pw=new PrintWriter(

  new OutputStreamWriter(

  new FileOutputStream("bufferedwriter.txt")));

  pw.println("hello world");

  }catch(Exception e){

  e.printStackTrace();

  }finally{

  if(pw!=null)

  try{

  pw.close();

  }catch(Exception e){

  e.printStackTrace();

  }

  }

  }

  }

  import java.io.*;

  public class BufferedReaderTest{

  public static void main(String args[]){

  BufferedReader br=null;

  try{

  br=new BufferedReader(

  new InputStreamReader(

  new FileInputStream("bufferedwriter.txt")));

  System.out.println(br.readLine());

  }catch(Exception e){

  e.printStackTrace();

  }finally{

  if(br!=null)

  try{

  br.close();

  }catch(Exception e){

  e.printStackTrace();

  }

  }

  }

  }

【计算机二级JAVA考试要点复习】相关文章:

1.计算机二级JAVA考试摸底试题

2.计算机二级JAVA考试模拟试题

3.二级C语言考试复习五要点

4.计算机二级考试Java试题及答案

5.计算机二级JAVA考试精选模拟试题

6.计算机二级考试Java巩固习题

7.计算机等级考试二级java精选试题

8.计算机二级考试Java选择真题

9.2018二级建造师考试复习要点