Java程序练习100题 下载本文

内容发布更新时间 : 2024/5/4 14:49:53星期一 下面是文章的全部内容请认真阅读。

}

}

练习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 {//创建没有连接的管道输出流和管道输入流

PipedOutputStream out = new PipedOutputStream( ); PipedInputStream in = new PipedInputStream( ); out.connect( in );//连接两个管道流 ThreadOut to = new ThreadOut( out ); ThreadIn ti = new ThreadIn( in ); to.start( );//启动线程 ti.start( ); } catch( IOException e ) { System.out.println( e ); } } }

/*向管道输出数据的线程子类:发送0~20之间的5个随机整数*/ class ThreadOut extends Thread { private int[] data1=new int[5]; //管道输出流要发送的数据 private DataOutputStream dos;//声明数据输出流对象dos public ThreadOut( PipedOutputStream out) { //将数据输出流和管道输出流连接,以便向管道发送int类型的数据 dos = new DataOutputStream(out); } public void run( ) {//重写线程类的方法,线程启动后将执行该方法 try {//向管道中写入数据 for(int i=0;i

/*从管道中读取数据的线程子类:接收管道中传过来的5个随机整数*/ class ThreadIn extends Thread { private int sum=0; //5个随机整数 private DataInputStream dis;//声明数据输入流对象dis public ThreadIn( PipedInputStream in ) { //将数据输入流和管道输入流连接,以便从管道读取int类型的数据

dis = new DataInputStream( in ); } public void run( ) {//重写线程类的方法,线程启动后将执行该方法 try {//接收输出管道发送的数据: int x; for(int i=0;i<5;i++) { x=dis.readInt(); sum+=x; System.out.println(\第\次接收数据:\ +x+\接收数据的和:\ try {sleep(1000);} catch(InterruptedException e) { } } dis.close( );//关闭流 } catch( IOException e ) { System.out.println( e ); } } }

8.使用RandomAccessFile类及其方法。 import java.io.*; public class Class1 { public static void main( String[ ] args ) { try { //以随机方式写入数据 RandomAccessFile out = new RandomAccessFile( \ out.writeInt(12345);//4 out.writeChar('A');//2 out.writeBoolean(true);//1 out.writeUTF(\程序设计\ //显示字符‘A' 和”程序设计“ out.seek(4); System.out.println(out.readChar()); out.skipBytes(1);//out.seek(7); System.out.println(out.readUTF()); System.out.println(out.getFilePointer()); System.out.println(out.length()); out.close(); System.in.read(); } catch( IOException e ) {