- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Java中的IO流总结(推荐)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
I/O是Input/output的缩写,在java中,对于数据的输入和输出以流的方式进行。java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过标准的方法输入或输出数据。 。
输入输出都是基于内存的角度来说的。输入:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中。 输出:输出output:将程序(内存)数据输出到磁盘、光盘等存储设备中.
流的分类 。
按操作数据单位不同分为:字节流(8 bit),字符流(16 bit) 。
按数据流的流向不同分为:输入流,输出流 。
按流的角色的不同分为:节点流,处理流 。
节点流:直接从数据源或目的地读写数据。 。
处理流:不直接连接到数据源或目的地,而是“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供更为强大的读写功能。 相当于是二次包装.
抽象基类 | 字节流 | 字符流 |
---|---|---|
输入流 | InputStream | Reader |
输出流 | OutputStream | Writer |
Java的IO流共涉及40多个类,实际上非常规则,都是从这4个抽象基类派生的。由这四个类派生出来的子类名称都是以其父类名作为子类名后缀.
InputStream 。
Reader 。
OutputStream 。
Writer 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
// 读取文本文件内容,用字节流去读,可能会乱码
public
void
test1() {
InputStream inputStream =
null
;
try
{
inputStream =
new
FileInputStream(
"hello.txt"
);
//相对路径是工程路径下
int
len;
byte
[] bytes =
new
byte
[
5
];
// 定义5字节长度的byte数组
while
((len = inputStream.read(bytes)) != -
1
) {
// 将数据读到byte数组中
System.out.println(
new
String(bytes,
0
, len));
//打印到控制台
}
}
catch
(Exception e) {
e.printStackTrace();
}
finally
{
try
{
if
(inputStream !=
null
) {
inputStream.close();
}
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
// 读取文本文件内容,用字符流去读,不会乱码,最多重复
public
void
test2() {
Reader reader =
null
;
try
{
reader =
new
FileReader(
"hello.txt"
);
int
len;
char
[] charBuff =
new
char
[
5
];
while
((len = reader.read(charBuff)) != -
1
) {
// System.out.println(new String(charBuff)); // 注意这两行的代码区别
System.out.println(
new
String(charBuff,
0
, len));
}
}
catch
(Exception e) {
e.printStackTrace();
}
finally
{
try
{
if
(reader !=
null
) {
reader.close();
}
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public
void
test3() {
InputStream inputStream =
null
;
OutputStream outputStream =
null
;
try
{
inputStream =
new
FileInputStream(
"hello.txt"
);
// 读取文件
outputStream =
new
FileOutputStream(
"hello2.txt"
);
// 相对路径,默认是工程路径下
int
len;
byte
[] bytes =
new
byte
[
5
];
while
((len = inputStream.read(bytes)) != -
1
) {
outputStream.write(bytes,
0
, len);
}
}
catch
(Exception e) {
e.printStackTrace();
}
finally
{
try
{
if
(inputStream !=
null
) {
inputStream.close();
}
if
(outputStream !=
null
){
outputStream.close();
}
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
|
字符流读取文件到输出到指定位置时,如果没有手动关闭流,则不会输出到指定位置,需要手动flush。但是如果在finally块中关闭了流,则会自动flush。在close()操作中,会帮我们flush.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
public
void
test4() {
Reader reader =
null
;
Writer writer =
null
;
try
{
reader =
new
FileReader(
"hello2.txt"
);
writer =
new
FileWriter(
"hello3.txt"
);
int
len;
char
[] charBuff =
new
char
[
5
];
while
((len = reader.read(charBuff)) != -
1
) {
writer.write(charBuff,
0
, len);
// writer.flush();
}
}
catch
(Exception e) {
e.printStackTrace();
}
finally
{
try
{
if
(reader !=
null
) {
reader.close();
}
if
(writer !=
null
) {
// 一定要关闭字符流,否则要手动flush
writer.close();
}
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
|
注意,字符流用来处理字符串很便捷,并且只能操作普通的文本文件,例如:.txt,.java,.c,.cpp 等语言的源代码。尤其注意.doc,excel,ppt这些不是文本文件。字节流既可以操做文本文件,也可以操作字节文件,比如.mp3,.avi,.rmvb,mp4,.jpg,.doc,.ppt。如果用字符流来操作图片等字节文件,生成的文件是无法打开的! 。
为了提高数据读写的速度,Java API提供了带缓冲功能的流类,在使用这些流类时,会创建一个内部缓冲区数组,缺省使用 8192个字节(8Kb) 的缓冲区.
缓冲流要“套接”在相应的节点流之上,根据数据操作单位可以把缓冲流分为BufferedInputStream和ufferedOutputStream以及BufferedReader和BufferedWriter。分别对应字节缓冲流和字符缓冲流。相当于在字节和字符流上包装了一下.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
public
void
test() {
InputStream inputStream ;
OutputStream outputStream ;
BufferedInputStream bis =
null
;
BufferedOutputStream bos =
null
;
try
{
inputStream =
new
FileInputStream(
"modify.txt"
);
//小于8KB
outputStream =
new
FileOutputStream(
"modify2.txt"
);
bis =
new
BufferedInputStream(inputStream);
bos =
new
BufferedOutputStream(outputStream);
int
len;
byte
[] bytes =
new
byte
[
5
];
while
((len = bis.read(bytes)) != -
1
) {
bos.write(bytes,
0
, len);
}
}
catch
(Exception e) {
e.printStackTrace();
}
finally
{
try
{
if
(bis !=
null
) {
bis.close();
}
if
(bos !=
null
){
bos.close();
}
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
public
void
test2() {
Reader reader ;
Writer writer ;
BufferedReader br =
null
;
BufferedWriter bw =
null
;
try
{
reader =
new
FileReader(
"modify.txt"
);
writer =
new
FileWriter(
"modify2.txt"
);
br =
new
BufferedReader(reader);
bw =
new
BufferedWriter(writer);
int
len;
char
[] bytes =
new
char
[
5
];
while
((len = br.read(bytes)) != -
1
) {
bw.write(bytes,
0
, len);
}
}
catch
(Exception e) {
e.printStackTrace();
}
finally
{
try
{
if
(br !=
null
) {
br.close();
}
if
(bw !=
null
){
bw.close();
}
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
|
**缓冲流如果没有手动关闭流,且读取的文件小于底层缓存大小8KB,是不会自动写到目标中去的,需要手动flush。**在关闭流时,只需关闭缓冲流即可,它会自动关闭它包装的底层节点流.
为了方便地操作Java语言的基本数据类型和String的数据,可以使用数据流。数据流有两个类:DataInputStream和DataOutputStream, 别“套接”在InputStream和OutputStream子类的流上.
使用如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
public
void
test() {
DataOutputStream dos =
null
;
try
{
OutputStream outputStream =
new
FileOutputStream(
"data.txt"
);
dos =
new
DataOutputStream(outputStream);
dos.writeUTF(
"热烈庆祝中国共产党成立一百周年"
);
dos.writeInt(
100
);
dos.writeBoolean(
true
);
System.out.println(
"成功!"
);
}
catch
(IOException e) {
e.printStackTrace();
}
finally
{
try
{
if
(dos !=
null
) {
dos.close();
}
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
public
void
test2(){
DataInputStream dis =
null
;
try
{
InputStream inputStream =
new
FileInputStream(
"data.txt"
);
dis =
new
DataInputStream(inputStream);
System.out.println(dis.readUTF());
//读取时要按照写入顺序读取
System.out.println(dis.readInt());
System.out.println(dis.readBoolean());
System.out.println(
"成功!"
);
}
catch
(IOException e) {
e.printStackTrace();
}
finally
{
try
{
if
(dis !=
null
) {
dis.close();
}
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
|
用于存储和读取基本数据类型数据或对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来.
序列化与反序列化的演示 。
先定义一个Person类,该类必须实现Serializable接口,否则序列化时会报java.io.NotSerializableException 的错误 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package
day07;
import
java.io.Serializable;
import
java.util.Date;
public
class
Person
implements
Serializable {
private
static
final
long
serialVersionUID = -5858950242987134591L;
private
String name;
private
Integer age;
private
Date date;
public
Person(){}
public
Person(String name, Integer age, Date date) {
this
.name = name;
this
.age = age;
this
.date = date;
}
// getter、setter略
@Override
public
String toString() {
return
"Person{"
+
"name='"
+ name + '\
''
+
", age="
+ age +
", date="
+ date +
'}'
;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public
void
test() {
ObjectOutputStream oos =
null
;
try
{
OutputStream outputStream =
new
FileOutputStream(
"person.txt"
);
// 创建输出流对象,指定输出位置
oos =
new
ObjectOutputStream(outputStream);
// 包装输出流
oos.writeObject(
new
Person(
"张三"
,
22
,
new
Date()));
// 序列化对象
System.out.println(
"序列化成功!"
);
}
catch
(Exception e) {
e.printStackTrace();
}
finally
{
if
(oos!=
null
){
try
{
oos.close();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public
void
test2() {
ObjectInputStream ois =
null
;
try
{
InputStream inputStream =
new
FileInputStream(
"person.txt"
);
ois =
new
ObjectInputStream(inputStream);
Person person = (Person) ois.readObject();
System.out.println(
"person姓名为:"
+ person.getName());
System.out.println(
"person年龄为:"
+ person.getAge());
SimpleDateFormat sdf =
new
SimpleDateFormat(
"yyyy年MM月dd日 hh时mm分ss秒"
);
System.out.println(
"创建时间为:"
+ sdf.format(person.getDate()));
}
catch
(IOException e) {
e.printStackTrace();
}
catch
(ClassNotFoundException e) {
e.printStackTrace();
}
finally
{
if
(ois !=
null
) {
try
{
ois.close();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
}
|
无论如何反序列化,时间都是不变的.
本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注我的更多内容! 。
原文链接:https://blog.csdn.net/weixin_44061521/article/details/118653939 。
最后此篇关于Java中的IO流总结(推荐)的文章就讲到这里了,如果你想了解更多关于Java中的IO流总结(推荐)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
1、定义 设 \(u\) 和 \(v\) 为一张图上的任意两个节点。令 \(c(u, v)\) 为它们之间的边的容量, \(f(u, v)\) 为它们之间的流量,则需要满足以
1、前言 工作中涉及到文件系统,有时候需要判断文件和目录是否存在。我结合apue第四章文件和目录,总结一下如何正确判断文件和目录是否存在,方便以后查询。 2、stat系列函数 stat函数用来
并查集(Union-Find Set): 一种用于管理分组的数据结构。它具备两个操作:(1)查询元素a和元素b是否为同一组 (2) 将元素a和b合并为同一组。 注意:并查集不能将在同一组的元素拆
当下,注解非常流行,以前很长篇的代码,现在基本上一个注解就能搞定。 那,在Mybatis中又有哪些注解呢? Mybatis中的注解基本上都在org.apache.ibatis.annotat
指针操作数组,方法一是p+index,方法二是p[index],第二种方法跟数组访问方法是一样的。 数组引用返回的是数组的第一个元素的指针地址。 可以将指针指向数组的任意元素,然后从那里开始访问
通常部署完php环境后会进行一些安全设置,除了熟悉各种php漏洞外,还可以通过配置php.ini来加固PHP的运行环境,PHP官方也曾经多次修改php.ini的默认设置。 下面对php.ini中一
在JavaScript中,使用typeof可以检测基本数据类型,使用instanceof可以检测引用数据类型。在PHP中,也有检测数据类型的方法,具体如下: 1、输出变量的数据类型(gettype
把图片缓存到本地,在很多场景都会用到,如果只是存储文件信息,那建一个plist文件,或者数据库就能很方便的解决问题,但是如果存储图片到沙盒就没那么方便了。这里简单介绍两种保存图片到沙盒的方法。
(1)需要安装docker容器,在docker容器内安装jenkins,gogs,tomcat。 新建maven项目,添加findbugs plugin。 使用docker
今天主题是实现并发服务器,实现方法有多种版本,先从简单的单进程代码实现到多进程,多线程的实现,最终引入一些高级模块来实现并发TCP服务器。 说到TCP,想起吐槽大会有个段子提到三次握手,也只有程序
如下所示: Ctrl+1或F2快速修复 Ctrl+D快捷删除行 Shift+Enter 快速切换到下一行,在本行的任何位置都可 Ctrl+F11快速运行代码 Alt+上下键 快速移动行(可
JSP是Servlet技术的扩展,本质上是Servlet的简易方式,更强调应用的外表表达。 JSP编译后是”类servlet”。 Servlet和JSP最主要的不同点在于,Servlet的应用逻辑
Java中的Runable,Callable,Future,FutureTask,ExecutorService,Excetor,Excutors,ThreadPoolExcetor在这里对这些关键
读取Java文件到byte数组的三种方法(总结) ? 1
用java实现的数组创建二叉树以及递归先序遍历,递归中序遍历,递归后序遍历,非递归前序遍历,非递归中序遍历,非递归后序遍历,深度优先遍历,广度优先遍历8种遍历方式:
1、简明总结 ASCII(char) 返回字符的ASCII码值 BIT_LENGTH(str) 返回字符串的比特长度 CONCAT(s1,s2…,sn)
java应用服务器(web server),是指运行java程序的web应用服务器软件,不包括nginx、Apache等通用web服务器软件。 一、Tomcat Tomcat是Apache 软件基
事务作为抽象层,允许应用忽略DB 内部一些复杂并发问题和某些硬件、软件故障,简化应用层的处理逻辑:事务中止(transaction abort),而应用仅需重试。对复杂访问模式,事务可大大减少需要考虑
我们在本教程学习了如何描述 XML 文档的结构 我们学习到了如何使用 DTD 来定义一个 XML 文档的合法元素,以及如何在我们的 XML 内部或者作为一个外部引用来声明 DTD 我们学习了如何为
在这个XPath 基础教程中我们讲解了如何在 XML 文档中查找信息 我们可以使用 XPath 的元素和属性在 XML 文档中进行导航 我们也学习了如何使用 XPath 中内建的某些标准函数 如
我是一名优秀的程序员,十分优秀!