0

我有一个包含大约 39000 个整数的文件,用逗号分隔,每行有 13 个整数,所以我设置了一个文件阅读器和一个扫描仪来读取和解析它,但是运行它几乎需要一个多小时。我想我可能需要使用缓冲阅读器,但不知道如何实现它。任何人都可以提出更好(更快)的方法吗?

这是我的代码:

public class ECGFilereader { // reads the ecg files from the SD card 

public final static int numChannels = 12;   // the data is stored in 12 channels, one for each lead
public final static int numSamples = 3000; //500 = fs so *6 for 6 seconds of data
private File file;
private Scanner scanner;
short [] [] ecg = new short [numChannels] [numSamples]; //Creates a short called ecg in which all of the samples for each channel lead will be stored

 public ECGFilereader (String fname) throws FileNotFoundException 
 {
    file = new File(Environment.getExternalStorageDirectory() +"/1009856.txt");     //accesses the ecg file from the SD card

    scanner = new Scanner(file);

    scanner.useDelimiter(",|\\r\\n");   //sets commas and end's of lines as separators between each int
 }

public boolean ReadFile(Waveform[] waves) // sorts data into and array of an array (12 channels each containing 3000 samples)
{
    for (int sample=0; sample<numSamples && scanner.hasNextInt(); sample++)     //
    {
        scanner.nextInt();
        for (int chan = 0; chan<numChannels; chan++)
        {
            if(scanner.hasNextInt())
                ecg [chan] [sample] = (short) scanner.nextInt();        

            else if (scanner.hasNextLine())
            {   scanner.nextLine();
            }
            else return false;
        }   
    }
    for (int chan=0; chan<numChannels; chan++)
        waves[chan].setSignal(ecg[chan]); // sets a signal equal to the ecg array of samples for each channel
    return true;

}
}

编辑:

我现在已经完全删除了扫描仪类,它可以与以下代码完美配合:

public boolean ReadFile(Waveform[] waves) // sorts data into and array of an array (12 channels each containing 3000 samples)
{
try {
    BufferedReader in = new BufferedReader(new FileReader(file));

    String reader = "";
    for (int sample=0; sample<numSamples; sample++){
    if ((reader = in.readLine()) == null) {
        break;}
    else {
        String[] RowData = reader.split(","); // sets the commas as separators for each int.
        for (int chan=0; chan <12 && chan<RowData.length; chan++)
            ecg [chan][sample]= Integer.parseInt(RowData[chan+1]); //parses each int from the current row into each channel for the ecg[]
        }
    }
    in.close();
} catch (IOException e) {
    }
    for (int chan=0; chan<numChannels; chan++)
        waves[chan].setSignal(ecg[chan]); // sets a signal equal to the ECG array of samples for each channel
    return true;

}

}

4

1 回答 1

1

您确定您的延迟来自扫描仪而不是您放入的最终 setSignal 循环吗?

我建议使用TraceView来确定您的程序挂起的确切位置。它极大地帮助了我解决类似的问题。

让我知道这是否有帮助。我建议将起点和终点放在第一个循环的起点和终点。像这样:

public class ECGFilereader { // 从 SD 卡读取心电图文件

公共最终静态 int numChannels = 12; // 数据存储在 12 个通道中,每个通道一个 public final static int numSamples = 3000; //500 = fs so *6 for 6 seconds of data private File file; 私人扫描仪扫描仪;短 [] [] ecg = 新短 [numChannels] [numSamples]; //创建一个称为 ecg 的 short,其中将存储每个通道导联的所有样本

public boolean ReadFile(Waveform[] waves) // sorts data into and array of an array (12
channels each containing 3000 samples)
{   
Debug.startMethodTracing("scanner");
for (int sample=0; sample<numSamples && scanner.hasNextInt(); sample++)     //
{
    scanner.nextInt();
    for (int chan = 0; chan<numChannels; chan++)
    {
        if(scanner.hasNextInt())
            ecg [chan] [sample] = (short) scanner.nextInt();        

        else if (scanner.hasNextLine())
            scanner.nextLine();

        else return false;
    }   
}
Debug.stopMethodTracing();

Debug.startMethodTracing("setSignal");
for (int chan=0; chan<numChannels; chan++)
    waves[chan].setSignal(ecg[chan]); 
    // sets a signal equal to the ecg array of samples for each channel
Debug.stopMethodTracing();
return true;
}

这样,您将有 2 个 traceView 文件进行分析,以了解您的性能问题来自何处。

编辑:

您需要确保您正在做的另一件事是检查外部存储是否可用。使用以下代码执行此操作:

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {
    // We can read and write the media
    mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    // We can only read the media
    mExternalStorageAvailable = true;
    mExternalStorageWriteable = false;
} else {
    // Something else is wrong. It may be one of many other states, but all we need
    //  to know is we can neither read nor write
    mExternalStorageAvailable = mExternalStorageWriteable = false;
}
于 2011-08-24T14:24:16.990 回答