第十章
8.定义一个复数类并按照复数的实部大小对复数对象进行排序
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
public class complex {
public int real;
public int virtual;
public complex(int real,int virtual) {
this.real=real;
this.virtual=virtual;
}
public String toString() {
return String.valueOf(real)+" +"+String.valueOf(virtual)+"i";
}
public static void main(String[] args) {
ArrayList<complex> list=new ArrayList<complex>();
complex a=new complex(3,4);
complex b=new complex(-1, 5);
complex c=new complex(2, 1);
list.add(a);list.add(b);list.add(c);
System.out.println(list);
Collections.sort(list, new Comparator<complex>() {
public int compare(complex c1,complex c2) {
return c1.real-c2.real;
}
});
System.out.println(list);
}
}
输出演示
[3 +4i, -1 +5i, 2 +1i]
[-1 +5i, 2 +1i, 3 +4i]
10.对第七章第六题进行改造,讲异常类型与中文提示储存在一种集合类中,从中实现相应的功能。
import java.util.HashMap;
class ExceptionMap{
HashMap<Class, String> em=new HashMap<Class, String>();
public ExceptionMap() {
em.put(NullPointerException.class, "空指针异常");
em.put(ArithmeticException.class,"除数为零异常");
}
}
public class ExceptionMapTest {
public static void main(String[] args) {
ExceptionMap mm=new ExceptionMap();
try {
String aString=null;
System.out.println(aString.charAt(0));
}catch(NullPointerException e) {
System.out.println(mm.em.get(e.getClass()));
}
try {
int d=0;
int t=2;
System.out.println(t/d);
}catch (ArithmeticException e) {
// TODO: handle exception
System.out.println(mm.em.get(e.getClass()));
}
}
}
输出演示
空指针异常
除数为零异常
第十四章
3.完成下面方法中的代码,要求建立一个缓冲区,将字节输入流中的内容转化为字符串.
//题目
static String loadStream(InputStream in)throws IOException{
}
//完成后
import java.io.*;
public class buffertest {
static String loadStream(InputStream in)throws IOException{
byte buffer[]=new byte[1024];
in.read(buffer);
String m=new String(buffer);
return m;
}
public static void main(String[] args) {
try {
String mString=loadStream(System.in);
System.out.println(mString);
}catch (Exception e) {
// TODO: handle exception
}
}
}
输出演示
你好//输入你好
你好//输出你好
4.编写程序,将一个字符串转化为字节数组输入流,将这个流中所有小写字母换成大写字母并写入字节数组输出流中,在将数组输出流转为字符串。
import java.io.*;
public class buffertest {
public static void main(String[] args) {
byte[] b="hello".getBytes();
ByteArrayInputStream bais=new ByteArrayInputStream(b);
int n=0;
ByteArrayOutputStream baos=new ByteArrayOutputStream();
while((n=bais.read())!=-1) {
if(n>=97&&n<=122) {
n-=32;
}
baos.write(n);
}
System.out.println(baos.toString());
}
}
输出演示
HELLO
5.完成下面方法中的代码,方法中的参数为一个文本文件的名称,要求将文件中的内容转为字符串
import java.io.*;
public class buffertest {
static String loadFile(String filename){
String t="";
try {
FileInputStream fis=new FileInputStream(filename);
byte buffer[]=new byte[1024];
int c=0;
while((c=fis.read(buffer,0,1024))!=-1) {
t=t+new String(buffer,0,c)+"\n";
}
} catch (Exception e) {
// TODO: handle exception
}
return t;
}
public static void main(String[] args) {
try {
String mString=loadFile("C:\\Users\\Witiy\\Desktop\\test.txt");
System.out.println(mString);
}catch (Exception e) {
// TODO: handle exception
}
}
}
输出示例
boom//该文件内容为boom
6.完成下面方法中的代码,将字符串contents中的内容写入文件filename中
import java.io.*;
public class buffertest {
static boolean saveFile(String filename,String contents){
try {
FileOutputStream fos=new FileOutputStream(filename);
byte buffer[]=new byte[1024];
buffer=contents.getBytes();
int c=0;
fos.write(buffer,0,buffer.length);
fos.close();
} catch (Exception e) {
return false;
}
return true;
}
static String loadFile(String filename){
String t="";
try {
FileInputStream fis=new FileInputStream(filename);
byte buffer[]=new byte[1024];
int c=0;
while((c=fis.read(buffer,0,1024))!=-1) {
t=t+new String(buffer,0,c)+"\n";
}
} catch (Exception e) {
// TODO: handle exception
}
return t;
}
public static void main(String[] args) {
try {
System.out.println(loadFile("C:\\Users\\Witiy\\Desktop\\test.txt"));
saveFile("C:\\Users\\Witiy\\Desktop\\test.txt", "test");
System.out.println(loadFile("C:\\Users\\Witiy\\Desktop\\test.txt"));
}catch (Exception e) {
// TODO: handle exception
}
}
}
输出示例
boom //test.txt一开始内容为boom
test//后来改写为test
7.socket套接字有一个方法getInputStream(),其含义是得到从网络上传过来的数据流,现要求编写一段程序,将接收的数据存入文件
import java.io.*;
import java.net.Socket;
public class buffertest {
public static void main(String[] args) {
Socket t=new Socket();
try {
DataInputStream dis=new DataInputStream(t.getInputStream());
byte[] bytes=new byte[1024];
dis.read(bytes);
String filename="test.txt";
FileOutputStream fos=new FileOutputStream(filename);
fos.write(bytes);
} catch (Exception e) {
// TODO: handle exception
}
}
}
8.编写程序实现文件查找功能,提供两个参数,第一个参数为查找的起始路径,第二个参数为要查找的文件名称。如果找到,则给出文件的完整路径,否则提示文件不存在。
import java.io.*;
public class buffertest {
static String findfile(String beginpath,String filename) {
File test=new File(beginpath);
String result=null;
if(test.isFile()&&test.getName().equals(filename))
return beginpath;
else if(test.isDirectory()){
File[] files=test.listFiles();
for(File t:files) {
System.out.println(t.getName());
result= findfile(t.getAbsolutePath(), filename);
if(result!=null)
return result;
}
}
return result;
}
public static void main(String[] args) {
try {
String filename="test.txt";
String pathString;
if((pathString=findfile("C:\\Users\\Witiy\\Desktop", filename))!=null)
System.out.println(pathString);
else {
System.out.println("文件不存在");
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
输出示例
C:\Users\Witiy\Desktop\test.txt
6.利用串行化技术和socket通信,将一个客户端构造的对象传送到服务器,并输出对象的属性值
//************************服务器端***********************
import java.net.*;
import java.io.*;
public class Server {
public static void main(String[] args) throws ClassNotFoundException, IOException {
ServerSocket server;
Socket socket = null;
try {
server = new ServerSocket(4700);
socket = server.accept();
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
test t=(test)in.readObject();
System.out.println(t.t1+" "+t.t2);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//********************客户端*********************
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;
class test implements Serializable{
public String t1;
public String t2;
public test(String t1,String t2) {
// TODO Auto-generated constructor stub
this.t1=t1;this.t2=t2;
}
}
public class client {
public static void main(String[] args) {
try {
Socket socket=new Socket("127.0.0.1", 4700);
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
test t=new test("test", "succeed");
oos.writeObject(t);
oos.flush();
oos.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
输出示例
test succeed
第十五章
利用URLConnection对象编写程序返回某网站的首页,并将首页内容存放到文件当中。
import java.net.*;
import java.io.*;
public class URLtest {
public static void main(String[] args) {
try {
URL url=new URL("http://www.baidu.com/");
URLConnection con=url.openConnection();
con.setDoOutput(true);
BufferedReader in=new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
String lineString;
FileOutputStream fos=new FileOutputStream("url.txt");
while((lineString=in.readLine())!=null) {
fos.write(lineString.getBytes());
fos.flush();
}
fos.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
第十六章
1.使用jdbc来操作数据库,通常包括以下几个步骤:
1)下载、载入JDBC driver
2)在客户程序与数据库之间建立联系
3)通过数据库连接将sql语句从java程序传到数据库
4)通过数据库返回的记录集得到所需的数据
5)如需要,在用修改后的数据更新数据库
6)操作结束,关闭数据库
2.JDBC驱动有三种,一种为将JDBC调用转换为数据库直接使用的网络协议,这种类型的JDBC驱动不需要安装数据库客户端软件,它使用java socket来连接数据库,驱动的实现由各数据库厂商提供;第二种为需要安装数据库客户端的驱动,也同样由数据库厂商提供;第三种为需要安装数据库客户端并且配置ODBC数据源,sun公司负责实现驱动。对于类型三,只需要用下列代码即可载入
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
对于类型一与二,首先要从数据库厂商哪里获取相应数据库的jdbc driver类库,再载入相应的驱动类,例如:
Class.forName("oracle.jdbc.driver.OracleDriver");
3.Connection是与数据库的连接对象,Statement是用Connection接口所引用对象的createStatement方法得到一个实现Statement接口的对象,ResultSet是执行Statement.executeQuery方法后返回的查询集。
4.数据库使用完毕后,需要依次关闭ResultSet、Statement、Connection所引用的对象。