云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > Java程序上机练习题

Java程序上机练习题

  • 62 次阅读
  • 3 次下载
  • 2025/5/6 19:28:57

try { Thread.sleep( 300); } catch (InterruptedException e) { } } } public void paint (Graphics g) { String s1=year+\年\月\日\ String s2=\星期\ String s3=hour+\ g.setFont (wordFont); g.setColor (Color.green); g.drawString (s1, 20, 50); g.drawString (s2, 20, 120); g.drawString (s3, 20, 200); } }

5.编写小程序实现Runnable接口,通过多线程实现在小程序窗口中不断的显示自然数:从1直到100。 import java.awt.*; import java.applet.*;

public class Javaapplet extends Applet implements Runnable { int counter=0; Thread t;

public void init() { t=new Thread(this); t.start(); } public void run() { while( counter<=100 ) { counter++; try { Thread.sleep(1000); } catch ( InterruptedException e ) { } repaint(); } } public void paint( Graphics g ) { setBackground(Color.black); g.setColor(Color.green); g.setFont(new Font(\ g.drawString( String.valueOf(counter),60,60 );

}

}

练习9 输入输出(15)

掌握:java的输入输出处理

1.编写应用程序,使用System.in.read()方法读取用户从键盘输入的字节数据,回车后,把从键盘输入的数据存放到数组buffer中,并将用户输入的数据通过System.out.print()显示在屏幕上。 import java.io.*; public class Class1 { public static void main(String args[]) { byte buffer[]=new byte[128]; int n; try { n=System.in.read(buffer); //把键盘输入的数据读到数组buffer中,返回实际读取的字节数 for(int i=0;i

2.编写应用程序,使用System.in.read()方法读取用户从键盘输入的字节数据,回车后,把从键盘输入的数据存放到数组buffer中,并将用户输入的数据保存为指定路径下的文件。 import java.io.*; public class Class1 { public static void main(String args[]) { byte buffer[]=new byte[128]; int n; try { n=System.in.read(buffer);

FileOutputStream out=new FileOutputStream(\追加 //FileOutputStream out=new FileOutputStream(\ out.write(buffer,0,n); out.close( ); } catch(IOException e) { System.out.print(e); } } }

3. 编写java应用程序,使用FileInputStream类对象读取程序本身(或其他目录下的文件)并显示在屏幕上。

import java.io.*; public class Class1 { public static void main (String[] args) { try { //FileInputStream fis=new FileInputStream(\ FileInputStream fis=new FileInputStream(\ int n; while((n=fis.read())!=-1) System.out.print((char)n); fis.close(); } catch(IOException e) { System.out.println(e.toString()); } } }

4. 编写java应用程序,使用FileInputStream类对象读取程序本身(或其他目录下的文件)到字节数组中,并显示在屏幕上(或存储为其他文件)。 import java.io.*;//读取程序本身,显示在屏幕上 public class Class1 { public static void main (String[] args) { try { FileInputStream fis=new FileInputStream(\ byte[] b=new byte[fis.available()]; System.out.println(\文件流的大小:\ int n=fis.read(b); myprint(b); System.out.print(\实际读取的字节数:\ fis.close(); } catch(IOException e) { System.out.println(e.toString()); } } static void myprint(byte[] x) { for(int i=0;i

} }

5.编写应用程序,程序中创建一个文件输入流对象fis,读取当前目录下文本文件test1.txt,该文件内容有如下两行文本:

Java program is easy. I like it.

从文件输入流fis中读取5个字节数据存放到数组b中,字节数据存放的位置从数组下标3开始。将读取的数据在屏幕输出。 import java.io.*; public class Class1 { public static void main( String[ ] args ) throws IOException { File file = new File(\ FileInputStream fis = new FileInputStream( file); int n=0; byte b[]=new byte[8]; n=fis.read(b,3,5); fis.close(); for(int i=3;i

6.编写应用程序,程序中创建一个文件输出流对象out向当前目录下已有的文件abc.txt(内容为:\)写入字符串\中的所有字符和大写字母'A'。 import java.io.*; public class Class1 { public static void main (String[] x) throws IOException { String s=\ byte[] b; FileOutputStream out=new FileOutputStream(\添加字节数据 b=s.getBytes(); out.write(b,0,b.length); out.write(65);//写入字节数据65---‘A’ out.close(); } }

7.编写两个线程子类,分别用来创建管道输出流和管道输入流,其中管道输出流向管道发送5个0~20之间的随机整数;管道输入流接收管道中传过来的5个随机整数,并求他们的和。编写Java应用程序测试管道流的数据传送。 //TestPipedStream.java import java.io.*;

public class TestPipedStream { public static void main( String[] args ) { try {//创建没有连接的管道输出流和管道输入流

搜索更多关于: Java程序上机练习题 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

try { Thread.sleep( 300); } catch (InterruptedException e) { } } } public void paint (Graphics g) { String s1=year+\年\月\日\ String s2=\星期\ String s3=hour+\ g.setFont (wordFont); g.setColor (Color.green); g.drawString (s1, 20, 50); g.drawString (s2, 20, 120); g.drawString (s3, 20, 200); } } 5.编写小程序实现Runnable接口,通过多线程实现在小程序窗口中不断的显示自然数:从1直到100。 import java.a

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com