点石科技整理(Http://www.loosr.cn) sanjtu@163.com
(二)使用文件输入输出流的应用程序
1. 程序功能:将保存在本地机当前文件夹中的KY10_2.HTML 文本文件的内容在屏幕上显
示出来,然后将其另存为KY10_2.txt 文件。 2. 编写KY10_2.java 程序文件,源代码如下
import java.io.*; public class KY5_4 {
public static void main(String[] args) throws IOException { FileReader in=new FileReader(\建立文件输入流 BufferedReader bin=new BufferedReader(in);//建立缓冲输入流 FileWriter out=new FileWriter(\建立文件输出流 String str;
while ((str=bin.readLine())!=null) {
//将缓冲区内容通过循环方式逐行赋值给字符串str System.out.println(str);//在屏幕上显示字符串str
out.write(str+\将字符串str 通过输出流写入KY5_1.txt 中 }
in.close(); out.close(); } }
3. 编译、运行程序
(三)转换流
1.程序功能:从键盘读入一行字符,并将其转换成大写打印在屏幕,当输入exit字符串时退出。
2. 编写KY10_3.java 程序文件,源代码如下
import java.io.*; public class KY10_3 {
public static void main(String args[]) { InputStreamReader isr =
new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = null; try {
s = br.readLine(); while(s!=null){
if(s.equalsIgnoreCase(\ System.out.println(s.toUpperCase()); s = br.readLine(); }
br.close();
成功不是偶然 失败不是命运 sanjtu
点石科技整理(Http://www.loosr.cn) sanjtu@163.com
} catch (IOException e) { e.printStackTrace(); } } }
3. 编译、运行程序
(四)使用数据输入输出流与文件输入输出流类的应用程序
使用数据输入流DataOutputStream 和数据输出流DataInputStream 可以读取或写入任何Java 类型的数据,不用关心它们的实际长度是多少字节。一般与文件输入流FileInputStream 和输出流类
FileOutputStream 一起使用。
1. 程序功能:将整型数据和字符串对象通过数据输出流写到文件中。将文件中的整型
数据和字符串对象通过数据输出流读出,并在屏幕上显示文件中的内容。 2. 编写KY10_4.java 程序文件,源代码如下。
import java.io.*; public class KY10_4
{
public static void main(String arg[]) { try
{ //添加方式创建文件输出流
FileOutputStream fout = new FileOutputStream(\DataOutputStream dout = new DataOutputStream(fout); dout.writeInt(1);
dout.writeChars(\罗马\dout.writeInt(2);
dout.writeChars(\北京\dout.close();
}
catch (IOException ioe){} try {
FileInputStream fin = new FileInputStream(\DataInputStream din = new DataInputStream(fin); int i = din.readInt();
while (i!=-1) //输入流未结束时,输入流结束时i 为-1 {
System.out.print(i+\char ch ;
while ((ch=din.readChar())!='\\n') //字符串未结束时 System.out.print(ch);
成功不是偶然 失败不是命运 sanjtu
点石科技整理(Http://www.loosr.cn) sanjtu@163.com
System.out.println(); i = din.readInt(); }
din.close(); }
catch (IOException ioe){} }
}
3. 编译并运行程序
成功不是偶然 失败不是命运 sanjtu
点石科技整理(Http://www.loosr.cn) sanjtu@163.com
第八次实验:多线程与异常处理
一、实验目的
了解线程的概念、线程的生命周期,掌握多线程的编程。掌握异常的概念以及如何定义、抛出和捕捉处理异常。
二、实验要求
1. 掌握利用Java语言编写多线程程序的方法 2. 掌握线程的调度方法
3. 编写一个程序,用来捕获各种类型的异常
4. 编写一个程序,用来说明异常处理块中各个语句块的作用 5. 熟悉异常的类型及其产生的原因
三、实验内容
(一)Thread子类的方法实现多线程
1. 编写 TwoThreadsTest.java 程序文件,源代码如下。 class SimpleThread extends Thread { public SimpleThread(String str) { super(str); }
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i + \ try {
sleep((int)(Math.random() * 1000)); } catch (InterruptedException e) {} }
System.out.println(\ }
}
public class TwoThreadsTest {
public static void main (String[] args) {
new SimpleThread(\ new SimpleThread(\ } }
2. 编译、运行
3. 请将程序的运行结果写在实验报告中。
成功不是偶然 失败不是命运 sanjtu
点石科技整理(Http://www.loosr.cn) sanjtu@163.com
(二)实现Runnable接口的方法实现多线程
1. 程序功能:一个时钟Applet,它显示当前时间并逐秒进行更新 2. 编写Clock.java 程序文件,源代码如下。 import java.awt.*; import java.applet.*;
import java.util.*;
public class Clock extends Applet implements Runnable{ Thread clockThread; public void start(){
if(clockThread==null){
clockThread=new Thread(this,\ clockThread.start(); } }
public void run(){
while(clockThread !=null){ repaint(); try{
clockThread.sleep(1000); }catch(InterruptedException e){} } }
public void paint(Graphics g){
Date now=new Date();
g.drawString(now.getHours()+\
s(),5,10);
}
public void stop(){
clockThread.stop(); clockThread=null; } }
3.编译Clock.java 文件。
4.编写Clock.html 文件,源代码如下:
Here is the output of my program:
成功不是偶然 失败不是命运 sanjtu