学号-姓名-Java语言实验报告四

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

《Java语言与面向对象程序设计基础》课程
实验报告四
姓名:xxx
学号:20097110080xxx
实验题1
[实验要求]
•掌握Java IO流处理
•掌握文件操作
•掌握多线程程序设计
[实验程序]
import java.io.*;
public class Test4_1 {
public static void main(String[] args) {
try {
File myDir=new File("C:/Test");
if ( !myDir.exists()) myDir.mkdir();
else if ( !myDir.isDirectory()) {
System.err.println(" 'C:/Test' is not a directory");
return;
}
File f=new File(myDir,"a.txt");
f.createNewFile();
FileWriter out = new FileWriter(f);
for (int i=0;i<26;i++){
out.write((char)('A'+i));
}
out.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
[运行结果]
[实验结论与收获]
掌握文件及文件夹的创建及文件的写入。

实验题2
[实验要求]
将实验1题中新建的"a.txt"文件内容复制到"C:\Test\Ex"目录中"b.txt"文件中。

[实验程序]
import java.io.*;
public class Test4_2 {
public static void main(String[] args) {
try {
File myDir=new File("C:/Test/Ex");
if ( !myDir.exists()) myDir.mkdir();
else if ( !myDir.isDirectory()) {
System.err.println(myDir+ " is not a directory");
return;
}
File myFile =new File(myDir,"b.txt");
myFile.createNewFile();
FileReader in= new FileReader("C:/Test/a.txt");
BufferedReader bufIn = new BufferedReader(in);
FileWriter out= new FileWriter (myFile);
BufferedWriter bufOut= new BufferedWriter(out);
String line;
line = bufIn.readLine();
//System.out.println(line);
while ( line!= null ) {
System.out.println(line);
bufOut.write(line,0,line.length());
bufOut.newLine();
line = bufIn.readLine();
}
bufIn.close();
bufOut.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
[运行结果]
[实验结论与收获]
掌握文件读取和写入。

实验题3
[实验要求]
•将实验1题中新建的“a.txt”文件中写入字符“*”,替换第6个字符。

[实验程序]
import java.io.*;
public class Test4_3 {
public static void main(String[] args) {
try {
RandomAccessFile r = new RandomAccessFile("C:/Test/a.txt","rw");
r.seek(5);
r.write('*');
r.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
[运行结果]
[实验结论与收获]
掌握随机文件创建与写入。

实验题4
[实验要求]
•调试并记录多线程程序(生产者/消费者实例)结果。

•体会多线程机制。

[实验程序]
Consumer.java
class Consumer implements Runnable{
SStack theStack;
public Consumer(SStack s){
theStack = s;
}
public void run(){
char c;
for (int i=0;i<20;i++) {
c = theStack.pop();
System.out.println("Consumed: "+c);
try{
Thread.sleep((int)(Math.random()*1000));
}catch(InterruptedException e){}
}
}
}
Producer.java
class Producer implements Runnable{
SStack theStack;
public Producer(SStack s){
theStack = s;
}
public void run(){
char c;
for(int i=0; i<20; i++){
c =(char)(Math.random()*26+'A');
theStack.push(c);
System.out.println("Produced: "+c);
try{
Thread.sleep((int)(Math.random()*100));
}catch(InterruptedException e){}
}
}
}
SStack.java
class SStack{
private int index = 0;
private char []data = new char[10];
public synchronized void push(char c){
while(index == data.length){
try{ this.wait();
}catch(InterruptedException e){ }
}
this.notify();
data[index] = c;
index++;
}
public synchronized char pop(){
while(index ==0){
try{ this.wait();
}catch(InterruptedException e){ } }
this.notify();
index--;
return data[index];
}
}
STest.java
public class STest{
public static void main(String args[]){
SStack stack = new SStack();
Runnable p=new Producer(stack);
Runnable c = new Consumer(stack);
Thread t1 = new Thread(p);
Thread t2 = new Thread(c);
t1.start();
t2.start();
}
}
[运行结果]
[实验结论与收获]
掌握多线程应用。

相关文档
最新文档