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.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java Tutorial
Java Book
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » File Input Output » File SplitterScreenshots 
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
w_w_w._j_a_v___a___2___s__.___c__o___m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.