JAVA练习题(第9章).ppt

上传人:牧羊曲112 文档编号:6510100 上传时间:2023-11-07 格式:PPT 页数:39 大小:302.99KB
返回 下载 相关 举报
JAVA练习题(第9章).ppt_第1页
第1页 / 共39页
JAVA练习题(第9章).ppt_第2页
第2页 / 共39页
JAVA练习题(第9章).ppt_第3页
第3页 / 共39页
JAVA练习题(第9章).ppt_第4页
第4页 / 共39页
JAVA练习题(第9章).ppt_第5页
第5页 / 共39页
点击查看更多>>
资源描述

《JAVA练习题(第9章).ppt》由会员分享,可在线阅读,更多相关《JAVA练习题(第9章).ppt(39页珍藏版)》请在三一办公上搜索。

1、JAVA程序设计练习题,第9章,一、选择题,1.可用于获得文件或目录的路径名的是()A.File B.RandomAccessFile C.FileInputStream D.BufferedReader 2.下面语句的功能是()。RandomAccessFile raf2=new RandomAccessFile(1.txt,rw);A.打开当前目录下的文件1.txt,既可以向文件写数据,也可以从文件读数据。B.打开当前目录下的文件1.txt,但只能向文件写入数据,不能从文件读取数据。C.打开当前目录下的文件1.txt,但不能向文件写入数据,只能从文件读取数据。D.以上说法都不对。,【答案】

2、:A,【答案】:A,3.关于BufferedReader类的readLine()方法,以下说法不正确的是()。A.方法readLine()每次读取一行数据。B.方法readLine()每次读取一个字节。C.该方法可能抛出IOException异常,调用该方法时通常应将它放到try块中,并通过catch块处理异常。D.如果读到流的末尾,该方法返回的结果为null。,【答案】:,.用于关闭流的方法是()A.read()B.flush()C.close()D.write().下列哪一个import命令可以使我们在程序中创建输入/输出流对象()A.import java.sql.*;B.import

3、java.util.*;C.import java.io.*;D.import.*;,【答案】:,【答案】:,.字符流与字节流的区别在于()A前者带有缓冲,后者没有B前者是块读写,后者是字节读写C.二者没有区别,可以互换使用D.每次读写的字节数不同.随机文件访问是由()类实现的。A.File B.BufferedInputStreamC.RandomAccessFile D.FileWriter,【答案】:,【答案】:,.下列流中哪个不属于字节流()AFileInputStreamBBufferedInputStreamC.FilterInputStreamD.InputStreamReade

4、r.在Java的类库中,包含实现输入/输出操作的包是(),【答案】:,【答案】:,10.FilterOutputStream是BufferedOutputStream、DataOutputStream及PrintStream的父类,以下哪个类可能是FilterOutputStream构造函数的参数类型?A.OutputStream B.FileC.InputStream D.BufferedOutputStream 11.下列哪一个import命令可以使我们在程序中创建输入/输出流对象()A.import java.sql.*;B.import java.util.*;C.import jav

5、a.io.*;D.import.*;,【答案】:A,【答案】:,12.如果需要从文件中读取数据,则可以在程序中创建哪一个类的对象()A.FileInputStream B.FileOutputStream C.DataOutputStream D.FileWriter13.下面的程序段创建了BufferedReader类的对象in,以便读取本机c盘my文件夹下的文件1.txt。File构造函数中正确的路径和文件名的表示是()。File f=new File(填代码处);file=new FileReader(f);in=new BufferedReader(file);A./1.txt B./

6、my/1.txt C.c:my1.txt D.c:my1.txt,【答案】:A,【答案】:C,14.下面的程序创建了一个文件输出流对象,用来向文件test.txt中输出数据,假设程序当前目录下不存在文件test.txt,编译下面的程序Test.java后,将该程序运行3次,则文件test.txt 的内容是()。import java.io.*;public class Test public static void main(String args)try String s=ABCDE;byte b=s.getBytes();FileOutputStream file=new FileOutp

