io工具类

这篇文章详细介绍了如何在Java中进行文件操作,包括读取、写入字符串和字节,使用Base64编码,以及文件的压缩。展示了从文件读取、写入到列表,以及标准输入的处理方法。
摘要由CSDN通过智能技术生成

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

// 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();
}

字符串写入文件

// 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();
			}
		}
	}
}

读取文件成字节数组

// 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;
}

将字节数组写入文件

// 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

// 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到文件

// 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();
			}
		}
	}
}

从标准输入中读入

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

文件转换为base64

// 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;
}

文件压缩

//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();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值