---
title: "去除bom方法"
author: "mapengfei00099"
date: "2013-07-08T09:24:26Z"
tags: []
categories: ["编程语言"]
slug: "bom"
draft: false
lastmod: "2013-07-08T09:24:26Z"
id: 1901171
url: "https://mapengfei00099.iteye.com/blog/1901171"
---

```
	public static void trimBom(String fileName) throws IOException {

		FileInputStream fin = new FileInputStream(fileName);
		// 开始写临时文件
		InputStream in = fin;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte b[] = new byte[4096];

		int len = 0;
		while (in.available() > 0) {
			len = in.read(b, 0, 4096);
			// out.write(b, 0, len);
			bos.write(b, 0, len);
		}

		in.close();
		fin.close();
		bos.close();
		// 临时文件写完，开始将临时文件写回本文件。
		FileOutputStream out = new FileOutputStream(fileName);
		out.write(bos.toByteArray());
		out.close();

	}
```