【填空题】以下是对象数据写入文件(d:data.txt)的程序代码,在【1】~【3】处填上适当的语句,使程序完整。 public writeF(){ { 【1】 {【2】; 【3】 wf=new ObjectOutputStream(new FileOutputStream(file); wf.writeObject(t); //t为一数据对象 wf.close(); }catch(Exception e){} }

【填空题】以下是对象数据写入文件(d:data.txt)的程序代码,在【1】~【3】处填上适当的语句,使程序完整。 public writeF(){ { 【1】 {【2】; 【3】 wf=new ObjectOutputStream(new FileOutputStream(file); wf.writeObject(t); //t为一数据对象 wf.close(); }catch(Exception e){} }


参考答案和解析
A

相关考题:

本题的功能是将文件java2.java复制为文件java2.java.bak。 importjava.io.*; public class java2{ public static void main(String sarg[]){ try{File file=new File("java2.java.bak"); ; FilelnputStream rfile=new FilelnputStream("ja- va2.java"); FileOutputStream wfile=new FileOutputStream ("java2.java.bak"); int c; while( ) wfile.write((char)c); rfile.close; wfile.close; }catch(Exception e){System.out.println("读取文 件的时候发生错误!");} System.out.println("复制操作完成!"); } }

阅读下列代码段 ByteArrayOutputStream bout=new ByteArrayOutputStream(); ObjectOutputStream ut=new ObjectOutputStream(bout); out. writeObject(this); out. close(); 以上代码段的作用是A.将对象写入内存B.将对象写入硬盘C.将对象写入光盘D.将对象写入文件

请完成下列Java程序:读取自己的源文件并把它压缩成GZIP文件。注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。import java.io.*;import java.util.zip.*;public class exl6 2 {public static void main{String[] arg) {ex16_2 obj16_2 = new ex16_2();obj16_2.doZip("ex16_2.java","ex16_2.gzip")}public void doZip(String strIn,String strOutFileInputStream in;FileOutputStream out;GZIPOutputStream gzip;int nFileLength;byte[] barray = new byte[10];try {in = new FileInputStream(strIn);out = new FileOutputStream(strOut);gzip = new GZIPOutputStream(out);while((nFileLength = in.read(barray, 0 barray.length)) > 0)_____________________;______________________;gzip.close();} catch(Exception e) {e.printStackTrace();}}}

下列程序的功能是在监控台上每隔一秒钟显示一个字符串“你好!”,能够填写在程序中画线位置,使程序完整并能正确运行的语句是 public class Exam implements Runnable{ public static void main(String args[]){ Exam t=new Exam(); Thread tt=new Thread(t); tt.start(); } public void run(){ for(;;){ try{ ; }catch(e){} System.out.println("你好!"); } } }A.sleep(1) RuntimeExceptionB.t.sleep(1000) InterruptedExceptionC.Thread.sleep(1) InterruptedExceptionD.Thread.sleep(1000) InterruptedException

请完成程序,首先由一个类simple实现Serializable接口,并有三个成员变量,分别为int型、double型和String型,可以用toString的方法显示这三个成员变量。在main方法中创建这个simple的持久对象,根据用户在命令行输入的三个参数来设定其中成员变量的值。然后,将这个对象写入名为TheSerial.data的文件中,并显示成员变量。最后从文件TheSerial.data中读出三个成员变量并显示出来。注意:请勿修改main()主方法和其他已有语句内容,仅在横线处填入适当语句。import java.io.*;class TheSerial implements Serializable{private int intvalue;private double doublevalue;private String string;The Serial (){intvalue=123;doublevalue=12.34;string="Serialize Test";}public void setDouble(double d){doublevalue=d;}public void setInt(int i){intvalue=i;}public void setString(String s){string=s;}public String to String(){return("int="+intvalue+" double="+doublevalue+" string="+string);}}public class simple{public static void main(String[] args){The Serial e1=new TheSerial();TheSerial e2;try{e1.setInt(Integer.parseInt(args[0]));e1.setDouble(Double.parseDouble(args[1]));e1.setString(args[2]);}catch(Exception e){e1.setString(e.getMessage());}System.out.println(e1);try{FileOutputStream S=new FileOutputStream("TheSerial.data");ObjectOutputStream OS=new ObjectOutputStream(oS);______;}catch(IOException ioException){System.out.println (ioException.getMessage ());}try{FileInputStream iS=new FileInputStream("TheSerial.data");ObjectInputStream IS=new ObjectInputStream(iS);______;System.out.println(e2);}catch(IOException ioException){System.out.println(ioException.getMessage());}catch(ClassNotFoundException cnfException){System.out.println(cnfException.getMessage());}}}

下面程序执行后,输出结果为:true请在程序的每条横线处填写一个语句,使程序的功能完整。注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。public class TestStringCompare{{public static void main(String ____________________ args){char charl[]={'t','e','s','t'};char char2[]={'t','e','s','t','1'};String str1=new String(___________________);String str2=new String(char2,0,4);System.out.println(__________________________);}}

创建线程对象,要传递代码与数据,而传递代码与数据有两种方法,一是通过继承Thread类,二是向Thread类传递一个Runnable对象。请在下面程序的每条横线处填写一个语句,使程序的功能完整。注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。public class TestThread{public static void main(String args[ ]){MyThread t=new MyThread();_______________________}}class MyThread_____________Thread{_____________________{for(int i=0;i10;i++){System.out.println(" " +i);}}}

下列程序建立一个file.dat的文件,并且允许按照不同的数据类型向里面输入数据,请将程序补充完整。注意:不改动程序结构,不得增行或删行。package ch3;import java.io.*;public class Test{public static void main(String[] args){try{______fos=new______("ch3/file.dat");______}catch(IOException ioe){ioe.printStackTrace();}}}

在当前文件夹中生成一个名为Filel.txt的文件,在D盘里生成一个名为mydir的文件夹,并在该文件夹中生成一个名为File2.txt的文件,最后输出文件File2.txt的完整路径。修改下列程序中的错误之处,使程序能够正确运行。注意:不改动程序结构,不得增行或删行。import java.io.*;public class FileTest{public static void main(String args[]){File dirObject=new File(d:\\mydir);File fileObjectl=new File(d:\\kmydir","Filel.txt");File fileObject2=new File(d:/mydir","File2.txt");System.out.println(fileObject2);try{Dirobject.mkdir();}catch(SecurityException e){}try{fileObject1.createNewFile();fileObject2.createNewFile();}catch(I0Exception e){}}}

请完成下列Java程序:程序的功能演示了如何通过实现Runnable接口创建线程对象,程序中定义了一个类B,类中重写了含一个字符串参数的构造方法,并实现了Runnable接口,即在类B中编写了接口中的run()方法的方法体。还定义了一个应用程序类ex35_2,其中创建类B的3个对象b1,b2和b3作为线程对象t1,t2和t3的参数,并启动这3个线程。注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。程序运行结果如下:public class ex35_2{public static void main(String args[ ]){Runnable b1=new B("First");Runnable b2=new B("Second");Runnable b3=new B("Third");Thread t1=new Thread(b1);Thread t2=new Thread(b2);Thread t3=new Thread(b3);t1.start ();t2.start ();t3.start();}}class B _____________________ Runnable{String s;public B(String str){s=str;}_________________{for(int i=1;i<3;i++){System. out. println ( s+ "运行!");try{Thread.sleep((int) (Math.random() *100) );}catch (InterruptedException e){e.printStackTrace ( );}}System. out.println (s+"结束!");}}

请完成以下程序,首先由一个类Example2_3实现Serializable接口,并有三个成员变量,分别为int型、double型和String型,可以用toString的方法显示这三个成员变量。在main方法中创建这个Example2_3的持久对象,根据用户在命令行输入的三个参数来设定其中成员变量的值。然后,将这个对象写入名为TheSerial.data的文件,并显示成员变量。最后从文件TheSerial.data中读出三个成员变量并显示出来。注意:请勿改动main()主方法和其他已有语句内容,仅在横线处填入适当语句。import java.io.*;class TheSerial implements Serializable{private int intValue;private double doubleValue;private String string;TheSerial(){intValue = 123;doubleValue = 12.34;string = "Serialize Test";}public void setDouble(double d){doubleValue = d;}public void setInt(int i){intValue = i;}public void setString(String s){string = s;}public String toString(){return("int="+intValue+"double="+doubleValue+" string="+string);}}public class Example2_3{public static void main(String argv[]){TheSerial e1 = new TheSerial();TheSerial e2;try{e1.setInt(Integer.parseInt(argv[0]));e1.setDouble(Double.parseDouble(argv[1]));e1.setString[argv[2]);}catch(Exception e){e1.setString(e.getMessage{));}System.out.println(e1);try{FileOutputStream S = new FileOutputStream("TheSerial.data");ObjectOutputStream IS = new ObjectOutputStream(oS);______;}catch(IOException ioException){System.out.println(ioException.getMessage());}try{FileInputStream iS = new FileInputStream("TheSerial. data");ObjectInputStream IS = new ObjectInputStream(iS);______System.out.println(e2);}catch(IOException ioException){System.out.println(ioException.getMessage());}catch(ClassNotFoundException cnfException){System.out.println(cnfException.getMessage());}}}

下列程序将类C15的对象写入文件filetxt,选择正确的语句填入下列程序的横线处。 package ch2; import java.util.*; import java.io.*; class C15______ { public iht a; public void setInt(int newa) { a = newa; } } public class Testl5 { pulibc static void main(String[] args) { C15 bj = new C15(); try { FileOutputStream fos =newFileOutputStream("ch2\\filel5.txt"); ObjectOutputStream os = ObjectOutputStream(fos); Obj.setInt(10); oos.writeObject(obj); oos.close (); fos.close(); } catch(IOException ioe) { ioe.printStackTrace(); } } }A.implements RunnableB.implements SedalizableC.extends SerializableD.implements InputStream

下面程序的功能是从键盘读取一行文本并将该文本存储到文件中。当用户运行该程序时,需要输入一行文本并按下回车键。然后在DOS系统提示符处输入type file.txt并按下Enter键,这时屏幕会显示type file.txt的内容。通过这个命令可以验证该程序的功能。请在每条横线处填写一条语句,使程序的功能完整。注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。源程序文件代码清单如下:import java.io.*;public class DoFile{public static void main(String args[]){byte buffername[]=______[80];try{System.out.println("\nEnter a line to be saved to disk:");int bytesbleck= ______;FileOutputStream f=new FileOutputStream("file.txt");______;}catch(Exception e){e.printStackTrace();}}}

在程序中,用户输入一个文件名,根据用户输入显示相应文件的信息。注意:请勿修改main()主方法和其他已有语句内容,仅在横线处填入适当语句。______java.io.*;public class basic{public static void main(String[] args){InputStreamReader reader;BufferedReader in;System.out.println("请输入文件名: ");try{reader=new InputStreamReader(______);in=new BufferedReader(reader);String filename=in.readLine();File file=new File(filename);System.out.println("文件名:"+file.______);System.out.println("路径:"+file.getAbsolutePath());System.out.println("大小:"+file.length());}catch(Exception e){e.printStackTrace();}}}

下列程序的运行结果是______。 package ch1; import java.io. *; public class ex28 { public static void main (String args [] ) throws IOException { try { File f1 = new File("ch1\\dir28"); f1.mkdir(); File f2 = new File(f1, "file59.txt"); FileOutputStream fos = new FileOutputStream(f2); for(int i = 0; i < 2; i++) { String s= i + "times"; byte[] b = s.getBytes(); fos.write(b,0,b.length); } } catch(IOException ioe) { ioe.printStackTrace(); } } }A.在目录ch1下建立一个目录dir28,并且建立文件file28.txt,在文件中写入"Otimes 1 times"B.在目录ch1下建立一个目录dir28,并且建立文件file28.txt,在文件中写入“l times”C.在目录chi下建立一个目录dir28,并且建立文件file28.txt,在文件中写入“Otimes”D.抛出IOExceptin异常

选择正确的语句填在下列程序的横线处,使程序正常运行。 package ch1; import java. io. *; import j ava. util. *; class C45 implements Seritizable ______Public Thread t = new Thread(new T45(), "t"); public intcnt = 0; public C45() { t. start ( ) class T45 implements Runnable public int[] a = new int[4]; public void run() { for(int i - 0; i < 4; i++) { a[i] - i +4; } } } public class ex45 { static String fileName = "ch1\\file45.txt"; public static void main(String[] args) throws Exception { C45 bj = new C45 (); FileOutputStream fos = new FileOutputStream(fileName); ObjectOutputStream os = new ObjectOutputStream(fos); oos. writeObject (obj); oos.clese (); fos.close (); System. out, println (obj . toString ( ) ); } }A.transientB.protectedC.packageD.final

下列程序的功能是在监控台上每隔一秒钟显示一个字符串”Hello!”,能够填写在程序中下画线位置,使程序完整并能正确运行的语句是( )。 public class Test implements Runnable{ public static void main(String args[]){ Test t=new Test: Threadt t=new Thread(t); tt.start: } public void run{ for(;;){ try{ ; }catch(e){} System.out.println("Hello"); } } }A.sleep(1000)B.t.sleep(1000)InterruptedExceptionInterruptedExceptionC.Thread.sleep(1000)D.Thread.sleep(1000)RuntimeExceptionInterruptedException

使用VC6打开考生文件夹下的工程test14_3,此工程包含一个test14_3.cpp,其中定义了类File,但类的定义并不完整。请按要求完成下列操作,将程序补充完整。(1)完成构造函数的定义,使数据成员filename,content指向为空。请在注释“//**1**”之后添加适当的语句;(2)完成函数set_contents的定义,该函数为content申请新的空间来存储new_content的内容,成功返回true,失败返回false。请在注释“//**2**”之后添加适当的语句;(3)完成成员函数copy(File source,File target)的定义,该函数实现将source的内容拷贝至target的功能,注意必须使用已经定义的成员函数来实现。请在注释“//**3**”之后添加适当的语句:(4)完成析构函数的定义,要求释放content指向的空间。请在注释“//**4**”之后添加适当的语句。输出结果如下:file2:file1 contentsfile2:file2 contents注意:除在指定的位置添加语句外,请不要改动程序中的其他语句。源程序文件testl4—3.cpp清单如下:include<iostream.h>include<string.h>class File{private:char file_name[64];char*contents;public:File(char*filename){// ** 1 **};File::~File(){// ** 4 **}void printfile(){cout<<file_name<<":"<<contents<<endl;)friend bool copy(File source,File target);bool set_contents(char * new_contents);};bool File::set_contents(char*new_contents){// ** 2 **if(contents==NULL)return false;strcpy(contents,new_contents);return true;}bool copy(File source,File target){// ** 3 **}void main{){File f1("filel"),f2("file2");f1.set_contents("filel contents");copy(f1,f2);f2.printfile();f2.set_contents("file2 contents,);f2.printfile();}

阅读以下说明和Java代码,将解答写入对应栏内。【说明】请完成下列Java程序。程序的执行结果是生成一个具有一个TextField类型的对象in、 Button类型的对象btn和Label类型的对象out图形用户界面,程序的功能是计算用户输入数的平方,如图3所示。注意:请勿改动main()主方法和其他已有的语句内容,仅在下划线处填入适当的语句。【程序】import java. awt.*;import java, awt. event.*;public class square {public static void main(String args[ ]){(1)}}class AppFrame. extends Frame{TheAdapterTest listener = new TheAdapterTest( );Text Field in = new TextField (5);Button btn = new Button("计算");Label ut = new Label("用于显示计算结果");public AppFrame( ){setLayout( new FlowLayout( ));add(in);add(btn)add(out);btn. addActionListener( new BtnActionAdapter( ));addWindowListener (listener);setSize(400,100);show( );}class BtnActionAdapter implements (2) {public void actionPerformed((3)) {String s = in. getText( );double d =(4)double sq = d * d;out. setText(d+"的平方是:" +sq);}}class TheAdapterTest extends WindowAdapter{public void windowCIosing((5)){System. exit(1)}}}

请完成下列Java程序:首先建立一个类,包含2个私有成员变量,分别为int和double型,并提供公共方法setInt()和 setDouble()分别给这两个变量赋值和用toString()方法显示对象的私有成员变量的值,要求将这个类定义为Serializable,再创建这个类的持久对象,将对象写入名为ex3_2.data的文件中并显示对象的2个成员变量,最后从这个文件中读取这个对象并显示对象的2个成员变量。注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。程序运行结果如下:ex3_2: i=123 d=123.456ex3_2: i=123 d=123.456import java.io.*;public class ex3_2 implements Serializable {private int n3_2;private double d3_2;ex3_2() {n3_2 = 25;d3_2 = 12.23;}public void setDouble(double x) {d3_2 = x;}public void setInt(int x) {n3_2 = x;}public String toString() {return("ex3_2: i=" + n3_2 + "d=" + d3_2);}public static void main(String[] arg) {ex3_2 e1 = new ex3_2 );ex3_2 e2;e1.setInt(123);e1.setDouble(123.456);System.out.println(e1);try {FileOutputStream fos3_2 = new FileOutputStream("ex3_2.data");ObjectOutputStream oos3_2 = new ObjectOutputStream(fos3_2);________________;} catch(IOException ioe) {System.out.println(ioe);}try {FileInputStream fis3_2 = new FileInputStream("ex3_2.data");ObjectInputStream ois3_2 = new ObjectInputStream(fis3_2);________________;System.out.println(e2);}catch(10Exception ioe) {System.out.println(ioe);}catch(ClassNotFoundException cnfe) {System.out.println(cnfe);}}}

下列程序将Date对象写入文件file42.txt中,选择正确的语句填入下列程序中的横线处。 package ch1; impbrt java. io. *; import java. util. *; public class ex42 { static String fileName = "ch1\\file42.txt"; static Date date = null; public static void main(String[] args) { date = new Date(); try { FileOutputStream fos = new FileOutputStream(file Name); ObjectOutStream os = new ObjectOutputStream(fos); oos.______; oos.close(); fos.close(); System.out.println(date.toString()); } catch(Exception e) { System.out.println(e.getMessage()); } } }A.writeObject()B.writeObject(date)C.write(date)D.writeByte(date)

( 11 )请在下列程序的空白处,填上适当的内容:Import java. awt. *;Import java. util. *;Class BufferTest{Public static void main(string args[])Throws IOException{FileOutputStream unbuf=new FileOutputStream( “ test.one ” ) ;BufferedOutputStream buf=new 【 11 】 (new FileOutputStream( “ test.two ” ));System.out.println( “ write file unbuffered: ” + time(unbuf) + “ ms ” );System.out.println( “ write file buffered: ” + time(buf) + “ ms ” );}Static int time (OutputStream os)Throws IOException{Date then = new Date();for (int i=0; i50000; i++){os.write(1);}}os.close();return(int)(()new Date()).getTime() - then.getTime());}

创建一个向文件“file.txt”追加内容的输出流对象的语句有()。A、FileOutputStream out=new FileOutputStream(“file.txt”,true);B、OutputStream out=new FileOutputStream(“file.txt”,“append”);C、OutputStream out=new FileOutputStream(“file.txt”);D、FileOutputStream out=new FileOutputStream(new file(“file.txt”));E、OutputStream out=new FileOutputStream(new File(“file.txt”),true.;

import java.io.*;  public class Forest implements Serializable {  private Tree tree = new Tree();  public static void main(String [] args) {  Forest f= new Forest();  try {  FileOutputStream fs = new FileOutputStream(”Forest.ser”);  ObjectOutputStream os = new ObjectOutputStream(fs);  os.writeObject(f); os.close();  } catch (Exception ex) { ex.printStackTrace(); }  }  }  class Tree { }  What is the result?() A、 Compilation fails.B、 An exception is thrown at runtime.C、 An instance of Forest is serialized.D、 A instance of Forest and an instance of Tree are both serialized.

单选题在下列程序的空白处,应填入的正确选项是(  )。import java.io.*;public class ObjectStreamTest{public static void main(string args[])throws IOException{ ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("serial.bin")); Java.until.Date d=new Java.until.Date(); oos.______(); ObjectInputStream ois=new ObjectlnputStream(new FileOutputStream("serial.bin")); try{ java.until.date restoredDate=(Java.until.Date)ois.readObject(); System.out.println("read object back from serial.bin file:"+restoredDate); } catch(ClassNotFoundException cnf){ System.out.println("class not found"); }}AreadObjectBWriterCBufferedWriterDwriteObject

单选题import java.io.*;  public class Forest implements Serializable {  private Tree tree = new Tree();  public static void main(String [] args) {  Forest f= new Forest();  try {  FileOutputStream fs = new FileOutputStream(”Forest.ser”);  ObjectOutputStream os = new ObjectOutputStream(fs);  os.writeObject(f); os.close();  } catch (Exception ex) { ex.printStackTrace(); }  }  }  class Tree { }  What is the result?()A Compilation fails.B An exception is thrown at runtime.C An instance of Forest is serialized.D A instance of Forest and an instance of Tree are both serialized.

多选题创建一个向文件“file.txt”追加内容的输出流对象的语句有()。AFileOutputStream out=new FileOutputStream(“file.txt”,true);BOutputStream out=new FileOutputStream(“file.txt”,“append”);COutputStream out=new FileOutputStream(“file.txt”);DFileOutputStream out=new FileOutputStream(new file(“file.txt”));EOutputStream out=new FileOutputStream(new File(“file.txt”),true.;

单选题阅读下列代码段:ByteArrayOutputStream bout=new ByteArrayOutputStream();ObjectOutputStream out=new ObjectOutputStream(bout);out.writeObject(this);out.close();以上代码段的作用是(  )。A将对象写入内存B将对象写入硬盘C将对象写入光盘D将对象写入文件