나는 파일 판독기와 스캐너를 읽고 구문 분석하도록 각 줄에 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;
}
}
도움을 주셔서 감사합니다. 나중에 참조 할 수 있도록 TraveView를 살펴 보겠습니다. 이를 위해 스캐너 클래스를 완전히 대체하고 결국 친구의 도움을 받아 작업을 완료했습니다. 나는 대답을 임박하게 게시 할 것입니다. 또한 SD 카드 확인에 대한 제안을 주셔서 감사합니다. – Jacko85
문제가 없습니다! 코드가 작동했기 때문에 다행입니다. BufferedReader는 일반적으로 어쨌든 대부분의 경우 사용하는 것이 좋습니다. 문제가 해결되면 답변을 올바르게 표시하는 것을 잊지 마십시오! 에 오신 것을 환영합니다 그리고 좋은 코딩 :) – Codeman