文件输出流

FileOutputStream:文件输出流是用于将数据写入 File,每次运行,都会覆盖之前文件中的数据

FileOutputStream(File file):创建一个向指定 File 对象表示的文件中写入数据的文件输出流

public class MyTest2 {
public static void main(String[] args) throws IOException {
File file = new File("c.txt");//封装文件
FileOutputStream out = new FileOutputStream(file);
//输出流所关联的文件如果不存在,则会自动创建 //我们使用输入流,往文件中写入数据
out.write(97); //a 一次写入一个字节
out.write(98); //b
out.write(99);//c
out.write(300); //如果你超过一个字节的范围,会丢弃掉多余字节
byte[] bytes = {100, 101, 102, 103, 104, 105};
out.write(bytes);//一次写入一个字节数组
System.out.println(new String(bytes));
out.write(bytes, 0, 3); //一次写入字节数组的一部分
String str = "我爱学习";
byte[] bytes1 = str.getBytes(); //解码 utf-8
out.write(bytes1);
out.write(bytes1,0,12);
//流使用完毕要释放资源
out.close();
/* 创建字节输出流对象了做了几件事情 ?
a : 调用系统资源创建a.txt文件
b:创建了一个out对象
c:把out对象指向这个文件
为什么一定要close() ?
a : 通知系统释放关于管理a.txt文件的资源
b:让Io流对象变成垃圾, 等待垃圾回收器对其回收*/
}
}

FileOutputStream(String name):创建一个向具有指定名称的文件中写入数据的输出文件流

File file = new File("c.txt");//封装文件
FileOutputStream out2 = new FileOutputStream("e.txt");//传入的是字符串路径

FileOutputStream(File file, boolean append):创建一个向指定 File 对象表示的文件中写入数据的文件输出流

**FileOutputStream(String name, boolean append): **创建一个向具有指定 name 的文件中写入数据的输出文件流

public class MyTest2 {
public static void main(String[] args) throws IOException {
//默认的是,每次运行,都会覆盖之前文件中的数据
//参数2:true 代表追加写入,不会重新覆盖文件中之前的数据
FileOutputStream out = new FileOutputStream("e.txt",true);
out.write("你忘了你有多美".getBytes());
out.write("\r\n".getBytes());//换行
out.write("我爱你".getBytes());
out.write("\r\n".getBytes());//换行 out.close();
}
}

文件输入流

文件输出流:就是文件中的数据

FileInputStream(File file) :通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。

案例1:一次读取一个字节来读取文本文件

public class MyTest {
public static void main(String[] args) throws IOException {
File file = new File("h.txt");
//file.createNewFile();
//输入流所关联的文件,如果不存在就会报错
FileInputStream in = new FileInputStream(file);
int b = in.read();//一次读取一个字节,返回的是这个字节数据,如果读取不到,返回 -1 我们会拿-1 判断他文件是否读取完
System.out.println(b);
}
}

案例2:一次读取一部分字节,填入到缓冲区数组

public class MyTest3 {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("a.txt");
byte[] bytes = new byte[1024];
//一次读取一部分字节,填入到缓冲区数组
int len = in.read(bytes, 0, 20);
for (byte Byte : bytes) {
System.out.println(Byte);
} in.close();
}
}

案例3:读一个字节写一个字节来复制一个文本文件

public class MyTest {
public static void main(String[] args) throws IOException {
//采用文件输入输入流进行文件的复制
//读一个字节写一个字节来复制一个文本文件
FileInputStream in = new FileInputStream("MyTest.java");
FileOutputStream out = new FileOutputStream("C:\\Users\\ShenMouMou\\Desktop\\MyTest.java");
//频繁的读写 int len = 0;//读取到字节
while ((len=in.read())!=-1){
out.write(len);
out.flush();
}
//释放资源
in.close();
out.close();
System.out.println("复制完成");
}
}

案例4:一次读写一个字节数组来进行复制MP3

public class MyTest {
public static void main(String[] args) throws IOException {
//很显然,一次读写一个字节去复制文件,效率太低,太耗时
//一次读写一个字节数组来进行复制,才是首选
File file = new File("烟花易冷Live_林志炫.mp3");
FileInputStream in = new FileInputStream(file);
FileOutputStream out = new FileOutputStream("C:\\" + file.getName());
//定义一个字节数组,充当缓冲区
byte[] bytes = new byte[1024 * 8];
//定义一个变量,来记录你每次读取到的有效字节个数
int len = 0;
long start = System.currentTimeMillis();
while ((len = in.read(bytes)) != -1) {
// System.out.println("循环次数");
out.write(bytes, 0, len);
out.flush();
}
long end = System.currentTimeMillis();
System.out.println("复制完成耗时" + (end - start) + "毫秒");
//释放资源
in.close();
out.close();
}
}

FileInputStream(String name):通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。

高效的字节输入输出流

BufferedInputStream(InputStream in) :创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。

BufferedInputStream(InputStream in, int size):创建具有指定缓冲区大小的 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用

BufferedOutputStream(OutputStream out):创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流

BufferedOutputStream(OutputStream out,int size):创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流