7、utStream(test.txt,true);file.write(b);file.close();catch(IOException e)System.out.println(e.toString();A.ABCABC B.ABCDE C.Test D.ABCDE ABCDE ABCDE,【答案】:D,15.下面的程序段的功能是()。File file1=new File(d:xxxyyyzzz);file1.mkdirs();A.在当前目录下生成子目录:xxxyyyzzz B.生成目录:e:xxxyyyzzzC.在当前目录下生成文件xxx.yyy.zzz D.以上说法都不对 16.流的传

8、递方式是()A.并行的 B.串行的 C.并行和串行 D.以上都不对,【答案】:D,【答案】:B,17.编译和运行下面的应用程序,并在命令行界面输入12345,则回车后屏幕输出的结果是().public class A public static void main(String args)throws IOException BufferedReader buf=new BufferedReader(new InputStreamReader(System.in);String str=buf.readLine();int x=Integer.parseInt(str);System.out.

9、println(x/100);A.45 B.5 C.123 D.12345,【答案】:C,18.下列不是java的输入输出流的是()A.文本流 B.字节流 C.字符流 D.文件流19凡是从中央处理器流向外部设备的数据流称为()A.文件流 B.字符流 C.输入流 D.输出流,【答案】:A,【答案】:D,20.获取一个不包含路径的文件名的方法为()A.String getName()B.String getPath()C.String getAbslutePath()D.String getParent()21下列属于文件输入输出类的是()A.FileInputStream和FileOutputS

10、tream B.BufferInputStream和BufferOutputStream C.PipedInputStream和PipedOutputStream D.以上都是,【答案】:A,【答案】:A,22.下列不属于FileInputStream输入流的read()成员函数的是()A.int read();B.int read(byte b);C.int read(byte b,int offset,int len);D.int read(int line);23当处理的数据量很多,或向文件些很多次小数据,一般使用()流 A.DataOutput B.FileOutput C.Buffe

11、redOutput D.PipedOutput,【答案】:D,【答案】:C,24.当要将一文本文件当作一个数据库访问,读完一个纪录后,跳到另一个纪录,它们在文件的不同地方时,一般使用()类访问。A.FileOutputStream B.RandomAccessFile C.PipedOutputStream D.BufferedOutputStream25.在JAVA中,要创建一个新目录,要使用()类的实例。A.File B.FileOutputStrean C.PrintWriter D.Dir,【答案】:B,【答案】:A,26.在JAVA中,要判断D盘下是否存在文件abc.txt,应该使用

12、以下()判断语句。A.if(new File(“d:abc.txt”).exists()=1)B.if(File.exists(“d:abc.txt”)=1)C.if(new File(“d:/abc.txt”).exists()D.if(File.exists(“d:/abc.txt)27.在JAVA中,()类提供定位本地文件系统,对文件或目录及其属性进行基本操作。A.Filelnputstream B.FileReader C.Filewriter D.File,【答案】:C,【答案】:D,28.给定一个java程序的main方法的代码片段如下:假如d 目录下不存在abc.txt文件,现运

13、行该程序,下面的结果正确的是()。try PrintWriter out=new PrintWriter(new FileOutputStream(“d:/abc.txt”);String name=”chen”;out.print(name);out.close();catch(Execption e)System.out.println(“文件没有发现!“);A.将在控制台上打印:“文件没有发现!”B.正常运行,但没有生成文件abc.txtC.运行后生成abc.txt,但该文件中无内容D.运行后生成abc.txt,该文件内容为:chen,【答案】:D,29.在java中,以下代码()正确地

14、创建了一个InputStreamReader类型的对象。A.new InputStreamReader(new FileInputStream(1.dat);B.new InputStreamReader(new FileReader(1.dat);C.new InputStreamReader(new BufferedReader(1.dat);D.new InputStreamReader(1.dat);30.下列选项中属于过滤流FilterInputStream的子类的是()。A.DataInputStreamB.DataOutputStreamC.PrintStreamD.Buffer

15、edOutputStream,【答案】:A,【答案】:A,二、填空题,1.对象串行化可以很容易地扩展成支持Java对象的 持续存储或持久存储,它提供了对象从流中重建的补充方式。2.FileInputStream是字节流;BufferedWriter是字符流;ObjectOutputStream是 对象流。3.java的输入输出流包括_字节流_、字符流、文件流、对象流以及多线程之间通信的管道。4.java中的非字符输出流都是_ OutputStream _抽象类的子类。5java对I/O访问还提供了同步处理机制,保证某时刻只有一个线程访问一个I/O流,这就是_过滤流_6.java中的字符输入流都

16、是抽象类_ Reader _的子类,字符输出流都是抽象类_ Writer _的子类。,7.DataOutputStream数据流向文件里写数据的方法为_ write()_.8.RandomAccessFile所实现的接口是_ DataInput接口_和DataOutput接口.9.文件类_ File _是java.io中的一个重要的非流类,里面封装了对文件系统进行操作的功能.10.文件操作中经常需要的是随机访问,java中的_ RandomAccessFile _类提供了随机访问文件的功能.11.数据流包括数据输入流(DataInputStream)类和数据输出流(DataOutputStre

17、am)类12.判断File对象所表示的文件是否可写的方法是(canWrite()),判断File对象所表示的文件是否可读的方法是(canRead()),三、读程序写结果,.import java.io.*;public class DataStreamTestpublic static void main(String args)FileInputStream fis;FileOutputStream fos;DataInputStream dis;DataOutputStream dos;try fos=new FileOutputStream(write.dat);dos=new Data

18、OutputStream(fos);dos.writeUTF(Java程序设计);dos.writeDouble(30.6);dos.writeInt(337);dos.writeBoolean(true);dos.close();,fis=new FileInputStream(write.dat);dis=new DataInputStream(fis);System.out.println(书名:+dis.readUTF();System.out.println(单价:+dis.readDouble();System.out.println(页数:+dis.readInt();Syste

19、m.out.println(“是否适合初学者:+dis.readBoolean();dis.close();catch(IOException e)e.printStackTrace();,书名:Java程序设计单价:30.6页数:337是否适合初学者:true,2.import java.io.*;public class FileReaderWriterTestpublic static void main(String args)FileReader fr=null;FileWriter fs=null;FileWriter fd=null;FileReader ft=null;try

20、fs=new FileWriter(source.txt);fs.write(很高兴学习java!);fs.close();fr=new FileReader(source.txt);fd=new FileWriter(dest.txt);int c;,System.out.print(源文件内容:);while(c=fr.read()!=-1)System.out.print(char)c);fd.write(c);fd.close();fr.close();System.out.print(n目标文件内容:);ft=new FileReader(dest.txt);char ch=new

21、char100;int count;while(count=ft.read(ch)!=-1)System.out.print(new String(ch,0,count);ft.close();catch(IOException e)e.printStackTrace();,源文件内容:很高兴学习java!目标文件内容:很高兴学习java!,3.import java.io.*;public class Example public static void main(String args)try FileOutputStream fos=new FileOutputStream(xx);fo

22、r(byte b=10;b50;b+)fos.write(b);fos.close();RandomAccessFile raf=new RandomAccessFile(xx,r);raf.seek(10);int i=raf.read();raf.close();System.out.println(i=+i);catch(IOException ioe),i=20,4.写出以下程序的功能。import java.io.*;public class TestFilepublic static void main(String args)throws Exception BufferedRe

23、ader br=new BufferedReader(new InputStreamReader(System.in);BufferedWriter bw=new BufferedWriter(new FileWriter(“input.txt);String s;while(true)System.out.print(请输入一个字符串:);System.out.flush();s=br.readLine();if(s.length()=0)break;bw.write(s);bw.newLine();bw.close();,功能:从键盘逐一输入字符串,逐一输出至input.txt文件中,直至

24、输入空行。,5.读下列代码,说出这段程序的功能。import java.io.*;public class Test public static void main(String argv)try BufferedReader is=new BufferedReader(new InputStreamReader(System.in);String inputLine;While(inputLine=is.readLine()!=null)System.out.println(inputLine);is.close();catch(IOException e)System.out.printl

25、n(IOException:+e);,功能:读取键盘输入,显示到屏幕上,直到键盘输入为空为止。,6.读下列代码,说出这段程序的功能。import java.io.*;public class Reverse2 public static void main(String args)int i,n=10;int a=new int10;try BufferedReader br=new BufferedReader(new InputStreamReader(System.in);ai=Integer.parseInt(br.readLine();catch(IOException e);for

26、(i=n-1;i=0;i=i-2)System.out.print(ai+);System.out.println();,功能:该程序使用字符缓冲输入流从键盘输入10个数,然后倒序并间隔打印出来。,7.import java.io.*;public class TestRandomAccess public static void main(String args)int data_arr=65,66,56,23,27,1,43,65,4,99;try RandomAccessFile randf=new RandomAccessFile(temp.dat,rw);for(int i=0;i=

27、0;i=i-2)randf.seek(i*4);System.out.print(+randf.readInt();randf.seek(40);System.out.println(randf.readUTF();randf.close();,99 65 1 23 66 Good morning!,catch(IOException e)System.out.println(File access error:+e);,8.阅读下面的程序,并回答问题。import java.io.*;public class Test public static void main(String args)

28、throws IOException BufferedReader buf=new BufferedReader(new InputStreamReader(System.in);while(true)String str=buf.readLine();if(str.equals(quit)break;int x=Integer.parseInt(str);System.out.println(x*x);编译运行上面的程序:从键盘输入5,回车后输出的结果如何?从键盘输入quit,回车后程序执行情况如何?,25 终止应用程序的运行,9.import java.io.*;public class

29、Test public static void main(String args)throws IOException BufferedReader buf=new BufferedReader(new InputStreamReader(System.in);while(true)String str=buf.readLine();if(str.equals(quit)break;int x=Integer.parseInt(str);System.out.println(x*x);编译运行上面的程序:从键盘输入10,回车后输出的结果如何?从键盘输入exit,回车后程序能正确执行吗?为什么?

30、,100 不能;因为方法Integer.parseInt(str)不能将字符串“exit”转化为整数,抛出异常。,四、补充代码,完成程序,1.下列程序的功能是使用BufferedReader读取文件BufferedReaderWriterTest.java内容,添加行号后再使用BufferedWriter写入到文件dest.java中。1:;/导入所需数据包2:public class BufferedReaderWriterTest 3:public static void main(String args)4:try 5:FileReader f=;/创建文件输入字符流f6:Buffere

31、dReader br=;/创建缓存输入字符流br,并套接输入流f7:FileWriter fw=;/创建文件输出字符流fw,import java.io.*,new FileReader(“BufferedReaderWriterTest.java”),new BufferedReader(f),new FileWriter(“dest.java”),8:BufferedWriter bw=;/创建缓存输出字符流bw,并套接输入流fw9:String s;10:int i=1;11:while(s=br.readLine()!=null)12:bw.write(i+:+s);13:bw.new

32、Line();14:15:bw.flush();16:br.close();17:bw.close();18:19:catch(IOException e)20:e.printStackTrace();21:22:23:,new BufferedWriter(fw),2.按注释提示完成文件复制的程序 import java.io.*;class FileStream public static void main(String args)try File inFile=new File(file1.txt);/指定源文件 File outFile=new File(file2.txt);/指定

33、目标文件 FileInputStream fis=;FileOutputStream fos=new FileOutputStream(outFile);int c;/逐字节从源文件中输入,再输出到fos流while(c=fis.read()!=-1);fis.close();fos.close();,new FileInputStream(inFile),fos.write(c),catch(Exception e)System.out.println(FileStreamsTest:+e);,3.下列程序实现从控制台输入并读取输出字符串.请将程序补充完整 import java.io.*;

34、public class CharInput public static void main(String args)throws java.io.IOException String s;inputStreamReader ir;BufferedReader in;ir=new_(System.io);in=new _(ir);while(s=in._)!=null)System.out.println(“Read;”+s);,InputStreamReader,BufferedReader,readLine(),4.下列程序实现从当前目录的date.ser文件中读取文件内容并显示出来.请将

35、程序补充完整.import java.io.*;import java.util.Date;public class UnSerializeDate UnSerializeDate()Date d=null;try FileInputStream f=new_(“date.ser”);ObjectInputStream s=new ObjectInputStream(_);d=(Date)s.readObject();f.close();,FileInputStream,f,catch(Exception e)e.printStackTrace();System.out.println(“Unserialized Date object from date.set”);System.out.println(“Date:”+d);public static void main(String args)new UnSerializeDate();,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号