Stringfilename=files[i].getName();
if(filename.trim().toLowerCase().endsWith(".java")){
System.out.print("files:
"+files[i].getName()+"\t");
}
if(files[i].isDirectory()){
Stringnn=files[i].getPath();
lisFile(nn);
}
}
}
}
流水号
答题时间
分数
内容号一
权重一
内容号二
权重二
050081
0
0
050600
10
0
0
内容号三
权重三
题型
难度
题类
外型
能力
0
0
5
5
0
0
0
区分度
关联题号1
关联题号2
相关码
等效试题号
空行
选项个数
0
0
0
0
0
23
0
签名
拟题人
初审人
复审人
定稿人
倪晓瑞
使用socket编写一个服务器端程序,服务器端程序在端口8888监听,如果它接到客户端发来的"hello"请求时会回应一个"hello",对客户端的其他请求不响应。
importjava.io.*;
import.*;
publicclassHelloServer
{
publicstaticvoidmain(Stringargs[])throwsIOException
{
ServerSocketserver=null;
server=newServerSocket(8888);
SocketClientSocket=null;
ClientSocket=server.accept();
Stringline;
BufferedReaderis=newBufferedReader(newInputStreamReader(ClientSocket.getInputStream()));
PrintWriteros=newPrintWriter(ClientSocket.getOutputStream());
while(true)
{
line=is.readLine();
if(line.equals("hello")){
os.println("hello");
os.flush();
}
}
}
流水号
答题时间
分数
内容号一
权重一
内容号二
权重二
050082
0
0
050100
10
0
0
内容号三
权重三
题型
难度
题类
外型
能力
0
0
5
5
0
0
0
区分度
关联题号1
关联题号2
相关码
等效试题号
空行
选项个数
0
0
0
0
0
40
0
签名
拟题人
初审人
复审人
定稿人
倪晓瑞
编写一个应用程序,完成文件的拷贝功能,文件名从命令行得到。
importjava.io.*;
classFileCopy
{
publicstaticvoidmain(String[]args)
{
FileInputStreamin;
FileOutputStreamout;
if(args.length<2)
{
System.out.println("Usage:
javacopysrcfiledestfile");
System.exit(-1);
}
try
{
in=newFileInputStream(args[0]);
out=newFileOutputStream(args[1]);
copyFile(in,out);
}
catch(Exceptione)
{
System.out.println(e);
}
}
privatestaticvoidcopyFile(FileInputStreamin,FileOutputStreamout)
{
intlength;
bytebuf[]=newbyte[1024];
try{
while((length=in.read(buf,0,1024))!
=-1)
{
out.write(buf,0,length);
}
}
catch(Exceptione)
{
System.out.println("Error:
"+e);
System.exit(-1);
}
}
}
流水号
答题时间
分数
内容号一
权重一
内容号二
权重二
050083
0
0
050100
10
0
0
内容号三
权重三
题型
难度
题类
外型
能力
0
0
5
5
0
0
0
区分度
关联题号1
关联题号2
相关码
等效试题号
空行
选项个数
0
0
0
0
0
43
0
签名
拟题人
初审人
复审人
定稿人
倪晓瑞
用输入/输出写一个程序,让用户输入一些姓名和电话号码。
每一个姓名和号码将加在文件里。
用户通过点"Done"按钮来告诉系统整个列表已输入完毕。
如果用户输入完整个列表,程序将创建一个输出文件并显示或打印出来。
格式如:
555-1212,Tom123-456-7890,PeggyL.234-5678,Marc234-5678,Ron876-4321,Beth&Brian33.1.42.45.70,Jean-Marc。
importjava.io.*;
classPhones
{
staticFileOutputStreamfos;
publicstaticfinalintlineLength=81;
publicstaticvoidmain(Stringargs[])throwsIOException
{
byte[]phone=newbyte[lineLength];
byte[]name=newbyte[lineLength];
intI;
try
{
fos=newFileOutputStream("phone.numbers");
}
catch(FileNotFoundExceptione)
{}
while(true)
{
System.err.println("Enteraname(enter'done'toquit)");
readLine(name);
if("done".equalsIgnoreCase(newString(name,0,0,4)))
{
break;
}
System.err.println("Enterthephonenumber");
readLine(phone);
for(inti=0;phone[i]!
=0;i++)
{fos.write(phone[i]);}
fos.write(',');
for(inti=0;name[i]!
=0;i++)
{fos.write(name[i]);}
fos.write('\n');
}
fos.close();
}
privatestaticvoidreadLine(byteline[])throwsIOException
{
inti=0,b=0;
while((i='\n'))
{ line[i++]=(byte)b;
}
line[i]=(byte)0;
}}
流水号
答题时间
分数
内容号一
权重一
内容号二
权重二
050084
0
0
050400
10
0
0
内容号三
权重三
题型
难度
题类
外型
能力
0
0
4
5
0
0
0
区分度
关联题号1
关联题号2
相关码
等效试题号
空行
选项个数
0
0
0
0
0
36
0
签名
拟题人
初审人
复审人
定稿人
倪晓瑞
设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1,写出程序。
publicclassThreadTest1{
privateintj;
publicstaticvoidmain(Stringargs[]){
ThreadTest1tt=newThreadTest1();
Incinc=tt.newInc();
Decdec=tt.newDec();
for(inti=0;i<2;i++){
Threadt=newThread(inc);
t.start();
t=newThread(dec);
t.start();
}
}
privatesynchronizedvoidinc(){
j++;
System.out.println(Thread.currentThread().getName()+"-inc:
"+j);
}
privatesynchronizedvoiddec(){
j--;
System.out.println(Thread.currentThread().getName()+"-dec:
"+j);
}
classIncimplementsRunnable{
publicvoidrun(){
for(inti=0;i<100;i++){
inc();
}
}
}
classDecimplementsRunnable{
publicvoidrun(){
for(inti=0;i<100;i++){
dec();
}
}
}
}
流水号
答题时间
分数
内容号一
权重一
内容号二
权重二
050085
0
0
050400
10
0
0
内容号三
权重三
题型
难度
题类
外型
能力
0
0
4
5
0
0
0
区分度
关联题号1
关联题号2
相关码
等效试题号
空行
选项个数
0
0
0
0
0
94
0
签名
拟题人
初审人
复审人
定稿人
倪晓瑞
编写应用类ConnectionManager,用来建立与指定数据库服务器的连接。
packagemyshop.db;
importjava.sql.*;
/**
*数据库连接控制类。
通过访问静态方法获得数据库连接
*/
publicabstractclassConnectionManager{
/**获得连接池连接*/
publicstaticfinalintTYPE_POOLED=0;
/**获得直接物理连接*/
publicstaticfinalintTYPE_DIRECT=1;
/**私有空构造方法。
保证本类不能够被实例化*/
privateConnectionManager(){
}
/**
*获得默认连接方式的数据库连接。
目前默认为物理连接。
*@returnConnection数据库连接
*/
publicstaticConnectiongetConnection(){
returngetConnection(TYPE_DIRECT);
}
/**
*获得指定连接方式的数据库连接
*@paramtypeint连接方式
*ConnectionManager.TYPE_POOLED数据库连接池连接
*ConnectionManager.TYPE_DIRECT直接物理连接
*@returnConnection数据库连接
*/
publicstaticConnectiongetConnection(inttype){
Connectionconn=null;
switch(type){
caseTYPE_DIRECT:
try{
//定义JDBC驱动程序
StringdriverName="org.gjt.mm.mysql.Driver";//MySQL驱动程序名称
Class.forName(driverName);
//数据库连接参数。
指定采用中文编码方式连接数据库
StringserverName="localhost";//数据库主机名称
Stringmydatabase="my_shop";//数据库名称
//连接url。
指定连接方式为GBK编码
Stringurl="jdbc:
mysql:
//"+serverName+"/"+
mydatabase+
"?
useUnicode=true&characterEncoding=GBK";
Stringusername="root";//连接用户名
Stringpassword="111111";//连接密码
//获得数据库连接
conn=DriverManager.getConnection(url,username,password);
}catch(ClassNotFoundExceptione){
//不能够找到所需类
e.printStackTrace();
}catch(SQLExceptione){
//获得数据库连接发生异常
e.printStackTrace();
}
break;
caseTYPE_POOLED:
thrownewUnsupportedOperationException("NowUnsupported.");
}
returnconn;
}
/**
*关闭数据库连接
*@paramconnConnection需要被关闭的数据库连接
*/
publicstaticvoidcloseConnection(Connectionconn){
if(conn!
=null){//连接是否有效
try{
if(!
conn.isClosed()){//连接是否已关闭
conn.close();//关闭连接
}
}catch(SQLExceptionex1){
ex1.printStackTrace();
}
}
}
/**
*主方法。
用于检测本类工作是否正常
*@paramargsString[]运行时参数
*/
publicstaticvoidmain(String[]args){
Connectionconn=null;
try{
conn=ConnectionManager.getConnection();
if(conn==null){
System.out.println("获得数据库连接错误.");
}else{
System.out.println("正常获得数据库连接=="+conn);
}
}catch(Exceptionex){
ex.printStackTrace();
}finally{
closeConnection(conn);
}
}
}