本帖最后由 ye646682485 于 2015-4-26 19:36 编辑
- /*
- 分割和合并
- */
- import java.util.*;
- import java.io.*;
- class FileSplitAndSequence
- {
- public static void main(String[] args)
- {
- File f = new File("1.JPG");
- //System.out.println(f.mkdir());//返回的是false
- try
- {
- split(f);
- }
- catch (Exception e)
- {
- System.out.println(e.toString());
- }
-
- }
- public static void split(File f)//将文件分割
- {
- String name =f.getName();
- byte[] buff = new byte[1024*1024];
- int len=0;
- int count=1;
- FileOutputStream fos =null;
- FileInputStream fis =null;
- try
- {
- fis= new FileInputStream(f);
- while ((len=fis.read(buff))!=-1)
- {
-
- fos = new FileOutputStream(name+"_"+(count++)+".part");//在相对路径中分割成功了,创建了文件
- //fos = new FileOutputStream(f.mkdir()+"\\"+name+"_"+(count++)+".part");//这里有错误
- //我想在这里创建一个文件夹,用于存放分割后的文件,可是这里f.mkdir()返回的是false
- //应该怎么创建呢?
- fos.write(buff,0,len);
- }
- }
- catch (IOException io)
- {
- throw new RuntimeException("aaaaa");
- }
- finally
- {
- try
- {
- if(fis!=null)
- fis.close();
- }
- catch (IOException io)
- {
- throw new RuntimeException("bbbbb");
- }
- }
-
- }
- }
复制代码 |
|