FileInputStream类

FileInputStream和FileOutPutStream类都是用来操作磁盘文件的。如果用户对文件读取需求比较简单,则可以使用FileInputStream类,该类继承InputStream类

 1 package cn.atun;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileOutputStream;
 6 
 7 /*
 8  * FileInputStream类
 9  * FileInputStream和FileOutPutStream类都是用来操作磁盘文件的。如果用户对文件读取需求比较简单,则可以使用FileInputStream类,该类继承InputStream类
10  * 
11  * */
12 public class FileIntputSteam
13 {
14 
15     public static void main(String[] args)
16     {
17 
18         File file = new File("e:", "test.txt");
19         try
20         {
21             FileOutputStream out = new FileOutputStream(file);// 创建FileOutputStream对象
22             byte buy[] = "我身处当时你幻想的未来里..".getBytes();// 创建BYTE数组
23             out.write(buy);// 将数组写和到流中
24             out.close();//关闭流
25 
26         } catch (Exception e)
27         {
28             e.printStackTrace();//输出异常信息
29         }
30 
31         // 读取数据到流并显示出来
32         try
33         {
34             FileInputStream in=new FileInputStream(file);//创建FileInputStream对象
35             byte byt[]=new byte[1024];// 创建BYTE数组
36             int len=in.read(byt);//从文件中读取数据
37             System.out.println("文件中的信息是:"+new String(byt,0,len));//输出
38             in.close();//关闭流
39                     
40         } catch (Exception e)
41         {
42             e.printStackTrace();//输出异常信息
43 
44         }
45 
46     }
47 
48 }

-----结果----

文件中的信息是:我身处当时你幻想的未来里..

转载于:https://www.cnblogs.com/c546170667/p/5854831.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值