利用输入输出流对文件操作的java代码

该代码示例展示了如何使用Java进行文件读取和写入。`InReader`类提供了读取文件内容为字符串和字节数组的方法,以及获取目录下所有文件名的功能。而`OutWriter`类则演示了如何利用字节流将内容写入文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、对文件读取的例子

package org.example;

import javax.crypto.SecretKey;
import java.io.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : Future master
 * @version : 1.0
 * @Project : TestThree
 * @Package : org.example
 * @ClassName : GetAes.java
 * @createTime : 2021/11/24 23:09
 * @Email : 2467636181@qq.com
 */
public class InReader {

//读取一个目录下的所有文件的内容并且返回一个字符串数组
    public String[] getFiles(File file) throws IOException {
        String[] strings = null;
        if(file.isDirectory()){
            File[] files = file.listFiles();
            strings = new String[files.length];
            for(int i = 0;i<files.length;i++){
                strings[i] = getFile(files[i]);
            }
        }
        return strings;
    }
//读取一个文件中的内容并且返回一个字符串
    public String getFile(File file) throws IOException {
        String str1 = null;
        if (file.isFile()){
            int length = new Long(file.length()).intValue();
            char[] content = new char[length];
            InputStream input = new FileInputStream(file);
            Reader reader = new InputStreamReader(input,"UTF-8");
            int len = reader.read(content);
            str1 = new String(content,0,len);
            reader.close();
            input.close();

        }
        return str1;
    }
//利用字节流对文件进行一个读取,返回字节数组
    public byte[] getFileByte(File file) throws IOException {
        byte[] bytes = null;
        if(file.isFile()){
            int length = new Long(file.length()).intValue();
            bytes = new byte[length];
            InputStream input = new FileInputStream(file);
            input.read(bytes);
            input.close();
        }
        return bytes;
    }
//读取一个目录下的所有文件名,并且返回一个字符串数组
    public String[] getFileName(File file){
        String[] names = null;
        if(file.isDirectory()){
            File[] files = file.listFiles();
            names = new String[files.length];
            for(int i = 0;i<files.length;i++){
                names[i] = files[i].getName();
            }
        }
        return names;
    }
}

二、对文件进行写入(利用字节流)的例子

package org.example;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : Future master
 * @version : 1.0
 * @Project : TestThree
 * @Package : org.example
 * @ClassName : OutWriter.java
 * @createTime : 2021/11/24 23:48
 * @Email : 2467636181@qq.com
 */
public class OutWriter {
    public void outWrite(byte[] content,File file) throws IOException {
        if(file.isFile()){
            OutputStream out = new FileOutputStream(file);
            BufferedOutputStream bufferOut = new BufferedOutputStream(out);
            bufferOut.write(content);
            bufferOut.flush();
            bufferOut.close();
            out.close();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

躺平崽

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

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

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

打赏作者

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

抵扣说明:

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

余额充值