在程序中所有的数据都是以流的形式进行传输或保存的,程序需要数据时要使用输入流读取数据,而当程序需要将一些数据保存起来时,就要使用输出流完成对于操作文件内容,要进行文件内容的操作就需要通过Java提供的两组数据流的操作类完成

字节操作流(byte):OutputStream,InputStream;

字符操作流(char):Writer,Reader

对于资源内容的操作,都会按照如下的步骤执行

(1)如果要操作的是文件,那么首先要通过File类对象找到一个要操作的文件路径

(2)通过字节流或者字符流的子类为字节流或字符流的对象实例化

(3)执行读写操作

(4)最后一定要关闭操作的资源。

1.字节输出流

public class test2 {
public static void main(String[] args) throws IOException {
File file=new File("D:"+File.separator+"hellodemo"+File.separator+"test.txt");//定义文件路径
if(!file.getParentFile().exists()) {//判断父路径是否存在
file.getParentFile().mkdirs();//创建父路径
}
OutputStream output=new FileOutputStream(file);//通过子类实例化父类
String data="Hello world";
output.write(data.getBytes());//将数据变为字节数组输出
output.close();
}
}

关于OutputStream定义的三个输出方法

1.public abstract void write(int b) throws IOException
2.public void write(byte[] b) throws IOException
3.public void write(byte[] b,int off,int len)throws IOException //从off开始,len结束

FileOutputStream的构造方法

1.public FileOutStream(File file) throws FileNotFound Exception//传入file实例构成文件输出流
2.public FileOutStream(File file,boolean append)throws FileNotFoundException//用于追加数据

追加数据

OutputStream output=new FileOutputStream(file,true);//通过子类实例化父类,设置追加为true
     String data="\r\ncaizhen 666";//   /r/n表示换行
output.write(data.getBytes());    //写入
output.close();
    

输出部分数据

output.write(data.getBytes(),0,5);

2.字节输入流

1.public abstract int read()throws IOException//读取单个字符,如果读到结尾返回-1
2.public int read(byte[] b)throws IOException//读取多个字符,如果需要读取的数据小于byte的数据,返回的是数据的个数,如果不是且已经读完则返回-1
3.public int read(byte[] b,int off,int len)throws IOException

2.1 一次性全部读取

public class test2 {
public static void main(String[] args) throws IOException {
File file=new File("D:"+File.separator+"hellodemo"+File.separator+"test.txt");//定义文件路径
if(file.exists()) {//判断文件是否存在
InputStream input=new FileInputStream(file);
byte data[]=new byte[1024];//假设要读的长度是1024
int len=input.read(data);//读取数据,返回读取个数
input.close();
System.out.println("读取的数据是:"+new String(data,0,len));
}
}
}

2.2单个字节读取

public class test2 {
public static void main(String[] args) throws IOException {
File file=new File("D:"+File.separator+"hellodemo"+File.separator+"test.txt");//定义文件路径
if(file.exists()) {//判断文件是否存在
InputStream input=new FileInputStream(file);
byte data[]=new byte[1024];//假设要读的长度是1024
int temp=0;
int foot=0;
while((temp=input.read())!=-1) {//表示未读取完毕
data[foot++]=(byte)temp;
}
input.close();
System.out.println("读取的数据是:"+new String(data,0,foot));
}
}
}

程序解读:

1.定义文件路径

2.判断文件是否存在,若存在则执行

3.利用FileInputStream的构造方法传入file实例获得InputStream对象

4.新建一个长度为1024的字节数组

5.利用InputStream对象的read()方法,读取一个字节,并且把变量赋值给我temp

6.当temp=-1时跳出循环,表示读取完毕

7.利用字符串的构造方法生成字符串并打印

3.部分读取

       InputStream input=new FileInputStream(file);
byte data[]=new byte[1024];//假设要读的长度是1024
int len=input.read(data,0,3);
input.close();
System.out.println("读取的数据是:"+new String(data,0,len));

最新文章

  1. IOS开发基础知识--碎片49
  2. MISC-极客大挑战-pen_and_apple.rar
  3. Graph单元
  4. iOS-解决iOS8及以上设置applicationIconBadgeNumber报错的问题
  5. yum只下载而不安装软件包?
  6. C# 文件读写FileInfo
  7. 山东理工大学第七届ACM校赛-完美素数 分类: 比赛 2015-06-26 10:36 15人阅读 评论(0) 收藏
  8. 集群监控系统Ganglia应用案例
  9. UVa 699 The Falling Leaves
  10. Spring 3.0 注解注入详解
  11. [LeetCode] Rotate Image [26]
  12. access的保留关键字
  13. WIFI机器人网
  14. 使用 electron 做个播放器
  15. Redis-入门笔记-15min带你一览redis
  16. 基于opencv3.0下的运动车辆识别
  17. oracle 12C利用dbca建库13步
  18. angr进阶(4)从任意位置开始
  19. mysql常用权限命令、乱码及其他问题记录
  20. Zabbix监控——Zabbix自定义用户参数制作监控项

热门文章

  1. CentOS 设置网络及安装 ifconfig
  2. [cf1491H]Yuezheng Ling and Dynamic Tree
  3. 听说你想把对象存储当 HDFS 用,我们这里有个方案...
  4. tomcat指定特定版本的jdk
  5. 联盛德 HLK-W806 (五): W801开发板上手报告
  6. JSOI2021 酱油记
  7. Codeforces 464E The Classic Problem(主席树+最短路+哈希,神仙题)
  8. LOJ #6207 - 米缇(杜教筛+拉格朗日插值)
  9. Atcoder Typical DP Contest S - マス目(状压 dp+剪枝)
  10. 洛谷 P3438 - [POI2006]ZAB-Frogs(乱搞/李超线段树)