我找不到我试图用 filereader 类读取的文件
我创建了一个名为 ECGFilereader 的文件读取器类,它应该打开 .txt,但是当我调试时,它会向使用 txt.file 的 Waveform 类抛出 FileNotFoundException
。下面是我的编码,任何帮助或建议将不胜感激。
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<numChannels; 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);
}
}
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<numChannels; 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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法直接访问 res 文件夹中的文件。当您想要访问原始文件夹中的文件时,请尝试
getResource().openRawResource(int id)
另请参阅此处:原始资源 Android 文件路径
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