I cannot find the file I am trying to read with my filereader class
I have created a file reader class called ECGFilereader which should open a .txt but when I debug it throws the FileNotFoundException to my Waveform class which uses the txt.file
Here is my coding below, any help or suggestions would be much appreciated.
ECGFilereader.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ECGFilereader {
public final static int numChannels = 12;
public final static int numSamples = 500*6; //500 = fs so *6 for 6 seconds of data
private File file;
private Scanner scanner;
int [] [] ecg = new int [numChannels] [numSamples];
/*private String path;
public ECGFilereader(String file_path){
path = file_path;
} */
public ECGFilereader (String fname) throws FileNotFoundException
{
file = new File("res/raw/ecg.txt");
scanner = new Scanner(file);
}
public boolean ReadFile(Waveform[] waves)
{
for (int m=0; m<numSamples && scanner.hasNextInt(); m++)
{
int x = scanner.nextInt();
for (int chan = 0; chan<numChannels && scanner.hasNextInt(); chan++)
{
ecg [chan] [m] = scanner.nextInt();
}
}
for (int chan=0; chan<numChan开发者_如何学JAVAnels; chan++)
waves[chan].setSignal(ecg[chan]);
return false;
}
}
Waveform.java
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.PointF;
public class Waveform {
private int[] signal;
public void setSignal(int [] x)
{
signal = new int[x.length];
for (int n = 0; n < x.length; n++)
signal[n] = x[n];
}
public void drawSignal(Canvas c, PointF pos)
{
Paint paint = new Paint();
//paint.setColor(getResources().getColor(R.color.ECG_WaveI));
paint.setColor(Color.BLACK);
paint.setStyle(Style.STROKE);
Path path = new Path();
path.moveTo(pos.x, pos.y);
for (int n=1; n<ECGFilereader.numSamples; n++)
path.lineTo(pos.x, (pos.y+signal[(int) n]));
c.drawPath(path, paint);
}
}
You can't access files in the res folder directly. As you want to access the file in the raw folder, try getResource().openRawResource(int id)
Look also here: raw Resources Android filepath
精彩评论