文件传输 博客分类: java基础 文件传输
程序员文章站
2024-03-25 11:53:58
...
将文件转换为二进制的文件,并将二进制写进文件
package ceshiOA; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.nio.ByteBuffer; import java.util.Arrays; public class Test { public static void main(String[] args) {doIt(5);} private static void doIt(int t){ Test test = new Test(); byte[] buffers =test.file2byte(new File("res/DocumentManager.rar")); OutputStreamWriter writer = null ; try{ int num = buffers.length %t == 0 ? buffers.length /t : buffers.length/t+1; for(int j = 0 ; j < t ; j ++){ writer = new OutputStreamWriter(new FileOutputStream("res/ProjectFiles/bytenumber("+j+").txt" , false) , "utf-8"); if(j == t-1){ byte[] buffer = new byte[buffers.length - num*j] ; System.arraycopy(buffers, num*j, buffer, 0, buffer.length); int forNum = buffer.length %1024 == 0 ? buffer.length /1024 : buffer.length/1024+1; for(int i= 0 ; i < forNum ; i++){ if(i == buffer.length/1024){ byte[] bu = new byte[buffer.length-i*1024] ; System.arraycopy(buffer, i*1024, bu, 0, bu.length); String str = Arrays.toString(bu).replaceAll(", ", " "); writer.write(str.substring(1,str.length()-1)); }else{ byte[] bu = new byte[1024] ; System.arraycopy(buffer, i*1024, bu, 0, bu.length); String str = Arrays.toString(bu).replaceAll(", ", " "); writer.write(str.substring(1,str.length()-1)); // writer.write(byte2string(bu)); } writer.write("\r\n"); writer.flush(); } }else{ byte[] buffer = new byte[num] ; System.arraycopy(buffers, num*j, buffer, 0, num); int forNum = buffer.length %1024 == 0 ? buffer.length /1024 : buffer.length/1024+1; for(int i= 0 ; i < forNum ; i++){ if(i == buffer.length/1024){ byte[] bu = new byte[buffer.length-i*1024] ; System.arraycopy(buffer, i*1024, bu, 0, bu.length); String str = Arrays.toString(bu).replaceAll(", ", " "); writer.write(str.substring(1,str.length()-1)); }else{ byte[] bu = new byte[1024] ; System.arraycopy(buffer, i*1024, bu, 0, bu.length); String str = Arrays.toString(bu).replaceAll(", ", " "); writer.write(str.substring(1,str.length()-1)); // writer.write(byte2string(bu)); } writer.write("\r\n"); writer.flush(); } } writer.flush(); } writer.flush(); } catch(Exception e){} test.byte2File(buffers); } private static String byte2string(byte[] buffer){ StringBuilder sb = new StringBuilder(); for(byte b : buffer){ sb.append(" "+b); } return sb.toString(); } private void byte2File(byte[] buffer){ FileOutputStream os = null ; try{ os = new FileOutputStream(new File("res/copyFile.rar")); os.write(buffer); os.flush(); } catch(Exception e){} finally{ if(os != null ){ try { os.close() ; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } private byte[] file2byte(File file){ FileInputStream os = null ; ByteBuffer buffer = ByteBuffer.allocate(10485760*20) ; try{ os = new FileInputStream(file); byte[] byBuffer = new byte[1024] ; int length = 0 ; while((length = os.read(byBuffer)) != -1){ byte[] putb = new byte[length]; System.arraycopy(byBuffer, 0, putb, 0, length); buffer.put(putb); } buffer.flip(); byte[] result = new byte[buffer.limit()] ; System.arraycopy(buffer.array(), 0, result, 0, result.length); return result; } catch(Exception e){ return new byte[1] ; } finally{ if(os!=null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
将二进制的文件还原为原文件
package ceshiOA; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; public class Binarry2File { public static void main(String[] args) { new Binarry2File().getFiles("res/ProjectFiles/", "res/project.rar"); } public void getFiles(String dirs , String saveFile){ File[] files = new File(dirs).listFiles(); BufferedReader reader = null ; FileOutputStream os = null ; for(File file : files){ try { os = new FileOutputStream(saveFile,true); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } System.out.println(file.getName()); try{ reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8")); String str = null ; while((str = reader.readLine()) != null){ String[] strs = str.split(" "); byte[] buffer =new byte[strs.length]; for(int i = 0 ; i < strs.length ; i ++){ buffer[i] = Byte.parseByte(strs[i]); } System.out.println(str); os.write(buffer); } os.flush(); } catch(Exception e ){ e.printStackTrace(); } finally{ if(reader!=null){ try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } if(os != null ){ try { os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } }
上一篇: css3动画之@keyframes
下一篇: css3 动画停留在最后一帧
推荐阅读
-
iBatis 3最佳实践 博客分类: java iBATISjava
-
iBatis 3最佳实践 博客分类: java iBATISjava
-
ACM--敲7 博客分类: java scannerindexOf
-
文件传输 博客分类: java基础 文件传输
-
java值传递还是引用传递 博客分类: java
-
Linux和Windows之间文件传输(Linux下rz,sz与ssh的配合使用) 博客分类: Linux linuxwindowsrzsz文件传输
-
java类中读取xxx.properties文件的内容 博客分类: java
-
wps笔试题目,实现java中indexof函数 博客分类: 笔试 indexof正则笔试wps
-
java读写word文档,完美解决方案 博客分类: java操作word java操作word
-
java的初始化流程 博客分类: java java初始化