文件输出流

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) + "毫秒");
}
}

最新文章

  1. 关于 iframe 在ie11 height:100% 无效的巨坑
  2. android studio 导入项目太慢(卡条)
  3. fastclick与zepto的 tap事件关于在手机上click事件的300ms延迟的区别
  4. 微信小程序-数据缓存
  5. jquery 实现 返回顶部
  6. Apache多站点配置(ubuntu)
  7. jQuery自动加载更多程序
  8. PC端和移动端地址适配
  9. Asp.net MVC Razor模板引擎技巧分享
  10. [LeetCode] Simplify Path(可以不用看)
  11. HTML5桌面通知:notification api
  12. on the wane
  13. Django ORM模型:想说爱你不容易
  14. 洛谷P1395 会议 题解
  15. PHP中array_merge和array+array的区别
  16. be动词的形式和用法
  17. AlertWindowManager 弹出提示窗口使用帮助(上)
  18. 介绍Collection框架的结构;Collection 和 Collections的区别
  19. ShaderLab学习总结
  20. ZOJ 4016 Mergeable Stack(利用list模拟多个栈的合并,STL的应用,splice函数!!!)

热门文章

  1. C#/.Net操作MongoDBHelper类
  2. DjangoDRF之视图总结
  3. getOwnPropertyDescriptor
  4. vue 利用路由跨页传参
  5. REDELK的安装和使用
  6. Test CMake run finished with errors
  7. django_restframework项目之数据库搭建(二)
  8. MySQL复制技术
  9. ansible自动化运维02
  10. istio部署-helm