gpt4 book ai didi

java - (处理/Java)将单个文件函数转换为采用数组的函数

转载 作者:行者123 更新时间:2023-12-02 10:56:13 25 4
gpt4 key购买 nike

我正在尝试创建一个程序,上传多个文件并将其名称和 BPM 标记存储到 ArrayList 中,以便在文件之间进行比较。我找到了两个可以帮助我的函数,但我无法将它们组合起来以获得我需要的函数。

第一个函数采用单个 mp3 文件并将其数据输出到控制台(使用 mp3agic 库):

File file = new File(dataPath("") + "/Song.mp3");

Mp3File mp3file = new Mp3File(file.getPath());
if (mp3file.hasId3v2Tag()) {
ID3v2 id3v2Tag = mp3file.getId3v2Tag();
println("Track: " + id3v2Tag.getTrack());
println("Artist: " + id3v2Tag.getArtist());
println("BPM: " + id3v2Tag.getBPM());
println("Album artist: " + id3v2Tag.getAlbumArtist());
}

第二个函数采用数据路径并输出包含文件夹中文件的名称和信息的目录

void setup() {

String path = "Desktop/mp3folder";

println("Listing all filenames in a directory: ");
String[] filenames = listFileNames(path);
printArray(filenames);

println("\nListing info about all files in a directory: ");
File[] files = listFiles(path);
for (int i = 0; i < files.length; i++) {
File f = files[i];
println("Name: " + f.getName());
println("Is directory: " + f.isDirectory())
println("-----------------------");
}
}

// This function returns all the files in a directory as an array of Strings
String[] listFileNames(String dir) {
File file = new File(dir);
if (file.isDirectory()) {
String names[] = file.list();
return names;
} else {
// If it's not a directory
return null;
}
}

// This function returns all the files in a directory as an array of File objects
// This is useful if you want more info about the file
File[] listFiles(String dir) {
File file = new File(dir);
if (file.isDirectory()) {
File[] files = file.listFiles();
return files;
} else {
// If it's not a directory
return null;
}
}

我尝试创建的函数将两者结合起来。我需要第一个函数中的艺术家、轨道和 BPM 来处理目录中的文件数组列表。

任何指导将不胜感激。任何有关其他方法的建议也将不胜感激。

最佳答案

解决此问题的一种方法是使用 classes封装您想要跟踪的数据。

例如,下面是一个简化的类,其中包含有关艺术家、轨道和 bpm 的信息:

public class TrackInfo{
private String artist;
private String track;
int bpm;
}

我也退后一步,break your problem down into smaller steps ,然后一次一个地取出这些碎片。您可以创建一个接受 File 参数并打印出该 File 的 MP3 数据的函数吗?

void printMp3Info(File file){
// print out data about file
}

在继续之前让其完美运行。在尝试将其与多个 File 实例的 ArrayList 一起使用之前,请尝试使用硬编码的 File 实例调用它。

如果你遇到困难,你可以发布 MCVE以及一个具体的技术问题。祝你好运。

关于java - (处理/Java)将单个文件函数转换为采用数组的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51715890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com