io工具类

文章目录

以字符串的形式读取文件内容

java 复制代码
// import java.io.*;
public static String file2Str(File file, String encoder) {
	StringBuilder sb = new StringBuilder();
	BufferedReader in = null;
	try {
		in = new BufferedReader(
				new InputStreamReader(
						new FileInputStream(file),
						encoder
				)
		);
		String str = in.readLine();
		while (str != null) {
			sb.append(str);
			str = in.readLine();
		}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (in != null) {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	return sb.toString();
}

字符串写入文件

java 复制代码
// import java.io.*;
public static void str2File(File file, String data, String encoder) {
	BufferedWriter out = null;
	try {
		out = new BufferedWriter(
				new OutputStreamWriter(
						new FileOutputStream(file),
						encoder
				)
		);
		out.write(data);
		out.flush();
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (out != null) {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

读取文件成字节数组

java 复制代码
// import java.io.*;
public static byte[] file2Byte(File file) {
	byte[] data = null;
	FileInputStream in = null;
	try {
		in = new FileInputStream(file);
		data = new byte[in.available()];
		in.read(data);
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (in != null) {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	return data;
}

将字节数组写入文件

java 复制代码
// import java.io.*;
public static void byte2File(File file, byte[] data) {
	FileOutputStream outputStream = null;
	try {
		outputStream = new FileOutputStream(file);
		outputStream.write(data);
		outputStream.close();
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (outputStream != null) {
			try {
				outputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

按行读取文件成list

java 复制代码
// import java.io.*;
// import java.util.List;
public static List<String> file2List(File file, String encoder) {
	List<String> alline = new ArrayList<>();
	BufferedReader in = null;
	try {
		in = new BufferedReader(
				new InputStreamReader(new FileInputStream(file), encoder)
		);
		String str = in.readLine();
		while (str != null) {
			alline.add(str);
			str = in.readLine();
		}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (in != null) {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	return alline;
}

输出list到文件

java 复制代码
// import java.io.*;
// import java.util.List;
public static void list2File(File file, List<String> data, String encoder) {
	BufferedWriter out = null;
	try {
		out = new BufferedWriter(
				new OutputStreamWriter(new FileOutputStream(file), encoder)
		);
		for (String str : data) {
			out.write(str);
			out.newLine();
		}
		out.flush();
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (out != null) {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

从标准输入中读入

java 复制代码
// import java.io.*;
public static String system2Str() throws IOException {
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    return stdin.readLine();
}

文件转换为base64

java 复制代码
// import java.io.*;
public static byte[] getBase64(File file) {
    FileInputStream fileInputStream = null;
    byte[] bytes = null;
    try {
        fileInputStream = new FileInputStream(file);
        bytes = new byte[fileInputStream.available()];
        fileInputStream.read(bytes);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (fileInputStream != null) {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return bytes;
}

文件压缩

java 复制代码
//import java.io.*;
//import java.util.zip.ZipEntry;
//import java.util.zip.ZipOutputStream;
/**
 * @param inputFilePath C:\Users\26294\Desktop\bb
 * @param zipFileName C:\Users\26294\Desktop\test.zip
 * @throws Exception
 */
public static void zip(String inputFilePath, String zipFileName)
        throws Exception {
    File srcFile = new File(inputFilePath);
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
    if (srcFile.isDirectory()) {
        zip(out, srcFile, "");
    } else {
        zip(out, srcFile, srcFile.getName());
    }
    out.close();
}

private static void zip(ZipOutputStream out, File srcFile, String base)
        throws Exception {
    if (srcFile.isDirectory()) {
        File[] files = srcFile.listFiles();
        if (!"".equals(base)) {
            out.putNextEntry(new ZipEntry(base + "/"));
            base = base + "/";
        }
        for (File file : files) {
            System.out.println(file.getAbsolutePath());
            zip(out, file, base + file.getName());
        }
    } else {
        out.putNextEntry(new ZipEntry(base));
        FileInputStream inputStream = new FileInputStream(srcFile);
        int b;
        while ((b = inputStream.read()) != -1) {
            out.write(b);
        }
        inputStream.close();
    }
}
相关推荐
SomeB1oody5 分钟前
【RustyML入门】6.3. 并行归约
开发语言·后端·机器学习·rust·教程
东小西25 分钟前
【SAA实战】第 1 篇:ReactAgent 入门——先撸个"会调工具的助手"跑起来
java·人工智能·spring
mqiqe1 小时前
AgentScope Java 2.0 协议集成全景解析:A2A、AG-UI、Agent Protocol 三大开放协议实战指南
java·开发语言·ui
lzhdim1 小时前
12、JavaScript常见的内存泄露问题 - JavaScript学习系列文章
开发语言·前端·javascript·学习·ecmascript
脉动数据行情1 小时前
Java 实现台股 TWSE/TPEx 行情采集(个股 + K 线)
java·开发语言·twse·tpex·台股
爱学习的小邓同学2 小时前
Golang --- (1)第一个Golang程序
开发语言·后端·golang
zx_741484812 小时前
【Python 入门】面向对象基础:类、对象、成员变量与构造方法
开发语言·python
月华路3 小时前
G1 GC 对数组与大对象(Humongous)的处理
java·jvm·算法
liangshanbo12154 小时前
虚拟列表深度面试题整理
java·开发语言·前端
ZISHU_9874 小时前
用 TLabel 给 SynTouch BioTac 数据做语义标注:从原始信号到结构化标注
开发语言·人工智能·python·数据·机器人触觉