File Splitter : File Splitter « File Input Output « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » File Input Output » File Splitter 




File Splitter
     
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//package com.power.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.LinkedList;
import java.util.Scanner;

/**
 
 @author f6036477
 */
public class FileSplitter {

  private File f;

  public FileSplitter(File f) {
    if (f == null)
      throw new IllegalArgumentException("File must be not null!");
    this.f = f;
    System.out.println("File Length (KB): " + f.length() 1024.0);
  }

  public boolean split(long size) {
    if (size <= 0)
      return false;

    try {
      int parts = ((int) (f.length() / size));
      long flength = 0;
      if (f.length() % size > 0)
        parts++;

      File[] fparts = new File[parts];

      FileInputStream fis = new FileInputStream(f);
      FileOutputStream fos = null;

      for (int i = 0; i < fparts.length; i++) {
        fparts[inew File(f.getPath() ".part." + i);
        fos = new FileOutputStream(fparts[i]);

        int read = 0;
        long total = 0;
        byte[] buff = new byte[1024];
        int origbuff = buff.length;
        while (total < size) {
          read = fis.read(buff);
          if (read != -1) {
            buff = FileEncoder.invertBuffer(buff, 0, read);
            total += read;
            flength += read;
            fos.write(buff, 0, read);
          }
          if (i == fparts.length - && read < origbuff)
            break;
        }

        fos.flush();
        fos.close();
        fos = null;
      }

      fis.close();
      // f.delete();
      f = fparts[0];

      System.out.println("Length Readed (KB): " + flength / 1024.0);
      return true;
    catch (Exception ex) {
      System.out.println(ex);
      System.out.println(ex.getLocalizedMessage());
      System.out.println(ex.getStackTrace()[0].getLineNumber());
      ex.printStackTrace();
      return false;
    }
  }

  public boolean split(int parts) {
    if (parts <= 0)
      return false;

    return this.split(f.length() / parts);
  }

  public boolean unsplit() {
    try {
      LinkedList<File> list = new LinkedList<File>();
      boolean exists = true;
      File temp = null;
      File dest = new File(f.getPath().substring(0,
          f.getPath().lastIndexOf(".part")));
      FileInputStream fis = null;
      FileOutputStream fos = new FileOutputStream(dest);
      int part = 0;
      long flength = 0;
      String name = null;
      while (exists) {
        name = f.getPath();
        name = name.substring(0, name.lastIndexOf("."1+ part;
        temp = new File(name);

        exists = temp.exists();
        if (!exists)
          break;

        fis = new FileInputStream(temp);
        byte[] buff = new byte[1024];

        int read = 0;
        while ((read = fis.read(buff)) 0) {
          buff = FileEncoder.invertBuffer(buff, 0, read);
          fos.write(buff, 0, read);
          if (read > 0)
            flength += read;
        }
        fis.close();
        fis = null;
        temp.delete();
        part++;
      }

      fos.flush();
      fos.close();
      f = dest;
      System.out.println("Length Writed: " + flength / 1024.0);
      return true;
    catch (Exception ex) {
      ex.printStackTrace();
      return false;
    }
  }

  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.print("Split? [true/false]: ");
    boolean split = Boolean.parseBoolean(scan.next());

    if (split) {
      File file = new File("T:/Java/com/power/nb/visual/dist/visual.zip");
      FileSplitter splitter = new FileSplitter(file);
      System.out.println("splitter.split(3): " + splitter.split(3));
    else {
      File file = new File(
          "T:/Java/com/power/nb/visual/dist/visual.zip.part.0");
      FileSplitter splitter = new FileSplitter(file);
      System.out.println("splitter.unsplit(): " + splitter.unsplit());
    }
  }

}

class FileEncoder {

  private File f;

  public FileEncoder(File f) {
    if (f == null)
      throw new IllegalArgumentException("File must be not null!");
    this.f = f;
  }

  public boolean encode() {
    try {
      File temp = new File(f.getPath() ".tmp");
      FileInputStream fis = new FileInputStream(f);
      FileOutputStream fos = new FileOutputStream(temp);
      byte[] buff = new byte[1024];

      int read = 0;
      while ((read = fis.read(buff)) 0) {
        buff = FileEncoder.invertBuffer(buff, 0, read);
        fos.write(buff, 0, read);
      }

      fis.close();
      fos.flush();
      fos.close();

      f.delete();
      temp.renameTo(f);
      f = temp;
      return true;
    catch (Exception ex) {
      ex.printStackTrace();
      return false;
    }
  }

  public static byte[] invertBuffer(byte[] buff, int offset, int length) {
    if (buff == null || buff.length == 0)
      return null;
    if (offset < || length < 0)
      return null;

    byte[] inverted = new byte[length];
    int ind = length - 1;
    for (int i = offset; i < length; i++) {
      inverted[ind= buff[i];
      ind--;
    }
    return inverted;
  }

  public boolean decode() {
    return this.encode();
  }

  public static void main(String[] args) {
    File file = new File("T:/Java/com/power/nb/visual/dist/visual.zip");
    FileEncoder enc = new FileEncoder(file);
    System.out.println("enc.decode(): " + enc.decode());
  }

}

   
    
    
    
    
  














Related examples in the same category
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.