首先我们要清楚什么是流,正如其名,很形象,流就是像水一样的东西,具有方向性,在java中

,流大概就是类

接下来,我们要对输入输出流有一个基本认识,什么是输入输出流呢?

输入输出明显需要一个参照,而这个参照就是主存。

清楚了上面的概念,我们接着看下去吧。

文件

文件的创建

文件创建共有三种方式

1、

File file = new File(文件的路径);

file.createNewFile();

2、

File file = new File(文件的父目录, 文件名);

3、

File file = new File(文件的父目录, 文件的子目录);

package file;

import org.junit.jupiter.api.Test;
import java.awt.*;
import java.io.File;
import java.io.IOException; public class FileCreate {
public static void main(String[] args) { }
//@Test
public void create01() throws IOException {
String filePath = "E:\\new1.txt";
File file = new File(filePath);
file.createNewFile();
System.out.println("文件创建成功");
}
//@Test
public void create02() throws IOException {
File file1 = new File("E:\\");
String fileName = "new2.txt";
File file = new File(file1, fileName);
file.createNewFile();
System.out.println("文件创建成功2");
}
@Test
public void create03()
{
String FilePath = "E:\\";
String FileName = "new3.txt";
File file = new File(FilePath, FileName);
try {
file.createNewFile();
System.out.println("文件创建成功3");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

文件信息的获取

package file;

import java.io.File;

public class FileInformation {
public static void main(String[] args) {
FileInformation fileInformation = new FileInformation();
fileInformation.info();
}
public void info()
{
File file = new File("E:\\new1.txt");
System.out.println("文件名=" + file.getName());
System.out.println("文件的句=绝对路径" + file.getAbsoluteFile());
System.out.println("文件的父目录" + file.getParentFile());
System.out.println("文件的大小" + file.length());
System.out.println("文件是否存在" + file.exists());
System.out.println("是否是一个文件" + file.isFile());
System.out.println("是否是一个目录" + file.isDirectory());
}
}

目录文件的创建和删除

删除

package file;

import com.sun.javafx.image.BytePixelSetter;
import org.junit.jupiter.api.Test; import java.io.File; public class Directory_ {
public static void main(String[] args) { }
@Test
public void f1()
{
String filePath = "E:\\new1.txt";
File file = new File(filePath);
if(file.exists())
{
if(file.delete()) System.out.println("删除成功");
else System.out.println("删除成功");
}else{
System.out.println("该文件不存在");
}
}
}

目录创建

mkdir创建一级目录、mkdirs创建多级目录

@Test
public void f3()
{
String filePath = "D:\\demo\\a\\b\\c";
File file = new File(filePath);
if(file.exists())
{
System.out.println("目录存在");
}else {
if(file.mkdirs()) System.out.println("该目录创建成功");
else System.out.println("该目录创建失败");
}
}

IO流

流的分类

1、按照数据单位不同分为字节流(操作二进制的文件)和字符流(一个字符占多少个字节是不确定的这和编码有点关系,操作文本文件)

2、按流向单位不同分为输入流和输出流。

3、按流的角色分:节点流和处理流(这里角色改没有引入下面会介绍)

InputStream和OutStream、Reader、Writer是四个个抽象类,其他类均是实现的这四个Abstract类,均以他们作为后缀名



输入流

字节输入流

package inputstream;

import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; public class FileInputStream_ {
public static void main(String[] args) {
}
//@Test
public void readFile01() throws IOException {
String filePath = "D:\\hello.txt";
FileInputStream fileInputStream = null;
int readdata = 0;
try {
fileInputStream = new FileInputStream(filePath);
//返回-1表示文件读完了
while((readdata = fileInputStream.read()) != -1){
System.out.print((char) readdata);
}
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
//关闭文件流
fileInputStream.close();
}
}
@Test
public void readFile02() throws IOException {
String filePath = "D:\\hello.txt";
byte[] buf = new byte[8];
FileInputStream fileInputStream = null;
int readlen = 0;
try {
fileInputStream = new FileInputStream(filePath);
//返回-1表示文件读完了
while((readlen = fileInputStream.read(buf)) != -1){
System.out.print(new String(buf, 0, readlen));
}
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
//关闭文件流
fileInputStream.close();
}
}
}

字节输出流

package outputstream_;

import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; public class FileOutputStream01 {
public static void main(String[] args) { }
@Test
public void writeFile() throws IOException {
String filePath = "D:\\a.txt";
FileOutputStream fileOutputStream = null;
try {
String str = "Hello World";
fileOutputStream = new FileOutputStream(filePath);
//写一个字符
// fileOutputStream.write('C');
//写一个字符串
fileOutputStream.write(str.getBytes());
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
fileOutputStream.close();
}
}
}
        fileOutputStream = new FileOutputStream(filePath);是覆盖
        fileOutputStream = new FileOutputStream(filePath, true);是追加到源文件后

应用

package outputstream_;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; public class FileCopy {
public static void main(String[] args) throws IOException {
//文件拷贝,将D:\\1.png拷贝到C:\\
//1、创建文件的输入流,将文件读入程序
//2、创建文件的输出流,将程序中的内容写入文件
String filePath = "D:\\1.jpg";
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
byte[] buf = new byte[1024];
int readlen = 0;
fileInputStream = new FileInputStream(filePath);
fileOutputStream = new FileOutputStream("E:\\1.jpg");
while((readlen = fileInputStream.read()) != -1)
{
//读取后写入文件,边读边写
fileOutputStream.write(buf, 0, readlen);
}
fileOutputStream.close();
fileInputStream.close();
}
}

FileReader

package filereader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; public class FileReader_ {
public static void main(String[] args) throws IOException {
String filePath = "D:\\a.txt";
FileReader fileReader = null;
int data = 0;
try {
fileReader = new FileReader(filePath);
while((data = fileReader.read()) != -1)
{
System.out.print((char) data);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
fileReader.close();
}
}
}

用字符数组来读

package filereader;

import org.junit.jupiter.api.Test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; public class FileReader_ {
public static void main(String[] args){
}
@Test
public void readFile01() throws IOException {
String filePath = "D:\\a.txt";
int readlen = 0;
FileReader fileReader = null;
char [] buf = new char[8];
try {
fileReader = new FileReader(filePath);
while((readlen = fileReader.read(buf)) != -1)
{
System.out.print(new String(buf, 0, readlen));
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
fileReader.close();
}
} }

FileWriter

写入数据之后一定要关流或者刷新数据才能被写入

package filewrite;

import java.io.FileWriter;
import java.io.IOException; public class FileWriter_ {
public static void main(String[] args) {
String path = "D:\\a.txt";
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(path, true);
fileWriter.write("aaaa");
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
fileWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}

节点流和处理流

处理流是连接在已存在的流之上为程序提供更强大的读写功能,包装流更加形象

BufferedReader & BufferedWriter

package BufferedReader_;

import javax.swing.text.Style;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; public class BufferedReader_ {
public static void main(String[] args) throws IOException {
String path = "D:\\a.txt";
BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
String line;
try {
//bufferedReader.readLine();按行读取,当读取到空时,表示读取结束
while((line = bufferedReader.readLine()) != null) System.out.println(line);;
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
bufferedReader.close();
}
}
}
package bufferedreader;

import java.io.*;

public class BufferedReader_ {
public static void main(String[] args) throws IOException {
String path = "D:\\a.txt";
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(path, true)); try {
bufferedWriter.write("阿杜吃饱饱");
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
bufferedWriter.flush();
} }
}

视频、图片等是二进制文件用字节流更方便

对象流

使用细节

1、读写顺序要一致

2、要求实现序列化或反序列化对象,需要实现Serializable

3、序列化默认是将所有属性都序列化,但是static和transient不序列化

4、序列化里面的属性也要是现实Serializable接口

标准输入输出流

转换流

转换流应用的情况:

因为字符流不能指定编码格式,而字节流可以指定编码格式,可以先按字节流读入,再用转换流转化,这样就不会乱码了

Java文件与IO流的更多相关文章

  1. java中的IO流

    Java中的IO流 在之前的时候我已经接触过C#中的IO流,也就是说集中数据固化的方式之一,那么我们今天来说一下java中的IO流. 首先,我们学习IO流就是要对文件或目录进行一系列的操作,那么怎样操 ...

  2. 文件和IO流

    摘要:本文主要介绍了Java的文件处理以及常用的IO流操作. 文件操作 概念 File是数据源(保存数据的地方)的一种,可以表示一个文件,也可以表示一个文件目录. File类只能对文件和文件夹进行创建 ...

  3. 第55节:Java当中的IO流-时间api(下)-上

    Java当中的IO流(下)-上 日期和时间 日期类:java.util.Date 系统时间: long time = System.currentTimeMillis(); public class ...

  4. 第54节:Java当中的IO流(中)

    Java当中的IO流(中) 删除目录 // 简书作者:达叔小生 import java.io.File; public class Demo{ public static void main(Stri ...

  5. 第53节:Java当中的IO流(上)

    Java当中的IO流 在Java中,字符串string可以用来操作文本数据内容,字符串缓冲区是什么呢?其实就是个容器,也是用来存储很多的数据类型的字符串,基本数据类型包装类的出现可以用来解决字符串和基 ...

  6. Java中的IO流总结

    Java中的IO流总结 1. 流的继承关系,以及字节流和字符流. 2. 节点流FileOutputStream和FileInputStream和处理流BufferedInputStream和Buffe ...

  7. Java中的IO流大体介绍

    由于Java中的IO流是在是知识点繁多,所以我大约花了1周的时间将其整理起来.但是整理起来后并不是将完事了,我还是要分字节流和字符流来讲述.然后字节流和字符流中还有是否带有缓冲流. 讲述完IO流后我将 ...

  8. Java中的IO流,Input和Output的用法,字节流和字符流的区别

    Java中的IO流:就是内存与设备之间的输入和输出操作就成为IO操作,也就是IO流.内存中的数据持久化到设备上-------->输出(Output).把 硬盘上的数据读取到内存中,这种操作 成为 ...

  9. Java中的IO流(五)

    上一篇<Java中的IO流(四)>记录了一下Properties类,此类不属于IO流,它属于集合框架.接下来说一下IO流中的其它流 一,打印流PrintStream PrintStream ...

  10. Java中的IO流(六)

    上一篇<Java中的IO流(五)>把流中的打印流PrintStream,PrintWriter,序列流SequenceInputStream以及结合之前所记录的知识点完成了文件的切割与文件 ...

随机推荐

  1. UI自动化 --- UI Automation 基础详解

    引言 上一篇文章UI自动化 --- 微软UI Automation中,介绍了UI Automation能够做什么,且借助 Inspect.exe 工具完成了一个模拟点击操作的Demo,文章结尾也提出了 ...

  2. 【Qt 应用】模仿实现Win10的Wifi列表

    这里使用 Qt 模仿实现了 Win10 系统下的 Wifi 列表,主要用的是 QlistWidget + xml + cmd命令行 实现. 效果 下载地址 https://github.com/con ...

  3. 即构SDK7月迭代:新增支持按通道设置延迟模式,让卡顿大大减少

    即构SDK 7月迭代如期而至,本月SDK更新主要增加了按推流通道设置延迟模式,大大减少了直播卡顿:媒体本地录制新增AAC 格式,可生成更小的录制文件,更易于上传.此外还有多项功能的优化,让用户获得更好 ...

  4. TypeScript: Object is of type 'unknown'.

    错误代码展示 解决方案 将e声明为any类型,如下所示: // 修改蛇的X和Y值 try { this.snake.X = X; this.snake.Y = Y; }catch(e:any){ // ...

  5. 记录一次解决数据库连接池连接泄露BUG

    1 BUG现象 系统并发请求,系统停滞无法使用,所有接口都是无法与后端进行交互的状态,系统并没有宕机 2 BUG的业务流程 插入分数方法 涉及插入表ABCD 加了声明式事务 查询分数方法 涉及表ABC ...

  6. 从0开始,Cloudreve开源云盘在centos7上部署,并可在外网访问(资料整合)

    全程我在网络上收集这些资料,太零碎了,每一个一看就会,一动手就废,而且很多都不能实现我白嫖的梦想 我一个人折腾了快一周,现在可以正常访问手机电脑多端访问 给个赞再走吧 此处为没有公网IP(回去折腾你家 ...

  7. 震惊!强大的接口自动化测试框架2.0,unittest与pytest无缝穿插对接,可以像postman一样编写代码

    theme: fancy highlight: arta 项目介绍 接口自动化测试项目2.0 软件架构 本框架主要是基于 Python + unittest + ddt + HTMLTestRunne ...

  8. 头疼!卷积神经网络是什么?CNN结构、训练与优化一文全解

    本文全面探讨了卷积神经网络CNN,深入分析了背景和重要性.定义与层次介绍.训练与优化,详细分析了其卷积层.激活函数.池化层.归一化层,最后列出其训练与优化的多项关键技术:训练集准备与增强.损失函数.优 ...

  9. numpy中计算相关系数的np.corrcoef

    np.corrcoef的作用 计算 Pearson 乘积矩相关系数.它可以用来分析给定数据集中各个变量之间的线性相关程度,返回一个相关系数矩阵,相关系数矩阵中的值介于 -1 到 1 之间,包括 -1 ...

  10. HDU 1171 0-1背包

    最近感觉DP已经完全忘了..各种爆炸,打算好好复习一发,0-1背包开始 Big Event in HDU Problem Description Nowadays, we all know that ...