这个连接包含了常用的流------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();
}
}