Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Split wav file Java

Posted on 2009-03-31
3
Medium Priority
?
1,058 Views
Last Modified: 2012-05-06
I want to split a wave format file into multiple small file. For e.g. If we have a 1 big wave file then i want to break that file in small 10 wave files. Is it possible in java and If YES then how?
Thanks in Advance
0
Comment
Question by:parveendalal
3 Comments
 
LVL 12

Accepted Solution

by:
basav_com earned 2000 total points
ID: 24031981
0
 
LVL 9

Expert Comment

by:brunoguimaraes
ID: 24032302
See code below, taken from http://www.coderanch.com/t/436301/Java-General-intermediate/java/split-file
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.swing.JOptionPane;
 
 
public class SplitFile {
    int FRG_FSIZE=0;
    
    public File[] splitFile(File source,int noFile,int lsize)
    {
        FRG_FSIZE = 1024*5;
        File[] fileFragments = new File[noFile];
        String[] frgfName = new String[noFile];
            try{    
                String sourceFName = source.getName();
                long sourceFSize = source.length();
                FileInputStream fis = new FileInputStream(source);
 
                String Fileinfo = new String(sourceFName + "," + String.valueOf(sourceFSize));
                System.out.println(noFile);
                if (lsize != 0) {
                    noFile--;
                }
                System.out.println(noFile);
                sourceFName = sourceFName.substring(0, sourceFName.lastIndexOf("."));
                int j=0;
                for (int i = 1; i <= noFile; i++) {
                    frgfName[i-1] ="temp\\"+sourceFName + String.valueOf(i)+".splt";
                    fileFragments[i-1] = new File(frgfName[i-1]);
                   
                    FileOutputStream fos = new FileOutputStream(fileFragments[i - 1]);
                    byte[] data = new byte[FRG_FSIZE];
                    int count = fis.read(data);
                    fos.write(data);
                    fos.close();
                    String frgFileInfo = new String(frgfName[i-1] + "," + String.valueOf(FRG_FSIZE));
                }
                if (lsize != 0) {                    
                    System.out.println(noFile);
                    frgfName[noFile] ="temp\\"+sourceFName + String.valueOf(noFile+1)+".splt";
                    fileFragments[noFile] = new File(frgfName[noFile]);
                    FileOutputStream fos = new FileOutputStream(fileFragments[noFile]);
                    byte[] data = new byte[lsize];
                    int count = fis.read(data);
                    fos.write(data);
                    fos.close();
                    String frgFileInfo = new String(frgfName[noFile] + "," + String.valueOf(lsize));
                }
 
               } catch (Exception e) {
                      
                   System.out.println("Error in Splitting"+e);
                    JOptionPane.showMessageDialog(null, "Error in Splitting File \n"+e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                     return null;
               }
        return fileFragments;
  }
  
  public static void main(String ar[])
  {
  	SplitFile sf=new SplitFile();
	sf.splitFile(new File("Placement.doc"),5,1024);
  }
 
 
}

Open in new window

0
 
LVL 17

Expert Comment

by:Thomas4019
ID: 24033686
brunoguimaraes, your answer may split the file but it produces a number of completely useless, unplayable files. A much better solution would to be use part of the Java Media Framework (JMF) as basav_com has suggested.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question