Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Split wav file Java

Posted on 2009-03-31
3
1,025 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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
runtime exception 2 41
Java program running SQL query 5 37
Tagging and Merging on Branch 1 29
What browser will run Java? 7 55
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

856 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