0
点赞
收藏
分享

微信扫一扫

基本字节流(FileOutputStream,FileInputStream)



这个连接包含了常用的流------IO流(总篇章) 



FileOutputStream


创建字节输出流对象(调用系统功能创建文件,创建字节输出流对象,让字节输出流对象指向文件)调用字节输出流对象的写数据方法释放资源(关闭此文件输出流并释放与此流相关联的任何系统资源)


package com.testIO;

import java.io.*;

/**
* @author 林高禄
* @create 2020-05-09-11:10
*/
public class FileOutputStreamDemo {
public static void main(String[] args) {
FileOutputStream fos = null;
try {
// true表示追加。false表示覆盖
fos = new FileOutputStream("test//src//com//testIO//a.txt",true);
// 写数据
fos.write("林高禄".getBytes());
/**
* 换行符
* window:\r\n
* linux:\n
* mac:\r
*/
fos.write("\r\n".getBytes());
fos.write("阿里云".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != fos) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}


 FileinputStream


  • 创建字节输入流对象
  • 调用字节输入流对象的读数据方法
  • 释放资源


package com.testIO;

import java.io.FileInputStream;
import java.io.IOException;

/**
* @author 林高禄
* @create 2020-05-09-11:10
*/
public class FileInputStreamDemo {
public static void main(String[]rgs) throws IOException {
FileInputStream fis = new FileInputStream("test//src//com//testIO//a.txt");
byte[] bys = new byte[1024];
int len;
while ((len=fis.read(bys))!= -1){
System.out.println(new String(bys,0,len));
}
fis.close();
}
}


复制图片 



package com.testIO;

import java.io.*;

/**
* @author 林高禄
* @create 2020-05-09-11:10
*/
public class Copy {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("test//src//com//testIO//aa.png");
FileOutputStream fos = new FileOutputStream("test//src//com//testIO//bb.png");
byte[] bys = new byte[1024];
int len;
while ((len=fis.read(bys))!= -1){
fos.write(bys,0,len);
}
fos.close();
fis.close();
}
}



举报

相关推荐

0 条评论