我找不到我试图用 filereader 类读取的文件

发布于 2024-11-28 09:23:36 字数 2260 浏览 1 评论 0原文

我创建了一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟若柳尘 2024-12-05 09:23:36

您无法直接访问 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文