2013-10-10 53 views
0

我是新来的Java和失去的那一刻计算值。的Java BufferedReader中的readline从文件

我有这样的代码:

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 

/** 
* 
* @author Darwish 
*/ 
public class M3UReader { 

    /** 
    * @param args the command line arguments 
    */ 

    public static boolean isValidHeader(String playList) 
    { 
     boolean returnValue = false; 
     BufferedReader br; 
     try 
     { 
      br = new BufferedReader(new FileReader(new File(playList))); 
      String s = br.readLine(); // declares the variable "s" 
      if(s.startsWith("#EXTM3U")) { // checks the line for this keyword 
       returnValue = true; // if its found, return true 
      } 
      br.close(); 
     } 
     catch (Exception e) 
     { 
      System.err.println("isValidHeader:: error with file "+ playList + ": " + e.getMessage()); 
     } 

     return returnValue; 
    } 
    public static int getNumberOfTracks(String playList) 
    { 
     int numberOfTracks = 0; // sets the default value to zero "0" 
     try 
     { 
      BufferedReader br = new BufferedReader(new FileReader(new File(playList))); 
      String s; 
      while((s = br.readLine())!=null) // if "s" first line is not null 
      { 
       if(s.startsWith("#")==false) { // if the first line starts with "#" equals to false. 
        numberOfTracks++; // increments 
       } 
      } 
      br.close(); 
     } 
     catch (Exception e) 
     { 
      numberOfTracks = -1; // chek if the file doesnt exist 
      System.err.println("could not open/read line from/close filename "+ playList); 
     } 
     return numberOfTracks; 

    } 

    public static int getTotalMinutes(String playList) 
    { 
     // code needed here 
    } 

    public static void main(String[] args) { 
     // TODO code application logic here 
     String filename = "files\\playlist.m3u"; // finds the file to read (filename <- variable declaration.) 
     boolean isHeaderValid = M3UReader.isValidHeader(filename); // declares the variabe isHeaderValid and links it with the class isValidHeader 
     System.out.println(filename + "header tested as "+ isHeaderValid); // outputs the results 

     if(isHeaderValid) 
     { 
      int numOfTracks = M3UReader.getNumberOfTracks(filename); 
      System.out.println(filename + " has "+ numOfTracks + " tracks "); 
     } 

    } 
} 

在方法getTotalMinutes,我必须找到一个方法来计算这是从文件中读取整型值的总和。该文件具有这样的数据:

#EXTM3U 
#EXTINF:537,Banco De Gaia - Drippy F:\SortedMusic\Electronic\Banco De Gaia\Big Men Cry\01 Drippy.mp3 
#EXTINF:757,Banco De Gaia - Celestine F:\SortedMusic\Electronic\Banco De Gaia\Big Men Cry\02 Celestine.mp3 
#EXTINF:565,Banco De Gaia - Drunk As A Monk F:\SortedMusic\Electronic\Banco De Gaia\Big Men Cry\03 Drunk As A Monk.mp3 
#EXTINF:369,Banco De Gaia - Big Men Cry F:\SortedMusic\Electronic\Banco De Gaia\Big Men Cry\04 Big Men Cry.mp3 

的#EXTINF后的数字:是音乐,从上述数据是以秒的长度。

我不知道对getTotalMinutes方法写什么代码,让程序从文件中读取分钟,然后计算他们都得到总分钟。我在网上搜索了如何做到这一点,不幸的是我找不到任何。所以,任何帮助表示赞赏。

+0

问题要求代码必须表现出对问题的理解最小正在解决。包括尝试解决方案,为什么他们没有工作,以及预期的结果。 –

+0

是什么*。*“的音乐的长​​度”是什么意思?分钟,秒钟,滴答滴答,滴答滴答滴答时间? – MadProgrammer

+0

音乐的长度以秒为单位。 –

回答

0

您可以使用此,它只是你的getNumberTracks方法的副本,但它是在解析文件,你需要获得总分钟的方式:

public static final String beginning = "#EXTINF:"; 
public static final String afterNumber = ","; 

public static int getTotalMinutes(String playList) { 
    int value = 0; 
    try { 
     BufferedReader br = new BufferedReader(new FileReader(new File(playList))); 
     String s; 
     while ((s = br.readLine()) != null) // if "s" first line is not null 
     { 
      if (s.contains(beginning)) { 
       String numberInString = s.substring(beginning.length(), s.indexOf(afterNumber)); 
       value += Integer.valueOf(numberInString); 
      } 
     } 
     br.close(); 
    } catch (Exception e) { 
    } 
    return value; 
} 
+0

哇,这解决了我的问题,谢谢!现在,我需要找到一个办法把一切都在getTotalMinutes方法:) 我已经想到了用s.contains的,但我不知道里面的()放什么。我试图使用%d不幸的是它没有工作。 –

0

因此,基于从here提供的说明,数字值是秒数。

因此,考虑到在#EXTINF:{d},{t}格式的String你应该能够使用简单的String操作来获取值了......

String text = "#EXTINF:537,Banco De Gaia - Drippy F:\\SortedMusic\\Electronic\\Banco De Gaia\\Big Men Cry\\01 Drippy.mp3"; 
String durationText = text.substring(text.indexOf(":") + 1, text.indexOf(",")); 
int durationSeconds = Integer.parseInt(durationText); 
System.out.println(durationSeconds); 

,它将打印537 ...

下一页我们只需要做一些简单的时间运算...

double seconds = durationSeconds; 
int hours = (int)(seconds/(60 * 60)); 
seconds = seconds % (60 * 60); 
int minutes = (int)(seconds/60); 
seconds = seconds % (60); 

System.out.println(hours + ":" + minutes + ":" + NumberFormat.getNumberInstance().format(seconds)); 

哪打印0:8:57(或8分57秒)

+0

感谢您的意见。我设法得到秒成具有user2854908的代码帮助分钟: INT numOfMinutes = M3UReader.getTotalMinutes(文件名); System.out.println(filename +“has”+ numOfTracks +“tracks,”+(numOfMinutes/60)+“minutes in total”); –

0

要阅读你要搜索有关M3U解析器信息M3U文件。目前已经有许多有效的开源解析器,但你需要密切关注他们的许可,如果你是在销售或分发本规划。

M3U解析器看起来有前途,如果你只是想要快速,高效。

M3u Parser

0
public static int getTotalMinutes(String filename) { 
    int totalSeconds = 0; 

    if (isValidHeader(filename)) { 
     try (BufferedReader br = new BufferedReader(new FileReader(new File(filename)));) { 
      String nextLine; 
      while ((nextLine = br.readLine()) != null) { 
       //If the next line is metadata it should be possible to extract the length of the song 
       if (nextLine.startsWith(M3U_METADATA)) { 
        int i1 = nextLine.indexOf(":"); 
        int i2 = nextLine.indexOf(","); 
        String substr = nextLine.substring(i1 + 1, i2); 
        totalSeconds += Integer.parseInt(substr); 
       } 
      } 
     } catch (IOException | NumberFormatException e) { 
      //Exception caught - set totalSeconds to 0 
      System.err.println("getTotalSeconds:: error with file " + filename + ": " + e.getMessage()); 
      totalSeconds = 0; 
     } 
    } 

    return totalSeconds; 
} 
相关问题