流是一个抽象 动态的概念 是一连串连续动态的数据集合。 输入流:通过流将数据源中的数据输送到程序 输出流:通过流将程序中的数据输送到目的数据源
InputStram
OutputStream
Reader
Writer
节点流也被成为低级流,是可以从/向一个特定的IO设备(如磁盘、网络)读/写数据的流。
处理流也被称为高级流,处理流是对一个已存在的流进行连接或封装,通过封装后的流来实现数据读/写功能。
public class IO_Demo {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("D:\\Programs\\java\\idea_workplace\\java基础学习\\Thread\\src\\com\\kk\\demo01\\a.txt");
int s1 = fis.read();
int s2 = fis.read();
int s3 = fis.read();
int s4 = fis.read();
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
} catch (Exception e) {
e.printStackTrace();
}finally {
if (fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public class IO_Demo2 {
public static void main(String[] args) {
FileInputStream fis = null;
try{
fis = new FileInputStream("D:\\Programs\\java\\idea_workplace\\java基础学习\\Thread\\src\\com\\kk\\demo01\\a.txt");
StringBuilder sb = new StringBuilder();
int temp = 0;
while((temp = fis.read()) != -1){
System.out.println(temp);
sb.append((char)temp);
}
System.out.println(sb.toString());
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if (fis != null){
fis.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}
public class FileDemo {
public static void main(String[] args) throws IOException {
File file = new File("D:\\Programs\\java\\idea_workplace\\java基础学习\\Thread\\src\\com\\kk\\demo01\\a2.txt");
System.out.println(file.delete());
System.out.println(file.createNewFile());
System.out.println(file.exists());
System.out.println(file.getName());
}
}
public class FileDemo {
public static void main(String[] args) throws Exception {
FileInputStream fis = null;
fis = new FileInputStream("D:\\Programs\\java\\idea_workplace\\java基础学习\\Thread\\src\\com\\kk\\demo01\\2022-07-22-14-04-32.png");
//创建文件自洁输出流对象
FileOutputStream fos = new FileOutputStream("D:\\Programs\\java\\idea_workplace\\java基础学习\\Thread\\src\\com\\kk\\demo01\\2022-07-22-14-04-32_copy.png");
File file = new File("D:\\Programs\\java\\idea_workplace\\java基础学习\\Thread\\src\\com\\kk\\demo01\\2022-07-22-14-04-32_copy.png");
if (fos!=null){
file.delete();
}
int temp=0;
while((temp=fis.read())!=-1){
fos.write(temp);
}
if (fis!=null){
fis.close();
}if (fos!=null){
fos.close();
}
}
}
public class FileDemo {
public static void main(String[] args) throws Exception {
FileInputStream fis = null;
fis = new FileInputStream("D:\\Programs\\java\\idea_workplace\\java基础学习\\Thread\\src\\com\\kk\\demo01\\2022-07-22-14-04-32.png");
//创建文件自洁输出流对象
FileOutputStream fos = new FileOutputStream("D:\\Programs\\java\\idea_workplace\\java基础学习\\Thread\\src\\com\\kk\\demo01\\2022-07-22-14-04-32_copy.png");
//创建一个缓冲区
byte[] buff = new byte[1024];
int temp=0;
while((temp=fis.read(buff))!=-1){
fos.write(buff,0,temp);
}
fos.flush();
if (fis!=null){
fis.close();
}if (fos!=null){
fos.close();
}
}
}
```
一口气将文件保存在缓冲区相当于一次性读取完全部字节
public class FileDemo {
public static void main(String[] args) throws Exception {
FileInputStream fis = null;
fis = new FileInputStream("D:\\Programs\\java\\idea_workplace\\java基础学习\\Thread\\src\\com\\kk\\demo01\\2022-07-22-14-04-32.png");
//创建文件自洁输出流对象
FileOutputStream fos = new FileOutputStream("D:\\Programs\\java\\idea_workplace\\java基础学习\\Thread\\src\\com\\kk\\demo01\\2022-07-22-14-04-32_copy.png");
//创建一个缓冲区
byte[] buff = new byte[fis.available()];
fis.read(buff);
fos.write(buff);
fos.flush();
if (fis!=null){
fis.close();
}if (fos!=null){
fos.close();
}
}
}
BufferedInputStream 和 BufferedOutputStream
public class FileStreamBuffer {
public static void main(String[] args) throws Exception{
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
fis = new FileInputStream("D:\\Programs\\java\\idea_workplace\\java基础学习\\Thread\\src\\com\\kk\\demo01\\2022-07-22-14-04-32.png");
bis = new BufferedInputStream(fis);
fos = new FileOutputStream("D:\\Programs\\java\\idea_workplace\\java基础学习\\Thread\\src\\com\\kk\\demo01\\2022-07-22-14-04-32_copy.png");
bos = new BufferedOutputStream(fos);
//缓冲区中,byte数组长度默认是8192
int temp=0;
while((temp = bis.read())!=-1){
bos.write(temp);
}
//关闭顺序:后开先关
if (bis != null){
bis.close();
}if (fis != null){
fis.close();
}if (bos != null){
bos.close();
}if (fos != null){
fos.close();
}
}
}
本文章使用limfx的vscode插件快速发布