深藍的天空下,有你有我...
          共享酸、甜、苦、辣
          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 三刀流の逆風 閱讀(1124) 評論(0)  編輯  收藏 所屬分類: JAVA
          主站蜘蛛池模板: 保定市| 达日县| 合阳县| 崇礼县| 庆阳市| 襄垣县| 泽库县| 横峰县| 自治县| 剑河县| 左云县| 浦江县| 辽阳县| 正宁县| 丁青县| 米脂县| 兰州市| 丹江口市| 武鸣县| 清涧县| 长沙市| 河津市| 鹤庆县| 中方县| 沙河市| 都江堰市| 怀化市| 江华| 汝南县| 宝清县| 六枝特区| 辽中县| 新泰市| 吴忠市| 安新县| 衡山县| 郴州市| 盐亭县| 徐闻县| 扶绥县| 保康县|