无法将文件中的字符串读取与java字符串进行比较

时间:2014-10-31 11:07:56

标签: java string utf-8 file-handling

我正在java中读取一个扩展名为.lab的文件,该文件基本上是一个带有utf字符的文本文件,内容如下:

0.100904     125    SIL
0.392625     125    तुझ्_beg 
0.622405     125    या_end 
0.623404     125    SIL
0.946096     125     ले_beg
1.120000     125    मळ्_mid 
1.362698     125    या_end 
1.363697     125    SIL

但在程序中,我比较如下:

arr[2].equals("SIL")

它没有用。 整个java代码如下:

public class SyllableCount 
{

static final File labDir = new File("/media/sda6/tts/programs/MyWork/silence_handling/labs_4");
static final HashMap<String, ArrayList<Float>> terminalSyllMap = new HashMap<String, ArrayList<Float>> ();

public void accessFilesForFolder(final File labDir) 
{
    System.out.println("in method");
    for (final File labFile : labDir.listFiles()) 
    {
        if (labFile.isDirectory()) 
        {
            accessFilesForFolder(labFile); //for recursive operation
        } else 
        {

            System.out.println(labFile.getName());
            BufferedReader br = null;
            String[] syllable = new String[100];//just an example-you have to initialize it big enough to hold all lines
            float[] timeFrame = new float [100];

            String sCurrentLine;
            try 
            {
                //br = new BufferedReader(new FileReader(labFile));
                br = new BufferedReader(new InputStreamReader(new FileInputStream(labFile), "UTF8"));


                int lineNo=0;
                while ((sCurrentLine = br.readLine()) != null) 
                {
                    String[] arr = sCurrentLine.split(" ");
                    //for the first line it'll print

                    if(arr[0].equalsIgnoreCase("#"))
                    {
                        lineNo++;
                        continue;
                    }


                        //entering them into separate arrays
                        timeFrame[lineNo] = Float.parseFloat(arr[0]);
                        syllable[lineNo] = arr[2];

                        lineNo++;

                }

                br.close(); 
                populateMaps(timeFrame, syllable, lineNo);

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

        }   
      }
    System.out.println(terminalSyllMap);
}

public void populateMaps(float[] timeFrame,String[] syllable, int lineNo) throws Exception
{



    String syllval;
    float duration;
    ArrayList<Float> timeframeArray;
    for(int i=0; i<lineNo-1; i++)
    {

        //System.out.println(syllable[i+1]);
        if (syllable[i+1].equals("SIL"))
        {
            syllval = syllable[i];
            duration = timeFrame[i+1] - timeFrame[i]; 
            if(terminalSyllMap.containsKey(syllval))
            {
                timeframeArray = terminalSyllMap.get(syllval);
            }
            else
            {
                timeframeArray = new ArrayList<Float>();
            }
            timeframeArray.add(duration);
            terminalSyllMap.put(syllval, timeframeArray);
        }
    }
}

public static void main(String[] args) 
{
    //
    SyllableCount run = new SyllableCount();
    run.accessFilesForFolder(labDir);

}





 }

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

尝试:

final String[] arr = sCurrentLine.split("\\s+");