数据流I/O

实验目的:

1.掌握文件输入\输出流的使用方法。

2.掌握带缓存的输入\输出流的使用方法。

实验内容和原理:

1. 文件输出流的应用。

定义如下字符串:

String str = “12345abcdef@#%&*软件工程”;

编写程序将该字符串写入文件”data.txt”。

2. 文件输入流的应用。

修改第1题中的程序,读文件”data.txt”,将读到的数据输出在控制台。

import java.io.*;
public class IO {
     public static void main(String[] args){
    	 File file=new File("data.txt");
    	 try{
    		 FileOutputStream out=new FileOutputStream(file);
    		 byte buy[]="呕心沥血编写的程序终于成功了!!!!".getBytes();
    		 out.write(buy);
    		 out.close();
    	    }catch(Exception e){
    		 e.printStackTrace();
    	           }
    	    try{
    	    	FileInputStream in=new FileInputStream(file);
    	    	byte byt[]=new byte[1024];
    	    	int len=in.read(byt);
    	    	System.out.println("文中的信息是:"+new String(byt,0,len));
    	    	in.close();
    	    }catch(Exception e){
    	    	e.printStackTrace();
    	                }
                     }
                 }

 运行结果为:

posted @ 2019-07-08 15:24  罗梦祺  阅读(192)  评论(0编辑  收藏  举报