當前位置:才華齋>計算機>java語言>

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

java語言 閱讀(2.58W)

知識就是靠積累起來的,經驗也是積累起來的。以下是本站小編整理的計算機二級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();

}

}

}

}