Java中的I-O(一)

30 篇文章 1 订阅

一、File类的常用方法

方法名称说明
boolean exists( )判断文件或目录是否存在
boolean isFile( )判断是否是文件
boolean isDirectory( )判断是否是目录
String getPath( )返回此对象表示的文件的相对路径名
String getAbsolutePath( )返回此对象表示的文件的绝对路径名
String getName( )返回此对象表示的文件或目录的名称
boolean delete( )删除此对象指定的文件或目录
boolean createNewFile( )创建名称的空文件,不创建文件夹
long length()返回文件的长度,单位为字节, 如果文件不存在,则返回 0L

二、文件的读取方式

通过流来读写文件
  流是一组有序的数据序列,以先进先出方式发送信息的通道

在这里插入图片描述

三、Java流的分类

按流向分
输入流
OutputStream作为基类
输入流
InputStream作为基类

输入输出流是相对于计算机内存来说的

按照处理数据单元划分
字节流
字节输入流InputStream基类
字节输出流OutputStream基类
字符流
字符输入流Reader基类
字符输处流Writer基类

字节流是 8 位通用字节流,字符流是 16 位 Unicode 字符流

四、FileInputStream

1、InputStream类常用方法

int read( )
int read(byte[] b)
int read(byte[] b,int off,int len)
void close( )
int available():可以从输入流中读取的字节数目

2、子类FileInputStream常用的构造方法

FileInputStream(File file)
FileInputStream(String name)

3、实现步骤

引入相关的类
构造文件输入流FileInputStream对象
读取文本文件的数据
关闭文件流对象

4、代码节选

/**
 * @description 读文件的方法
 * @author Jule_zhou
 * @date 2022-08-12 17:27:25
 * @param file 文件位置
 * @return {@link byte[]} 读取出的字节文件,可以通过String str = new String(byte[] a);语句转换为字符串类型
 */
public byte[] read(File file){
    FileInputStream fileInputStream = null;
    byte[] bytes = null;
    try {
        // 创建文本读取对象
        fileInputStream = new FileInputStream(file);
        // 创建缓冲的字符型数组,长度为可以从输入流中读取的字节数目
        bytes = new byte[fileInputStream.available()];
        // 读取文件中的内容
        fileInputStream.read(bytes,0,bytes.length);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        try {
            if (fileInputStream != null) {
                // 关闭文件
                fileInputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return bytes;
}

五、FileOutputStream

1、OutputStream类常用方法

void write(int c)
void write(byte[] buf)
void write(byte[] b,int off,int len)
void close()
void flush():强制把缓冲区的数据写到输出流中

2、子类FileOutputStream常用的构造方法

FileOutputStream (File file)
FileOutputStream(String name)
FileOutputStream(String name,boolean append)

3、实现步骤

引入相关的类
构造文件输出流FileOutputStream对象
把数据写入文本文件
关闭文件流对象

4、代码节选

/**
 * @description 文件写入
 * @author Jule_zhou
 * @date 2022-08-12 17:10:13
 * @param s 要输入的内容
 * @param file 文件位置
 * @return
 */
public void write(String s,File file){
    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(file);
        byte[] bytes = s.getBytes();
        // 将字符串中的内容写入文件
        fileOutputStream.write(bytes);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fileOutputStream != null) {
                // 关闭文件
                fileOutputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

六、完整代码示例

package *;

import java.io.*;
import java.util.Scanner;
/**
 * @authorDesc 收获源于是每一分的努力
 * @author Jule_zhou
 * @date 2022-08-13 17:06:18
 * @version
 * @description 文件处理
 */
public class Copy {
    static Scanner scan = new Scanner(System.in);
    /**
     * @description 文件写入
     * @author Jule_zhou
     * @date 2022-08-12 17:10:13
     * @param s 要输入的内容
     * @param file 文件位置
     * @return
     */
    public void write(String s,File file){
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);

            byte[] bytes = s.getBytes();
            // 将字符串中的内容写入文件
            fileOutputStream.write(bytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileOutputStream != null) {
                    // 关闭文件
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * @description 读文件的方法(一)
     * @author Jule_zhou
     * @date 2022-08-12 17:27:25
     * @param file 文件位置
     * @return {@link byte[]} 读取出的字节文件,可以通过String str = new String(byte[] a);语句转换为字符串类型
     */
    public byte[] read(File file){
        FileInputStream fileInputStream = null;
        byte[] bytes = null;
        try {
            // 创建文本读取对象
            fileInputStream = new FileInputStream(file);
            // 创建缓冲的字符型数组,长度为可以从输入流中读取的字节数目
            bytes = new byte[fileInputStream.available()];
            // 读取文件中的内容
            fileInputStream.read(bytes,0,bytes.length);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (fileInputStream != null) {
                    // 关闭文件
                    fileInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bytes;
    }
    public static void main(String[] args) {
        Copy copy = new Copy();
        // 创建文件位置对像
        File fileRead = new File("D:\\我的青春谁做主.txt");
        File fileWrite = new File("D:\\my Prime.txt");

        System.out.print("写入《我的青春谁做主.txt》的内容:");
        String str = scan.next();
        copy.write(str,fileRead);
        System.out.println("------------读取《我的青春谁做主.txt》中的的内容------------");
        byte[] bytes = copy.read(fileRead);
        System.out.println("------------输出《我的青春谁做主.txt》中的的内容------------");
        str = new String(bytes);
        System.out.println(str);
        System.out.println("------------写入《my Prime.txt》中------------");
        copy.write(str,fileWrite);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jule_zhou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值