}
System.out.println( e ); System.exit( -2 );
} }
9.编写应用程序,使用RandomAccessFile类及其方法,把程序本身分两次显示在屏幕上。第一次直接显示,第二次给每一行添加的行号显示。 import java.io.*; public class Test { }
public static void main( String[ ] args ) throws IOException { RandomAccessFile randomfile = new RandomAccessFile(\ String s; }
while((s=randomfile.readLine())!=null) { System.out.println(s); }
long filePointer=0,fileLength=randomfile.length();
randomfile.seek(0); //文件指针已到文件尾,将文件指针重设到文件开始位置 int i=1;
while(filePointer s=randomfile.readLine(); System.out.println((i++)+\ \filePointer=randomfile.getFilePointer(); randomfile.close(); 10.在程序所在的目录下有子目录b,目录b下有文本文件testb.txt。编写应用程序,创建文件对象: File file=new File(\ 通过文件对象file得到它的文件名、相对路径、绝对路径、父目录名。 import java.io.*; public class Class1 { public static void main( String args[ ] ) { } File file=new File(\ System.out.println(\文件名:\System.out.println(\相对路径:\System.out.println(\绝对路径:\System.out.println(\父目录名:\ } 11.请编写一个名为Class1.java的Application,其功能为:测验文件Class1.java是否存在并输出其长度。 import java.io.*; public class Class1 { public static void main (String[] args) { } File f1=new File(\ System.out.println(\System.out.println(\ } 12.编写应用程序:创建目录c:\\temp,并创建一个文件2.txt,向该文件中写入字符串\came.\共5次。 import java.io.*; public class Class1 { public static void main(String args[]) throws IOException { File file1=new File(\ file1.mkdir(); File file2=new File(file1,\ FileOutputStream out1=new FileOutputStream(file2); String s=\ byte[] b=s.getBytes(); for(int i=0;i<5;i++) { out1.write(b,0,b.length); } } } 13.过滤文件并显示。 import java.io.*; public class Class1 { public static void main( String[ ] args ) { try { File oDir = new File( \ String[ ] strList; //如果没有命令行参数就取当前目录下的所有文件列表 if( 0 == args.length ) strList = oDir.list( ); else strList = oDir.list( new DirFilter(args[0])); for( int i = 0; i < strList.length; i ++ ) System.out.println( strList[ i ] ); } catch( Exception e ) } } { } System.out.println( e ); class DirFilter implements FilenameFilter { String strPick; DirFilter( String pickname ) { this.strPick = pickname; } public boolean accept( File dir, String name ) { } return name.endsWith( strPick ); } 14.编写java应用程序,使用FileReader类对象读取程序本身(或其他目录下的文件)并显示在屏幕上。 import java.io.*;//读取程序本身,显示在屏幕上 public class Class1 { public static void main (String[] args) { } try { FileReader fis=new FileReader(\ //FileReader fis=new FileReader(\int n; while((n=fis.read())!=-1) System.out.print((char)n); fis.close(); } catch(IOException e) { } System.out.println(e.toString()); } 15.编写应用程序,创建BufferedReader的对象,从某个文本文件中的字符输入数据流中读取一行字符(该文件与程序在同一目录下),跳过10个字节后将其显示出来。 import java.io.*; public class Class1 { public static void main(String args[]) throws IOException { BufferedReader b_reader=new BufferedReader(new FileReader(\ String str; } } b_reader.skip(10); str=b_reader.readLine(); System.out.println(str); 三、图形图像、常用组件和事件处理、布局 练习10(6) 掌握:(1)绘制字符串、字符、字节(2)字体、字型、字号的使用(3)颜色的设置 1. 请编写一个Applet,其功能为:在坐标(20,30)处以绿色显示“我喜欢绿色。 ” ,在坐标(20,60)处以蓝色显示“我也喜欢蓝色。” 。 @程序 import java.awt.*; import java.applet.*; public class Applet1 extends Applet { public void paint( Graphics g) { } g.setColor(Color. green); g.drawString(\我喜欢绿色。\g.setColor(Color.blue); g.drawString(\我也喜欢蓝色。\ } 2. 请编写一个Applet,分别以红、绿、黄、粉、白、蓝等六种颜色,在同一行上显示六个数字 1、2、3、4、5、6。 @程序 import java.applet.*; import java.awt.*; public class Applet1 extends Applet { public void paint(Graphics g) { int j=0; Color color[ ]=new Color[6]; color[0]=Color.red; color[1]=Color.green; color[2]=Color.yellow; color[3]= Color.pink; color[4]=Color.white; color[5]=Color.blue; for(char ch='1'; ch <= '6'; ch++) { g.setColor( color[ch-'1']); g.drawString(\ } } } j+=20; 3. 请编写一个Applet,以不同的灰度值在一行上显示数字 0 - 9 。 @程序 import java.applet.*; import java.awt.*; public class Applet1 extends Applet { public void paint(Graphics g) { int j=0; int red=0, green=0,blue=0; } for(char ch='0'; ch <= '9'; ch++) { } g.setColor( new Color(red,green,blue)); g.drawString(\j+=20; red+=85; green+=85; blue+=85; } 4. 编写Java小程序,将字符串“I like java!”重复显示5次,每次显示在1行上。要求显示字体为\字体,字体风格为斜体,第1行字符串的字体大小是15,后面的每一行的字体大小依次增加5,每行的间隔为30像素。 @程序 import java.awt.*; import java.applet.*; public class Applet1 extends Applet { } Font f1; int size=15; int y=30; String str=\public void paint(Graphics g) { } for(int i=0;i<5;i++) { } f1=new Font(\g.setFont(f1); g.drawString(str,30,y); size+=5; y+=30;