Java保存mp3文件

时间:2016-09-29 11:35:42

标签: java

我正在尝试使用文件选择器从一个目录创建导入图像和mp3文件,并将它们保存到另一个目录。图像很好但我似乎无法找到如何保存mp3文件。

图片

@Override
 public void saveFile(File file) {
  //Get image path
  String imagePath = file.getAbsolutePath();
  String imageName = file.getName();
  System.out.println(imagePath);

  //Read image
  try {
   bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
   bufferedImage = ImageIO.read(new File(imagePath));
   System.out.println("Reading complete.");
  } catch (IOException e) {
   System.out.println("Error: " + e);
  }

  //write image
  try {
   f = new File("H:\\TestFolder\\images\\" + imageName); //output file path
   ImageIO.write(bufferedImage, "jpg", f);
   System.out.println("Writing complete.");
  } catch (IOException e) {
   System.out.println("Error: " + e);
  }
 }

MP3

@Override
public void saveFile(File file) {

    try{
        f = new File(file, "H:\\TestFolder\\test.mp3"); //file.getAbsolutePath();

    }catch (Exception e) {
        e.printStackTrace();
    }

}

2 个答案:

答案 0 :(得分:0)

使用Files.copy(source,target,REPLACE_EXISTING);

https://docs.oracle.com/javase/tutorial/essential/io/copy.html

答案 1 :(得分:-1)

试试这样:

File f = new File("H:\\TestFolder\\test.mp3");
InputStream is = new FileInputStream(f);

OutputStream outstream = new FileOutputStream(new File("H:\\TestFolder2\\blabla.mp3"));
byte[] buffer = new byte[4096];
int len;
while ((len = is.read(buffer)) > 0) {
    outstream.write(buffer, 0, len);
}
outstream.close();