Member Avatar

So in class we are making a program and we have a big problem with what is pretty much the last method we need. We want a download function, which basically takes a file already specified and copies it to the destination (and name) chosen by the user.

WE have a file chooser which allows the user to do that but unfortunately we are having a big problem with the method that would actually copy the mp3 sound. I have looked this up and i know that there is the filecopy method for this, and i tried it and seemed to work fine.

The sound file copied to where it was supposed to copy and everything, it played, but the sound it played was completely messed up.

SO i'm wondering if anyone else has had this problem and if they maybe know how to solve this, if u guys have any idead please help, or maybe there is another way around this?

Thx

Re: Copy MP3 files in java problem need help 80 80

Are you copying from local file or downloading from net?
Your code for that part?

Re: Copy MP3 files in java problem need help 80 80
Member Avatar
protected void onDownload()
	{
		FileDialog fileDialog = new FileDialog(this, "Select Download destination", FileDialog.SAVE);
        fileDialog.show();
        if (fileDialog.getFile() != null)
        {
            String directory = fileDialog.getDirectory();
            if (!directory.endsWith(File.separator)) 
            {
                directory += File.separator;
            }
            String file = fileDialog.getFile();
            File input = new File(EditBooks[thenum].getAbook());
            File output = new File(directory + file);
            try
            {
            	copy(input, output);
            }
            catch(IOException e)
            {
            	
            }
        }
	}

private static void copy(File inputFile, File outputFile) throws IOException
	{
		if (checkForExist)
		{
			if (outputFile.exists())
			{
				boolean proceed = false;
				do
				{
					System.out.println("Warning!!! Output file exist. Replace it? Yes(y) / No(n) / Yes to all (a)");
					BufferedReader consoleInput = new BufferedReader(new InputStreamReader(System.in));
					String ans = consoleInput.readLine().toLowerCase();
					if (ans.startsWith("n"))
					{
						System.out.println(inputFile.getName() + " Aborted");
						return;
					}
					if (ans.startsWith("a"))
					{
					checkForExist = false;
					proceed = true;
					}
					if (ans.startsWith("y"))
					{
						proceed = true;
					}
				}
				while(!proceed);
			}
		}
	
	System.out.println(inputFile.getName() + " Copying....");
	FileReader in = new FileReader(inputFile);
	FileWriter out = new FileWriter(outputFile);
	int c;

	while ((c = in.read()) != -1)
	out.write(c);

	in.close();
	out.close();

	System.out.println(inputFile.getName() + " Copied");

	}

The onDownload method is what runs when the download button is pressed, and EditBooks[thenum].getAbook() calls up the path and name of the file that is to be copied.

A lot of error checking going on but it copies the file at line 63.

Re: Copy MP3 files in java problem need help 80 80

Cool, I will assume you are copying files from local direction so no TCP/HTTP. Then here are some links that does the same thing:
http://www.rgagnon.com/javadetails/java-0064.html
http://www.java2s.com/Code/Java/File-Input-Output/CopyfilesusingJavaIOAPI.htm
http://www.roseindia.net/java/beginners/CopyFile.shtml

Haven't done that but If I was to do the same, I will use copyTo Method. See tutorial here how to use it.

Re: Copy MP3 files in java problem need help 80 80
Member Avatar

THANK YOU soo much dude. That first link was the method we needed :)
I just tested it and works perfectly. I hope it does if it doesn't i hope u'll be around :)


Thank you so much again, thats all the methods in our program working :D

Re: Copy MP3 files in java problem need help 80 80

Glad I helped :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.