第6章 案例Word文档下载推荐.docx
- 文档编号:22240238
- 上传时间:2023-02-03
- 格式:DOCX
- 页数:15
- 大小:19.68KB
第6章 案例Word文档下载推荐.docx
《第6章 案例Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《第6章 案例Word文档下载推荐.docx(15页珍藏版)》请在冰豆网上搜索。
s2=newString(s);
//用串对象s创建一个新的字符串对象s2
s3=newString(charArray);
//用字符数组创建字符串对象s3
//用字符串数组中下标为6开始的3个字符创建字符串对象s4
s4=newString(charArray,6,3);
//用字符串数组byteArray按缺省的字符编码方案创建串对象s5
s5=newString(byteArray);
//从前面创建的字节型数组byteArray下标为2的字节开始,取连续的4个字节创建串对象
//s6,即取{-49,-14,-74,-44}
s6=newString(byteArray,2,4);
//构造一个新的字符串对象s7,其值为字符串buffer的当前内容
s7=newString(buffer);
System.out.println("
s1="
+s1);
s2="
+s2);
s3="
+s3);
s4="
+s4);
s5="
+s5);
s6="
+s6);
s7="
+s7);
+ss);
buffer="
+buffer);
}
/*运行结果如下:
s1=
s2=hello
s3=birthday
s4=day
s5=面向对象
s6=向对
s7=Welcomtojavaprogramming!
ss=ABC
buffer=Welcomtojavaprogramming!
*/
案例2:
//运行时需要输入参数的main方法。
publicclassC8_2
for(inti=0;
i<
args.length;
i++)
System.out.println(args[i]);
/*运行时输入“javaC7_7HelloWorldLet'
sJava!
”命令,则有如下的结果:
Hello
World
Let'
s
Java!
案例3:
//StringBuffer类的测试缓冲区长度的方法
publicclassC8_3
StringBufferbuf1=newStringBuffer();
//创建一个空字符串缓冲区
//指定初始长度为16创建一个空字符串缓冲区
StringBufferbuf2=newStringBuffer(10);
//用指定的"
串创建一个字符串缓冲区
StringBufferbuf3=newStringBuffer("
//返回当前字符串长度
intlen1=buf1.length();
intlen2=buf2.length();
intlen3=buf3.length();
//返回当前缓冲区长度
intle1=buf1.capacity();
intle2=buf2.capacity();
intle3=buf3.capacity();
//从buf3字符串中取下标为3的字符
charch=buf3.charAt(3);
//使用StringBuffer的toString方法将三个StringBuffer
//对象转换成String对象输出
buf1="
+buf1.toString());
buf2="
+buf2.toString());
buf3="
+buf3.toString());
len1="
+len1+"
\tlen2="
+len2+"
\tlen3="
+len3);
le1="
+le1+"
\tle2="
+le2+"
\tle3="
+le3);
ch="
+ch);
/*运行结果:
buf1=
buf2=
buf3=hello
len1=0len2=0len3=5
le1=16le2=10le3=21
ch=l
案例4:
//应用Math类的方法
publicclassC8_4
{publicstaticvoidmain(String[]args)
{doublea=2,b=3;
doublez1=Math.pow(a,b);
//引用Math类的pow方法求a的b次方
doublez2=Math.sqrt(9);
//引用Math类的sqrt方法求9的平方根
System.out.print("
z1="
+z1);
\tz2="
+z2);
}
//程序的运行结果为:
z1=8.0z2=3.0
案例5:
//catch的匹配按书写顺序,满足一个catch后不再管后面的catch。
classAnnoyanceextendsException{}
classSneezeextendsAnnoyance{}
publicclassC9_1{
publicstaticvoidmain(String[]args){
try{
thrownewSneeze();
}catch(Annoyancea){
System.out.println(“catchAnnoyance”);
}catch(Sneezes){
System.out.println(“catchSneeze”);
//输出结果为catchannoyance
案例6:
//异常处理中getMessage()、toString()、printStackTrace()3个方法的返回值
publicclassC9_2{
publicstaticvoidmain(String[]args){
try{thrownewException(“HereismyException”):
}catch(Exceptione){
System.out.println(“caughtException”);
System.out.println(“e.getMessage()”+e.getMessage());
System.out.println(“e.toString()”+e.toString());
System.out.println(“e.printStackTrace()”+e.e.printStackTrace());
/*运行结果:
caughexception
e.getMessage():
HereismyException
e.toString():
java.lang.Exception:
e.printStackTrace():
java.lang.Exception;
HereismyExceptionatExceptionMethods.main(ExceptionMethods.java:
7)
案例7:
//使用嵌套的try-catch-finally语句自行处理异常。
publicclassC9_3
staticinta,b,c;
publicstaticvoidmain(Stringargs[])
try
{a=10;
b=0;
{c=a/b;
a/b="
+c);
catch(IndexOutOfBoundsExceptionE)
{System.out.println("
捕捉超出索引异常..."
}
finally
嵌套内层的finally区块"
catch(ArithmeticExceptionE)
捕捉数学运算异常:
b="
+b);
嵌套外层的finally区块"
if(b==0)
程序执行发生异常!
"
else
程序正常执行完毕!
嵌套内层的finally区块
b=0
嵌套外层的finally区块
案例8:
//使用Throw语句抛出异常。
publicclassC9_4
publicstaticvoidmain(String[]args)
thrownewNullPointerException("
自编异常"
catch(NullPointerExceptione)
exception:
+e);
//运行结果:
Exception:
java.lang.NullPointerException:
自编异常
案例9:
//使用Throws语句抛出异常。
importjava.awt.*;
importjava.applet.*;
publicclassC9_5extendsApplet
{
staticvoidthrowOne()throwsIllegalAccessException
{thrownewIllegalAccessException("
publicvoidpaint(Graphicsg)
try{throwOne();
catch(IllegalAccessExceptione)
{g.drawString("
发生异常:
+e,20,20);
java.lang.IllegalAccessException:
自编异常
案例10:
//键盘输入数据的读取。
publicclassC9_6
intcount;
byteb[]=newbyte[256];
Stringstr;
DataInputStreamdin=newDataInputStream(System.in);
BufferedInputStreamin=newBufferedInputStream(din);
try{
if(in.markSupported())//判断当前输入流是否支持mark和reset方法
支持mark"
输入字符串,按Enter结束"
in.mark(256);
//在输入流的当前位置上设置标记,并保留256位
//读键盘输入的数据存入b数组,该方法返回实际读入的字节数给count
count=in.read(b);
读入字符数:
+count);
str=newString(b,0,count);
//将b数组转换为字符串
输入的字符串为:
+str);
in.reset();
//重新回到标记处读取数据
in.read(b,0,2);
//读前两个字符
str=newString(b,0,2);
字符串的前两个:
in.skip(count/2);
in.read(b,0,count/2);
str=newString(b,0,count/2);
字符串的后半段:
不支持mark"
din.close();
in.close();
catch(IOExceptionE)
发生I/O错误!
支持mark
输入字符串,按Enter结束123456789
11
123456789
12
6789
案例11:
//从键盘读入字符串写入屏幕。
publicclassC9_7
{intcount;
byteb[]=newbyte[256];
BufferedInputStreamin=newBufferedInputStream(System.in);
//键盘读入
DataOutputStreamstdout=newDataOutputStream(System.out);
//屏幕输出
BufferedOutputStreamout=newBufferedOutputStream(stdout);
PrintStreamp=newPrintStream(System.out);
//屏幕输出
{if(in.markSupported())
p.println("
//使用PrintStream输出到屏幕
p.print("
输入字符串,请按Enter结束"
//将数组b[]中从0位置开始的count长度的字节写到缓冲流缓冲区中
out.write(b,0,count);
out.flush();
//将缓冲流缓冲区中的数据输出到屏幕上
p.close();
stdout.close();
out.close();
输入字符串,请按Enter结束123456789
案例12:
/*利用InputStreamReader类、BufferedReader类、OutputStreamWriter类实现从键盘输入字符串,再输出到屏幕上。
利用CharArrayReader类、CharArrayWriter类实现存储器读/写操作。
publicclassC9_8
charc1[],c2[],temp;
CharArrayReadercin;
CharArrayWritercout;
InputStreamReadersin=newInputStreamReader(System.in);
//键盘输入
BufferedReaderin=newBufferedReader(sin);
OutputStreamWriterout=newOutputStreamWriter(System.out);
{System.out.print("
请输入一个字符串,请按Enter结束"
str=in.readLine();
//读入字符串
c1=str.toCharArray();
//将字符串转换成字符数组
cin=newCharArrayReader(c1);
cout=newCharArrayWriter();
while(cin.ready())//写入c1的内容到CharArrayWriter中
{cout.write(cin.read());
c2="
c2=cout.toCharArray();
//将CharArrayWriter的内容输出到c2中
System.out.print(newString(c2));
\n直接由CharArray输出:
cout.writeTo(out);
//输出到OutputStreamWriter
//OutputStreamWriter强制输出到屏幕
I/O错误!
请输入一个字符串,请按Enter结束12345abc
c2=12345abc
直接由CharArray输出:
12345abc
案例13:
//获取文件的文件名、长度、大小等特性。
importjava.util.*;
publicclassC9_9
{publicstaticvoidmain(Stringargs[])
StringPath;
InputStreamReaderdin=newInputStreamReader(System.in);
BufferedReaderin=newBufferedReader(din);
请输入相对或绝对路径:
Path=in.readLine();
//读取输入
Filef=newFile(Path);
路径:
+f.getParent());
档案:
+f.getName());
绝对路径:
+f.getAbsolutePath());
文件大小:
+f.length());
是否为文件:
+(f.isFile()?
是"
:
否"
));
是否为目录:
+(f.isDirectory()?
是否为隐藏:
+(f.isHidden()?
是否可读取:
+(f.canRead()?
是否可写入:
+(f.canWrite()?
最后修改时间:
+newDate(f.lastModified()));
C9_9.java
null
f:
\javapro\C9_9.java
1461
是
否
MonMay0116:
41:
44CMT+08:
002006
案例14:
/*直接利用FileInputStream类和FileOutputStream类完成从键盘读入数据写入文件中,
再从写入的文件中读出数据打印到屏幕上的操作。
publicclassC9_10
charc;
intc1;
FilefilePath=newFile("
temp"
//在当前目录下建目录,也可用绝对目录
if(!
filePath.exists())filePath.mkdir();
//若目录不存在,则建之
Filefl=newFile(filePath,"
d1.txt"
//在指定目录下建文件类对象
FileOutputStreamfout=newFileOutputStream(fl);
Sy
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 第6章 案例