Java 字节流
2024-09-18 06:03:18
OutputStream此抽象类,是表示输出字节流的所有类的超类。操作的数据都是字节,定义了输出字节流的基本共性功能方法。
OutputStream有很多子类,其中子类FileOutputStream可用来写入数据到文件。
FileOutputStream类,即文件输出流,是用于将数据写入 File的输出流。
将数据写到文件中,代码演示:
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
//需求:将数据写入到文件中。
//创建存储数据的文件。
File file = new File("c:\\file.txt");
//创建一个用于操作文件的字节输出流对象。一创建就必须明确数据存储目的地。
//输出流目的是文件,会自动创建。如果文件存在,则覆盖。
FileOutputStream fos = new FileOutputStream(file);
//调用父类中的write方法。
byte[] data = "abcde".getBytes();
fos.write(data);
//关闭流资源。
fos.close();
}
}
给文件中续写和换行
我们直接new FileOutputStream(file)这样创建对象,写入数据,会覆盖原有的文件,那么我们想在原有的文件中续写内容怎么办呢?
继续查阅FileOutputStream的API。发现在FileOutputStream的构造函数中,可以接受一个boolean类型的值,如果值true,就会在文件末位继续添加。
public class FileOutputStreamDemo2 {
public static void main(String[] args) throws Exception {
File file = new File("c:\\file.txt");
FileOutputStream fos = new FileOutputStream(file, true);
String str = "\r\n"+"aaa";
fos.write(str.getBytes());
fos.close();
}
}
IO异常的处理
public class FileOutputStreamDemo3 {
public static void main(String[] args) {
File file = new File("c:\\file.txt");
//定义FileOutputStream的引用
FileOutputStream fos = null;
try {
//创建FileOutputStream对象
fos = new FileOutputStream(file);
//写出数据
fos.write("abcde".getBytes());
} catch (IOException e) {
System.out.println(e.toString() + "----");
throw new RuntimeException("文件写入失败,重试"); } finally {
//一定要判断fos是否为null,只有不为null时,才可以关闭资源
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
}
}
}
InputStream此抽象类,是表示字节输入流的所有类的超类。,定义了字节输入流的基本共性功能方法。
l int read():读取一个字节并返回,没有字节返回-1.
int read(byte[]): 读取一定量的字节数,并存储到字节数组中,返回读取到的字节数。
InputStream有很多子类,其中子类FileInputStream可用来读取文件内容。
FileInputStream 从文件系统中的某个文件中获得输入字节。
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
File file = new File("c:\\file.txt");
//创建一个字节输入流对象,必须明确数据源,其实就是创建字节读取流和数据源相关联。
FileInputStream fis = new FileInputStream(file);
//读取数据。使用 read();一次读一个字节。
int ch = 0;
while((ch=fis.read())!=-1){
System.out.println("ch="+(char)ch);
}
// 关闭资源。
fis.close();
}
}
//读取数据read(byte[])方法
public class FileInputStreamDemo2 {
public static void main(String[] args) throws IOException {
/*
* 演示第二个读取方法, read(byte[]);
*/
File file = new File("c:\\file.txt");
// 创建一个字节输入流对象,必须明确数据源,其实就是创建字节读取流和数据源相关联。
FileInputStream fis = new FileInputStream(file);
//创建一个字节数组。
byte[] buf = new byte[1024];//长度可以定义成1024的整数倍。
int len = 0;
while((len=fis.read(buf))!=-1){
System.out.println(new String(buf,0,len));
}
fis.close();
}
}
最新文章
- MSCRM 仪表盘 控件 数量 更改(Change the maximum no. of controls on MSCRM Dashboards )
- 逻辑运算符——逻辑与&;&;、逻辑或||
- NYOJ(21),BFS,三个水杯
- guice框架的入门使用
- hdu 4619 Warm up 2 ( 二分图最大匹配 )
- CodeForces 706E Working routine
- XSS闯关游戏准备阶段及XSS构造方法
- 优秀员工的修炼——通往专家、管理之路
- debian删除i386的包
- 搜索核心原理之网页和查询的相关性——TF-IDF
- GCC输出带C源代码的汇编文件
- Unity3D学习笔记(三十五):Shader着色器(2)- 顶点片元着色器
- 问题描述: fatal error: 'XCTest/XCTest.h' file not found
- HP服务器安装配置教程
- VMware下centos7安装VMware Tools
- CSS实现点击改变元素背景色
- HDU 3374 String Problem(KMP+最大(最小)表示)
- POJ 2288 Islands and Bridges(状压dp)
- html发展史简介(摘抄)
- BZOJ4299 &; CC FRBSUM:ForbiddenSum &; BZOJ4408 &; 洛谷4587 &; LOJ2174:[FJOI2016]神秘数——题解