糯米文學吧

位置:首頁 > 計算機 > java語言

計算機二級JAVA考試要點複習

java語言2.54W

知識就是靠積累起來的,經驗也是積累起來的。以下是本站小編整理的計算機二級JAVA考試要點複習,歡迎學習!

計算機二級JAVA考試要點複習

  一、對象流

串行化:對象通過寫出描述自己狀態的數值來記述自己的過程叫串行話

對象流:能夠輸入輸出對象的流

將串行化的對象通過對象流寫入文件或傳送到其他地方

對象流是在普通流上加了傳輸對象的功能,所以構造對象流時要先構造普通文件流

注意:只有實現了Serializable接口的類才能被串行化

例子:

import .*;

class Student implements Serializable{

private String name;

private int age;

public Student(String name,int age){

=name;

=age;

}

public void greeting(){

tln("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 s1=new Student("Jerry",24);

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

eObject(s1);

eObject(s2);

}catch(Exception e){

tStackTrace();

}finally{

if(oos!=null)

try{

e();

}catch(Exception e){

tStackTrace();

}

}

}

}

import .*;

public class ObjectInTest{

public static void main(String args[]){

ObjectInputStream ois=null;

Student s=null;

try{

ois=new ObjectInputStream(

new FileInputStream(""));

tln("--------------------");

s=(Student)Object();

tln(s);

ting();

tln("--------------------");

s=(Student)Object();

tln(s);

ting();

}catch(Exception e){

tStackTrace();

}finally{

if(ois!=null)

try{

e();

}catch(Exception e){

tStackTrace();

}

}

}

}

  二、字符流 InputStreamReader/OutputStreamWriter

上面的幾種流的.單位是 byte,所以叫做字節流,寫入文件的都是二進制字節,我們無法直接看,下面要學習的是字節流

Java採用 Unicode 字符集,每個字符和漢字都採用2個字節進行編碼,ASCII 碼是 Unicode 編碼的自集

InputStreamReader 是 字節流 到 字符橋的橋樑 ( byte->char 讀取字節然後用特定字符集編碼成字符)

OutputStreamWriter是 字符流 到 字節流的橋樑 ( char->byte )

他們是在字節流的基礎上加了橋樑作用,所以構造他們時要先構造普通文件流

我們常用的是:

BufferedReader 方法:readLine()

PrintWriter 方法:println()

例子:

import .*;

public class PrintWriterTest{

public static void main(String args[]){

PrintWriter pw=null;

try{

pw=new PrintWriter(

new OutputStreamWriter(

new FileOutputStream("")));

tln("hello world");

}catch(Exception e){

tStackTrace();

}finally{

if(pw!=null)

try{

e();

}catch(Exception e){

tStackTrace();

}

}

}

}

import .*;

public class BufferedReaderTest{

public static void main(String args[]){

BufferedReader br=null;

try{

br=new BufferedReader(

new InputStreamReader(

new FileInputStream("")));

tln(Line());

}catch(Exception e){

tStackTrace();

}finally{

if(br!=null)

try{

e();

}catch(Exception e){

tStackTrace();

}

}

}

}