0

나는 파일 판독기와 스캐너를 읽고 구문 분석하도록 각 줄에 13 개의 정수로 쉼표로 구분 된 39000 개의 int를 포함하는 파일을 가지고있다. 그러나 그것은 일상적으로 1 시간 이상 걸립니다. 내가 아마 bufferedreader를 사용해야하지만 어떻게 구현 해야할지 모르겠다 생각합니다. 아무도 더 나은 (빠른) 방법을 제안 할 수 있습니까? 여기 스캐너 클래스를 사용하여 파일을 구문 분석하는 데 너무 오래 걸린다.

내 코드입니다 :

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; 

} 

}

답변

1

지연이 스캐너에서 오는 것이지 최종 setSignal lo가 아닌지 확인 하시겠습니까 너는 넣었 니?

TraceView을 사용하여 프로그램이 전화를 끊는 위치를 정확하게 파악하는 것이 좋습니다. 비슷한 문제로 크게 도움이되었습니다.

도움이 될지 알려주세요. 첫 번째 루프의 시작과 끝 부분에 시작 지점과 중단 지점을 두는 것이 좋습니다. 이처럼 :

공용 클래스 ECGFilereader {//은 SD 카드에서 심전도 파일을 읽어

공공 최종 정적 INT numChannels = 12; // 데이터는 12 개의 채널에 저장되며 각 리드 당 하나씩 public final static int numSamples = 3000; // 500 = fs so * 6 초 동안 6 초 데이터 개인 파일 파일; 전용 스캐너 스캐너; short [] [] ecg = 새 짧은 [numChannels] [numSamples]; // 각 채널 리드에 대한 모든 샘플이

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; 
} 
+0

도움을 주셔서 감사합니다. 나중에 참조 할 수 있도록 TraveView를 살펴 보겠습니다. 이를 위해 스캐너 클래스를 완전히 대체하고 결국 친구의 도움을 받아 작업을 완료했습니다. 나는 대답을 임박하게 게시 할 것입니다. 또한 SD 카드 확인에 대한 제안을 주셔서 감사합니다. – Jacko85

+0

문제가 없습니다! 코드가 작동했기 때문에 다행입니다. BufferedReader는 일반적으로 어쨌든 대부분의 경우 사용하는 것이 좋습니다. 문제가 해결되면 답변을 올바르게 표시하는 것을 잊지 마십시오! 에 오신 것을 환영합니다 그리고 좋은 코딩 :) – Codeman

관련 문제