public class MyTest {
public static void main(String[] args) throws IOException {
copy0(); //高效的流
copy1();
} private static void copy0() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("烟花易冷Live_林志炫.mp3"), 1024);
BufferedOutputStream bos= new BufferedOutputStream(new FileOutputStream("C:\\a.mp3"));
//读取一个字节写入一个字节
//一次读取一个字节,写一个字节来复制音乐
int len = 0;//用来记录读取到的字节
long start = System.currentTimeMillis();
byte[] bytes = new byte[1024 * 8];
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes,0,len);
bos.flush();
}
long end = System.currentTimeMillis();
bos.close();
bis.close();
System.out.println("复制完成耗时" + (end - start) + "毫秒");
} private static void copy1() throws IOException {
File file = new File("烟花易冷Live_林志炫.mp3");
FileInputStream in = new FileInputStream(file);
FileOutputStream out = new FileOutputStream("C:\\Users\\ShenMouMou\\Desktop\\" + file.getName());
int len = 0;//用来记录读取到的字节
byte[] bytes = new byte[1024 * 8];
long start = System.currentTimeMillis();
while ((len = in.read(bytes)) != -1) {
out.write(bytes,0,len);
out.flush();
}
long end = System.currentTimeMillis();
in.close();
out.close();
System.out.println("复制完成耗时" + (end - start) + "毫秒");
}
}

IO流——字节流的更多相关文章

  1. JAVA之IO流(字节流)

    输入和输出 JAVA的流分为输入流和输出流两部分, 输入流:InputStream或者Reader:从文件中读到程序中: 输出流:OutputStream或者Writer:从程序中输出到文件中: Re ...

  2. IO流(字节流,字符流,缓冲流)

    一:IO流的分类(组织架构) 根据处理数据类型的不同分为:字节流和字符流 根据数据流向不同分为:输入流和输出流   这么庞大的体系里面,常用的就那么几个,我们把它们抽取出来,如下图:   二:字符字节 ...

  3. Java之IO流(字节流,字符流)

    IO流和Properties IO流 IO流是指计算机与外部世界或者一个程序与计算机的其余部分的之间的接口.它对于任何计算机系统都非常关键, 因而所有 I/O 的主体实际上是内置在操作系统中的.单独的 ...

  4. Java IO流-字节流

    2017-11-05 17:48:17 Java中的IO流按数据类型分类分为两种,一是字节流,二是字符流.字符流的出现是为了简化文本数据的读入和写出操作. 如果操作的文件是文本文件,那么使用字符流会大 ...

  5. Java—IO流 字节流

    IO流(输入流.输出流),又分为字节流.字符流. 流是磁盘或其它外围设备中存储的数据的源点或终点. 输入流:程序从输入流读取数据源.数据源包括外界(键盘.文件.网络…),即是将数据源读入到程序的通信通 ...

  6. IO流——字节流、字符流

    在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成. 流的分类 ①   流按其流向分为“输入流”和“输出 ...

  7. JAVA基础——IO流字节流

    在Java中把不同的输入输出源(键盘.文件.网路连接)抽象表述为“流”. 1.输入流.输出流 .字节输入流通过FileInputStream和来操作 字节输出流通过FileOutputStream来操 ...

  8. java IO流——字节流

    字节流主要操作byte类型数据,以byte数组为准,主要操作类有InputStream(字节输入流).OutputSteam(字节输出流)由于IputStream和OutputStream都是抽象类, ...

  9. 1(2)IO流---字节流

    一.分类 字节流(不适用于文本) InputStream OutputStream 字符流 Reader Writer  二.字节流 1.概述     InputStream 字节输入流,是被读的,抽 ...

随机推荐

  1. C#中字符串的操作大全

    一.C#中字符串的建立过程 例如定义变量 strT="Welcome to "; strT+="www.cuit.edu.cn"; 程序首先创建一个System ...

  2. 兼容 .NET Core3.0, Natasha 框架实现 隔离域与热编译操作

    关于 Natasha    动态构建已经成为了封装者们的家常便饭,从现有的开发趋势来看,普通反射性能之低,会迫使开发者转向EMIT/表达式树等构建方式,但是无论是EMIT还是表达式树,都会依赖于反射的 ...

  3. 费劲周折的Haskell开发环境搭建过程

    大概倒腾了一周才搭建好Haskell的开发环境,遇到了很多莫名其妙的问题. 首先,Haskell实在是够冷门,中文网站上的信息实在有限.仅有的一些安装教程分享都感觉不大靠谱,所以我还是直接去外网找吧. ...

  4. Linux目录结构(1)

    /bin[重点](/usr/bin./usr/local/bin):存放常用命令 /sbin:存放的是系统管理员使用的系统管理程序 /home[重点]:存放普通用户的主目录,在linux中每个用户都有 ...

  5. Java 9新特性

    1.jdk 目录结构 JDK9 具体目录结构如下所示: bin: 该目录包含所有的命令. conf: 包含用户可以编辑的配置文件,例如以前位于 jre\lib 目录中的.properties 和 .p ...

  6. ADO.NET 根据实体类自动生成添加修改语句仅限Oracle使用

    话不多说直接上代码,新手上路,高手路过勿喷,请多多指教. /// <summary> /// 等于号 /// </summary> ) + Convert.ToChar() + ...

  7. MUI下拉菜单样式

    <div class="mui-input-row my_select"> <label style="width: 47px;padding-righ ...

  8. 用jTessBoxEditorFX训练字库

    软件下载:https://sourceforge.net/projects/vietocr/files/jTessBoxEditor/ 官方字库下载:https://github.com/tesser ...

  9. CentOS7 firewalld防火墙规则

    在CentOS7里有几种防火墙共存:firewalld.iptables.ebtables,默认是使用firewalld来管理netfilter子系统,不过底层调用的命令仍然是iptables等. f ...

  10. ASP.NET Core MVC 502 bad gateway 超时如何处理

    在网页程序运行需要较长时间运行的时候,ASP.NET Core MVC会出现502 bad gateway请求超时情况.一般默认的超时时间都比较短,我们需要在 web.config 中配置一下.其中  ...