Results 1 to 13 of 13

Thread: Merge file

  1. #1
    skidrow's Avatar
    skidrow is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default Merge file

    I write a small application to merge file, current it merge text file, audio file but not work with video file. I don't know why. this's code:

    PHP Code:
    public static void main(String[] args) throws IOException {
    		// TODO Auto-generated method stub
    		JFileChooser F = new JFileChooser();
    		String[] Path = new String[2];
    		for(int i=0;i<=1;i++){
    			int t = F.showOpenDialog(null);
    			if(t==F.APPROVE_OPTION){
    				File F1 = F.getSelectedFile();
    				Path[i] = F1.getPath();
    			}
    		}
    		if(F.showSaveDialog(null)==JFileChooser.APPROVE_OPTION){
    			FileOutputStream out = new FileOutputStream(F.getSelectedFile());
    			for(int i=0; i<=1;i++){
    				FileInputStream F1 = new FileInputStream(Path[i]);
    				int n = F1.available();
    				while(n>0){
    					byte[] B = new byte[n];
    					int k = F1.read(B);
    					if(k==-1) break;
    					out.write(B);
    				}
    				F1.close();
    			}
    			out.close();
    		}
    		JOptionPane.showMessageDialog(null, "Merge Successful...!");
    	}

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    What do you mean by merge? Do you mean: Copy the contents of one file after the contents of another file so that the new file has the contents of one file in front followed by the data from the second file.

    If their is a program that reads a specific type of file and processes it, like a video that is displayed, why do you think that program will understand finding more data following the contents of what it thinks is a single file?

  3. #3
    skidrow's Avatar
    skidrow is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    I think copy the contents of one file after the contents of another file so that the new file. I don't understand you. Can you explain to me?

  4. #4
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    It's possible that a video file will have a header at the start that gives information about the video, such as the length, or number of frames, etc. Appending another file on the end wouldn't change this header information, so the merged section might not be recognised (also, the merged file might have its own header information).

    I'm not a video guru, so I don't know, but it strikes me as likely. I suggest you find out how commercial video editing applications merge videos.

  5. #5
    skidrow's Avatar
    skidrow is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    Thanhks to dlorde, because i want to try code myself.

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,796
    Rep Power
    19

    Default

    Java Code:
    int n = F1.available();
    Quoting from the API:
    Returns an estimate of the number of remaining bytes that can be read...
    Bolding mine.

    It would be more reliable for you to read in a loop storing into a byte buffer of some size (8k is the norm).

    That said, as already mentioned there may well be header information in a video file. To be honest, I'm surprised it worked for some audio files (unless they're wav).

  7. #7
    skidrow's Avatar
    skidrow is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Java Code:
    int n = F1.available();
    Quoting from the API:

    Bolding mine.

    It would be more reliable for you to read in a loop storing into a byte buffer of some size (8k is the norm).

    That said, as already mentioned there may well be header information in a video file. To be honest, I'm surprised it worked for some audio files (unless they're wav).
    I think it work with small file.
    What can i do?

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,796
    Rep Power
    19

    Default

    Probably, but then it can probably read in the whole of the small file quite easily.
    The usual way is something along the lines of:
    Java Code:
    byte[] buffer = new byte[about 8k];
    while ((n = in.read(buffer)) > -1) {
        out.write(buffer, n);
    }
    That reads from in, and writes to out.
    You'll have to check the API for exact details as I've done this off the top of my head, which invariably means I've got something wrong.

  9. #9
    skidrow's Avatar
    skidrow is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    I try, thanks to Tolls

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,796
    Rep Power
    19

    Default

    Note, this isn't going to help you at all if the file format involves headers...which is likely.

  11. #11
    skidrow's Avatar
    skidrow is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    I know, if i do with file format involves headers as video file,..what can i do?

  12. #12
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,796
    Rep Power
    19

    Default

    Leanr the file formats and see if you can do stuff involving merging the file data and relevant parts of the header.
    But that involves knowing the stuff you're working with.

  13. #13
    skidrow's Avatar
    skidrow is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

Similar Threads

  1. Merge Two Xml files ????
    By alwz_nikhil in forum XML
    Replies: 5
    Last Post: 01-18-2011, 09:18 AM
  2. Merge XML using XSL
    By palanikumark in forum XML
    Replies: 1
    Last Post: 08-16-2008, 12:14 AM
  3. Replies: 1
    Last Post: 03-27-2008, 05:06 PM
  4. Merge 2 button become one
    By banie in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2008, 05:26 PM
  5. Merge Sort Help
    By Hollywood in forum New To Java
    Replies: 5
    Last Post: 01-30-2008, 03:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •