实验一Java常用工具类编程Word文件下载.docx
《实验一Java常用工具类编程Word文件下载.docx》由会员分享,可在线阅读,更多相关《实验一Java常用工具类编程Word文件下载.docx(19页珍藏版)》请在冰豆网上搜索。
,"
Strings2="
c++.java.vb"
c++.java.vbdotnet"
String[]str=s2.split("
\\.|"
for(inti=0;
i<
str.length;
i++)
System.out.println(str[i]);
2、StringBuffer类
StringBuffer
publicStringBuffer()
构造一个不包含字符的字符串缓冲区,其初始的容量设为16个字符。
publicStringBuffer(intlength):
构造一个不包含字符的字符串缓冲区,其初始的容量由参数length设置。
publicStringBuffer(Stringstr):
构造一个字符串缓冲区,来表示和字符串参数相同的字符序列。
length
publicintlength():
返回字符串缓冲区的长度(字符数)。
capacity
publicintcapacity():
返回字符串缓冲区的当前容量。
该容量表示可用于插入新的字符的存储空间;
超出该容量时会发生新的容量分配。
ensureCapacity
publicsynchronizedvoidensureCapacity(intminimumCapacity):
保证缓冲区的容量至少等于指定的最小数。
如果字符串缓冲区的当前容量少于该参数,则分配一个新的更大的内部缓冲区。
新容量将取如下参数中较大的一个:
setLength
publicsynchronizedvoidsetLength(intnewLength):
设置该字符串缓冲区的长度。
如果参数newLength小于该字符串缓冲区的当前长度。
该字符串缓冲区将被截断来包含恰好等于由参数newLength给出的字符数。
append
publicsynchronizedStringBufferappend(Objectobj):
把Object型参数的字符串表示添加到该字符串缓冲区。
StringBufferx=newStringBuffer().append("
a"
).append(4).append("
c"
).toString();
insert
publicsynchronizedStringBufferinsert(intoffset,Objectobj):
把Object型参数的字符串表示插入到字符串缓冲区。
reverse
publicsynchronizedStringBufferreverse():
该字符串缓冲区的字符序列被其反向字符序列所替换。
toString
publicStringtoString():
转换为一个表示该字符串缓冲区数据的字符串。
分配一个新的String对象,并且用字符串缓冲区所表示的字符序列进行初始化。
于是此String被返回。
随后缓冲区发生的变化不再影响该String的内容。
3、日期类示例
1)获取服务器端当前日期:
importjava.util.Date;
DatemyDate=newDate();
2)获取当前年、月、日:
intthisYear=myDate.getYear()+1900;
//thisYear=2009
intthisMonth=myDate.getMonth()+1;
//thisMonth=10
intthisDate=myDate.getDate();
//thisDate=30
3)按本地时区输出当前日期
out.println(myDate.toLocaleString());
输出结果为:
2003-5-30
4)按照指定格式打印日期
import"
java.util.Date"
java.text.DateFormat"
DatedNow=newDate();
SimpleDateFormatformatter=
newSimpleDateFormat("
Eyyyy.MM.dd'
at'
hh:
mm:
ssazzz"
System.out.println("
Itis"
+formatter.format(dNow));
输出的结果为:
Itis星期五2003.05.30at11:
30:
46上午CST
5)将字符串转换为日期
import="
Stringinput="
1222-11-11"
SimpleDateFormatformatter=newSimpleDateFormat("
yyyy-MM-dd"
Datet=null;
try{
t=formatter.parse(input);
System.out.println(t);
}catch(ParseExceptione){
System.out.println("
unparseableusing"
+formatter);
}
FriNov1100:
00:
00CST1222
6)计算日期之间的间隔getTime()函数返回日期与1900-01-0100:
00相差的毫秒数
2003-05-01"
Dated1=null;
Dated2=newDate();
longdiff=d2.getTime()-d1.getTime();
out.println("
Differenceis"
+(diff/(1000*60*60*24))+"
days."
Differenceis29days.
7)日期的加减运算
方法:
用Calendar类的add()方法
Calendarnow=Calendar.getInstance();
Itisnow"
+formatter.format(now.getTime()));
now.add(Calendar.DAY_OF_YEAR,-(365*2));
<
br>
"
Twoyearsagowas"
Itisnow星期五2003.05.30at01:
45:
32下午CST
Twoyearsagowas星期三2001.05.30at01:
8)比较日期
用equals()、before()、after()方法
java.util.*"
java.text.*"
DateFormatdf=newSimpleDateFormat("
yyy-MM-dd"
Dated1=df.parse("
2000-01-01"
Dated2=df.parse("
1999-12-31"
Stringrelation=null;
if(d1.equals(d2))
relation="
thesamedateas"
elseif(d1.before(d2))
before"
else
after"
System.out.println(d1+"
is"
+relation+'
'
+d2);
SatJan0100:
00CST2000isafterFriDec3100:
00CST1999
1.2实验题目
1、使用类String类的分割split将字符串“SolutionstoselectedexercisescanbefoundintheelectronicdocumentTheThinkinginJavaAnnotatedSolutionGuide,availableforasmallfeefromBruceEckel”单词提取输出。
单词以空格或,分割。
packagejob1;
publicclassSplit{
publicstaticvoidmain(String[]args){
Strings1="
Solutionstoselectedexercises"
+
canbefoundintheelectronicdocument"
TheThinkinginJavaAnnotatedSolutionGuide,"
availableforasmallfeefromBruceEckel"
Strings2[]=s1.split("
|,"
for(inti=0;
s2.length;
System.out.println(s2[i]);
}
2、设计一个类Student,类的属性有:
姓名,学号,出生日期,性别,所在系等。
并生成学生类对象数组。
按照学生的姓名将学生排序输出。
使用String类的compareTo方法。
publicclassStudent{
privateStringname;
privateintnum;
privateStringbirth;
privateStringsex;
privateStringsdept;
publicStudent(){
publicStudent(Stringname,intnum,Stringbirth,Stringsex,Stringsdepet){
this.name=name;
this.num=num;
this.birth=birth;
this.sex=sex;
this.sdept=sdept;
publicStringgetName(){
returnname;
publicvoidsetName(Stringname){
publicintgetNum(){
returnnum;
publicvoidsetNum(intnum){
publicStringgetBirth(){
returnbirth;
publicvoidsetBirth(Stringbirth){
publicStringgetSex(){
returnsex;
publicvoidsetSex(Stringsex){
publicStringgetSdept(){
returnsdept;
publicvoidsetSdept(Stringsdept){
importjava.util.Comparator;
importjava.text.CollationKey;
importjava.text.Collator;
publicclassMyCmpimplementsComparator{
publicintcompare(Objectobj1,Objectobj2){
Collatorcollator=Collator.getInstance();
CollationKeykey1=collator.getCollationKey(obj1.toString());
CollationKeykey2=collator.getCollationKey(obj2.toString());
returnpareTo(key2);
importjava.util.*;
publicclassTreeText{
TreeMapmap=newTreeMap(newMyCmp());
map.put("
蒋高登"
200818511990-08-03男CS"
蒋稀文"
200818521989-01-11男CS"
董骏"
200818501990-04-05男CS"
Setkeys=map.entrySet();
Iteratorit=keys.iterator();
while(it.hasNext()){
Map.Entrye=(Map.Entry)it.next();
System.out.println(e.getKey()+"
"
+e.getValue());
}
3、设计一个程序计算2011-05-01日与系统当前日期相差的天数。
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.Date;
publicclassDays{
publicstaticvoidmain(String[]args)throwsParseException{
Dated1=newDate();
SimpleDateFormatformat=newSimpleDateFormat("
Strings="
2011-05-01"
Dated2=format.parse(s);
intdays=(int)((d2.getTime()-d1.getTime())/(1000*60*60*24));
System.out.print("
2011-05-01日与系统当前时间相差"
+days+"
天"
4、使用日历类等相关方法按截图做出一个日历参照书本示例,研究其中代码回顾与复习利用JavaSwing编程。
参考:
以下函数根据输入的年和月计算相应的数字
publicvoidshowCalendar(intyear,intmonth){
Calendarcal=Calendar.getInstance();
cal.set(Calendar.YEAR,year);
cal.set(Calendar.MONTH,month-1);
//计算当前月一共有多少天
intdays=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//计算当前月的1号为星期几
cal.set(Calendar.DAY_OF_MONTH,1);
//设置为1号
intfirstweek=cal.get(Calendar.DAY_OF_WEEK);
。
。
importjava.awt.BorderLayout;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.util.Calendar;
importjavax.swing.*;
importjavax.swing.table.DefaultTableModel;
publicclassCalendarTableextendsJFrameimplementsActionListener{
privateJLabellabel_year=newJLabel("
年"
privateJLabellabel_month=newJLabel("
月"
privateJComboBoxbox_year=newJComboBox();
privateJComboBoxbox_month=newJComboBox();
privateJButtonbtn=newJButton("
确定"
privateJTabletable;
privateStringweek[]={"
日"
一"
二"
三"
四"
五"
六"
};
privateStringrows[][]=newString[6][7];
publicCalendarTable(){
JPaneljp_north=newJPanel(newGridLayout(1,5));
for(inti=1;
13;
box_month.addItem(i);
jp_north.add(box_year);
jp_north.add(label_year);
jp_north.add(box_month);
jp_north.add(label_month);
jp_north.add(btn);
table=newJTable(rows,week);
JScrollPanescroll=newJScrollPane(table);
this.add(jp_north,BorderLayout.NORTH);
this.add(scroll);
calendarinit();
btn.addActionListener(this);
box_year.addActionListener(this);
box_month.addActionListener(this);
publicvoidactionPerformed(ActionEvente){
Calendarcal=Calendar.getInstance();
intyear=cal.get(Calendar.YEAR);
//box_year.getSelectedIndex();
intmonth=box_month.getSelectedIndex();
showCalendar(year,month);
table.setModel(newDefaultTableModel(rows,week));
privatevoidcalendarinit(){
box_year.addItem(newInteger(cal.get(Calendar.YEAR)));
box_month.setSelectedIndex(cal.get(Calendar.MONTH));
showCalendar(Calendar.YEAR,Calendar.MONTH);
privatevoidshowCalendar(intyear,intmonth){
cal.set(Calendar.YEAR,year);
cal.set(Calendar.MONTH,month);
intdays=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
intfirstweek=cal.get(Calendar.DAY_OF_WEEK);
for(inti=0;
rows.length;
i++){
for(intj=0;
j<
rows[i].length;
j++){
rows[i][j]="
}
intline=0;
days+firstweek-1;
if(i%7==0){
line++;
if(firstweek==1){
rows[0][0]="
1"
if(i>
=firstweek-1){
rows[line][i%7]=(i-firstweek+2)+"
publicstaticvoidmain(Strin