Java程序上机练习题(6)

2019-08-30 23:55

}

}

练习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

catch(IOException e) { System.out.print(e); }

} }

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

System.out.print((char)b[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

dos.writeInt(data1[i]);//向管道输出流写入数据 try {sleep(1000);} //线程休眠1000毫秒

catch(InterruptedException e) { } }

dos.close( );//关闭流

}

catch( IOException e ) { }

System.out.println( e );

}

/*从管道中读取数据的线程子类:接收管道中传过来的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(); }

catch( IOException e ) { System.out.println( e ); }

sum+=x;

System.out.println(\第\次接收数据:\

+x+\接收数据的和:\

try {sleep(1000);} catch(InterruptedException e) { } }

dis.close( );//关闭流

} }

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' 和”程序设计“ }

catch( IOException e ) {

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();


Java程序上机练习题(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:某高速公路路基溶洞处理施工技术方案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: