深藍(lán)的天空下,有你有我...
          共享酸、甜、苦、辣
          posts - 23,comments - 19,trackbacks - 0
          import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class FileByteUtil { public static void main(String[] args) throws Exception { File file=new File( "f:/test.doc "); byte[] fileByte = file2byte(file); byte2file(fileByte, "f:/test2.doc "); } public static byte[] file2byte(File f) throws Exception { return file2byte(f.getPath()); } public static byte[] file2byte(String f) throws Exception { try { InputStream in = new FileInputStream(f); byte[] tmp = new byte[512]; ByteArrayOutputStream out = new ByteArrayOutputStream(); int bytesRead = in.read(tmp); while (bytesRead != -1) { out.write(tmp, 0, bytesRead); bytesRead = in.read(tmp); } return out.toByteArray(); } catch (Exception e) { e.printStackTrace(); } return null; } // writes byte [] to a file public static void byte2file(byte[] data, String fn) throws Exception { try { OutputStream out = new FileOutputStream(fn); out.write(data); out.flush(); } catch (FileNotFoundException e) { throw e; } catch (IOException e) { throw e; } } }
          posted on 2011-06-19 02:01 三刀流の逆風(fēng) 閱讀(1120) 評論(0)  編輯  收藏 所屬分類: JAVA
          主站蜘蛛池模板: 泌阳县| 蓬溪县| 吉木萨尔县| 水富县| 广元市| 扬中市| 汶川县| 偏关县| 海林市| 福州市| 图们市| 漯河市| 安化县| 增城市| 荆门市| 东至县| 尚志市| 新和县| 阳东县| 奉新县| 汾西县| 荔波县| 淄博市| 称多县| 临沧市| 达拉特旗| 宜阳县| 华亭县| 鄱阳县| 温泉县| 新乡县| 文安县| 奉新县| 息烽县| 泸西县| 沙湾县| 裕民县| 栾城县| 平乡县| 桦甸市| 昌黎县|