深藍的天空下,有你有我...
          共享酸、甜、苦、辣
          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
          主站蜘蛛池模板: 婺源县| 溆浦县| 锦屏县| 班玛县| 保康县| 成安县| 醴陵市| 楚雄市| 阜康市| 霍州市| 古交市| 新沂市| 梨树县| 上蔡县| 大田县| 兴化市| 吉林市| 唐海县| 景宁| 大关县| 阳西县| 邵东县| 宁安市| 庆城县| 阳信县| 和静县| 思茅市| 灵石县| 芦山县| 鄂托克前旗| 美姑县| 建瓯市| 全州县| 沙雅县| 武邑县| 绥芬河市| 汝南县| 洛阳市| 井冈山市| 江门市| 东阿县|