3

我想读取带有分隔符“!”的 csv 文件。但是,当我在我的 android 应用程序中运行以下代码时,整个应用程序就会挂起。请帮助我解决问题。

在记事本中打开时,输入文件如下所示:

/////////////////////////////////////////////////////////


Line Number"!Shift!"QC Operator"!"Line Operator"!"Printing Operator"!"Product 
Group"!"Product Series"!Product!" Type of Defect"!"PCBType"!CORRECTIVE ACTION (CA)
1!DAY!Deepak!Chetan!Arun!TIMER!MM30XX!55Q!NO Solder!POWER!REWORK
2!NIGHT!Kajal!Kajal!Ashish!TEMP!MM10XX!55Q!Less solder!LOGIC!Replacement
3!!Kavita!Mahesh G!B.C!COUNTER!TC!EM306A!Dry solder!DISPLAY!NA
4!!Madhuri!Mahesh.P!Pabitra!PLC!PID !EM368 !IC PIN SOLDER SHORT!CARD!
5!!Namita!Namita!Sachin!METER!XT!EM368-C!Tombs toning!!
!!Sneha!Naveen!Sayali!DPM!800!MF16 !One side open !!

///////////////////////////////////////////////////////

读取文件的代码:

    public void ReadConfigFile() throws IOException 
{      
    File myFile = new File(Environment.getExternalStorageDirectory() + File.separator + "QCVerification"+ File.separator+"List of Entries.csv");
    FileInputStream fIn = new FileInputStream(myFile);
    BufferedReader myReader = new BufferedReader(
            new InputStreamReader(fIn));
    String aDataRow = "";
    ArrayList<String> FileWholeData = new ArrayList<String>();
    FileWholeData.removeAll(null);
    while ((aDataRow = myReader.readLine()) != null) 
    {
        FileWholeData.add(aDataRow);
    }
    MyQcDataStructure = new QCDataStructure();
    for(Integer __index = 0;__index <FileWholeData.size();__index++)
    {   
        String TempString = "";
        TempString = FileWholeData.get(__index);
        StringTokenizer tokenizer = new StringTokenizer(TempString,"!");   
        int columnIndex = 0;   
        while (tokenizer.hasMoreTokens()) 
        {                  
            switch (columnIndex) 
            {     
            case 0:   
                MyQcDataStructure.QCLineNo.add(tokenizer.nextToken());
                break;
            case 1:
                    MyQcDataStructure.QCShift.add(tokenizer.nextToken());
                    break;
            case 2:
                    MyQcDataStructure.QCQCOPer.add(tokenizer.nextToken());
                    break;
            case 3:
                    MyQcDataStructure.QCLineOper.add(tokenizer.nextToken());
                    break;
            case 4:
                    MyQcDataStructure.QCPrintingOper.add(tokenizer.nextToken());
                    break;
            case 5:
                    MyQcDataStructure.QCProductGorup.add(tokenizer.nextToken());
                    break;
            case 6:
                    MyQcDataStructure.QCProductSeries.add(tokenizer.nextToken());
                    break;
            case 7:
                    MyQcDataStructure.QCProduct.add(tokenizer.nextToken());
                    break;
            case 8:
                    MyQcDataStructure.QCPDefect.add(tokenizer.nextToken());
                    break;
            case 9:
                    MyQcDataStructure.QCPCBType.add(tokenizer.nextToken());
                    break;
            case 10:
                    MyQcDataStructure.QCCA.add(tokenizer.nextToken());
                    break;
            }                      
            columnIndex++; 
        }
        break;
    }
    String aBuffer;
    aBuffer = "";
    for(int __secondIndex = 0;__secondIndex < MyQcDataStructure.QCLineNo.size();__secondIndex++)
    {
        aBuffer = aBuffer + MyQcDataStructure.QCLineNo.get(__secondIndex);
        aBuffer = aBuffer + "$";
        aBuffer = aBuffer + MyQcDataStructure.QCShift.get(__secondIndex);
        aBuffer = aBuffer + "$";
        aBuffer = aBuffer + MyQcDataStructure.QCQCOPer.get(__secondIndex);
        aBuffer = aBuffer + "$";
        aBuffer = aBuffer + MyQcDataStructure.QCLineOper.get(__secondIndex);
        aBuffer = aBuffer + "$";
        aBuffer = aBuffer + MyQcDataStructure.QCPrintingOper.get(__secondIndex);
        aBuffer = aBuffer + "$";
        aBuffer = aBuffer + MyQcDataStructure.QCProductGorup.get(__secondIndex);
        aBuffer = aBuffer + "$";
        aBuffer = aBuffer + MyQcDataStructure.QCProductSeries.get(__secondIndex);
        aBuffer = aBuffer + "$";
        aBuffer = aBuffer + MyQcDataStructure.QCProduct.get(__secondIndex);
        aBuffer = aBuffer + "$";
        aBuffer = aBuffer + MyQcDataStructure.QCPDefect.get(__secondIndex);
        aBuffer = aBuffer + "$";
        aBuffer = aBuffer + MyQcDataStructure.QCPCBType.get(__secondIndex);
        aBuffer = aBuffer + "$";
        aBuffer = aBuffer + MyQcDataStructure.QCCA.get(__secondIndex);
        aBuffer = aBuffer + "$";

    }
    txtData.setText(aBuffer);
    myReader.close();
    Toast.makeText(getBaseContext(),
            "Done reading santosh SD 'mysdfile.txt'",
            Toast.LENGTH_SHORT).show();
}   

但是当以下代码在我的应用程序中运行时,应用程序会挂起:

while (tokenizer.hasMoreTokens()) 
        {                  
            switch (columnIndex) 
            {  

任何解决此问题的帮助将不胜感激。

4

1 回答 1

0

您的 switch 块中没有默认情况。所以当列达到 11 时,你就有了一个无限循环。

添加

default : 
    tokenizer.nextToken();
    break;

到你的 switch 语句,或者做

while (tokenizer.hasMoreTokens() && columnIndex <= 10)
于 2012-12-07T12:25:30.953 回答