import java.io.*; import java.util.Arrays; public class SplitAndCountTest01 { static int a[] = new int[3]; public static void main(String[] args) { File file1=new File("e:/abc/back.jpg"); splitbyte(file1); split(file1); countread(); } public static void splitbyte(File file1){ int filelength=(int)file1.length(); for(int i=0;i<3;i++){ if(filelength/3==0){ a[i]=(int)filelength/3; } else{ if(i<=1){ a[i]=(int)filelength/3; }else{ a[i]=(int)(filelength/3+filelength%3); } } } } public static void writefile(){ for(int i=0;i<3;i++){ } } public static void split(File file1){ try(var bis = new BufferedInputStream(new FileInputStream(file1))){ int temp=0; //byte[] bytes=new byte[(int)file1.length()]; //while (temp=bis.read(bytes)) System.out.println(a[1]); for(int i=0;i<3;i++){ byte[] bytes=new byte[a[i]]; File file2=new File("e:/abc/a/"+"back"+i+".jpg"); var bos=new BufferedOutputStream(new FileOutputStream(file2)); bis.read(bytes); bos.write(bytes,0,bytes.length); bos.flush(); System.out.println(a); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //合并 public static void countread(){ for(int i=0;i<3;i++){ File file3=new File("e:/abc/a/"+"back"+i+".jpg"); try(var bisa= new BufferedInputStream(new FileInputStream(file3))) { byte[] bytes2=bisa.readAllBytes(); System.out.println(a[i]); System.out.println(new String(bisa.readAllBytes())); File file4=new File("e:/abc/a/"+"back"+5+".jpg"); var bosa=new BufferedOutputStream(new FileOutputStream(file4,true)); bosa.write(bytes2,0,a[i]); bosa.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } }
采用字节流操作实现将文件分成若干小份再重新合并
最新推荐文章于 2022-07-26 22:26:13 发布