(完整word版)Java-2实用教程(第5版)习题解答

发布时间 : 星期四 文章(完整word版)Java-2实用教程(第5版)习题解答更新完毕开始阅读

}

catch(IOException e){}

1.C。2.B。 三、阅读程序 1.【代码1】:51。【代码2】:0。 2.【代码1】:3。【代码2】:abc。【代码3】:1。【代码4】:dbc。 3.上机实习题,解答略。 四、编程题

1.文件E.java的长度是51个字节,请说出E类中标注的【代码1】,【代码2】的输出结果。

import java.io.*; public class E {

public static void main(String args[]) { File f = new File(\

try{ RandomAccessFile in = new RandomAccessFile(f,\ System.out.println(f.length()); //【代码1】 FileOutputStream out = new FileOutputStream(f); System.out.println(f.length()); //【代码2】 }

catch(IOException e) {

System.out.println(\ } } }

2.请说出E类中标注的【代码1】~【代码4】的输出结果。 import java.io.*; public class E {

public static void main(String args[]) { int n=-1;

File f =new File(\ byte [] a=\

try{ FileOutputStream out=new FileOutputStream(f); out.write(a); out.close();

FileInputStream in=new FileInputStream(f); byte [] tom= new byte[3]; int m = in.read(tom,0,3);

System.out.println(m); //【代码1】 String s=new String(tom,0,3);

System.out.println(s); //【代码2】

m = in.read(tom,0,3);

System.out.println(m); //【代码3】 s=new String(tom,0,3);

System.out.println(s); //【代码4】 }

catch(IOException e) {} } }

3.了解打印流。我们已经学习了数据流,其特点是用Java的数据类型读写文件,但使用数据流写成的文件用其它文件阅读器无法进行阅读(看上去是乱码)。PrintStream类提供了一个过滤输出流,该输出流能以文本格式显示Java的数据类型。上机实习下列程序: import java.io.*; public class E {

public static void main(String args[]) { try{ File file=new File(\

FileOutputStream out=new FileOutputStream(file); PrintStream ps=new PrintStream(out); ps.print(12345.6789); ps.println(\ ps.println(true); ps.close(); }

catch(IOException e){} } }

四、编写程序

1.使用RandomAccessFile流将一个文本文件倒置读出。

2.使用Java的输入、输出流将一个文本文件的内容按行读出,每读出一行就顺序添加行号,并写入到另一个文件中。

3.参考例子16,解析一个文件中的价格数据,并计算平均价格,比如该文件的内容如下: 商品列表:

电视机,2567元/台 洗衣机,3562元/台 冰箱,6573元/台

1. import java.io.*;

public class E {

public static void main(String args[]) { File f=new File(\

try{ RandomAccessFile random=new RandomAccessFile(f,\ random.seek(0); long m=random.length(); while(m>=0) {

m=m-1; random.seek(m); int c=random.readByte(); if(c<=255&&c>=0) System.out.print((char)c); else { m=m-1; random.seek(m); byte cc[]=new byte[2]; random.readFully(cc);

System.out.print(new String(cc)); } } }

catch(Exception exp){} } }

2. import java.io.*;

public class E {

public static void main(String args[ ]) { File file=new File(\ File tempFile=new File(\

try{ FileReader inOne=new FileReader(file);

BufferedReader inTwo= new BufferedReader(inOne); FileWriter tofile=new FileWriter(tempFile); BufferedWriter out= new BufferedWriter(tofile); String s=null; int i=0;

s=inTwo.readLine(); while(s!=null) { i++;

out.write(i+\ out.newLine(); s=inTwo.readLine(); }

inOne.close(); inTwo.close(); out.flush(); out.close(); tofile.close(); }

catch(IOException e){} } }

3. import java.io.*;

import java.util.*; public class E {

public static void main(String args[]) { File file = new File(\ Scanner sc = null; double sum=0; int count = 0;

try { sc = new Scanner(file);

sc.useDelimiter(\ while(sc.hasNext()){

try{ double price = sc.nextDouble(); count++; sum = sum+price; System.out.println(price); }

catch(InputMismatchException exp){ String t = sc.next(); } }

System.out.println(\平均价格:\ }

catch(Exception exp){ System.out.println(exp); } } }

习题11(第11章)

一、问答题

1.问答题

(1)怎样启动Mysql数据库服务器? 1.在MySQL安装目录的bin子目录下键入mysqld或mysqld -nt 启动MySQL数据库服务器。

(2)JDBC-MySQL数据库驱动的jar文件因该拷贝到哪个目录中? (3)预处理语句的好处是什么?

3.减轻数据库内部SQL语句解释器的负担。

联系合同范文客服:xxxxx#qq.com(#替换为@)