当前位置: 首页 > 知识库问答 >
问题:

Android-java.io.FileNotFoundExcema打开失败:ENOENT(没有这样的文件或目录)

拓拔弘化
2023-03-14

我正在尝试编写一个Android应用程序,它将解析一个. owl文件(OWL文件)并显示类和子类。当我尝试打开文件时,我一直得到以下异常。

02-22 18:48:40.421: W/System.err(535): java.io.FileNotFoundException: /Antibiotics.owl: open failed: ENOENT (No such file or directory)
02-22 18:48:40.431: W/System.err(535):  at libcore.io.IoBridge.open(IoBridge.java:406)
02-22 18:48:40.431: W/System.err(535):  at java.io.FileInputStream.<init>(FileInputStream.java:78)
02-22 18:48:40.431: W/System.err(535):  at com.Sample.SampleApp.StartingPoint.parseOWLFile(StartingPoint.java:102)
02-22 18:48:40.431: W/System.err(535):  at com.Sample.SampleApp.StartingPoint$3.onClick(StartingPoint.java:66)

以下是我的Android代码:

import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
import android.os.Bundle;

public class StartingPoint extends ActionBarActivity {

private static final int REQUEST_PATH = 1;
String currentFile;
EditText et1;
Button browse, ok, exit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    et1 = (EditText)findViewById(R.id.editText);

    browse = (Button) findViewById(R.id.browseButton);
    browse.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v)
        {
            getfile(v);
        }

    });

    exit = (Button)findViewById(R.id.ExitButton);
    exit.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v)
        {
            Intent i = new Intent(Intent.ACTION_MAIN);
            i.addCategory(Intent.CATEGORY_HOME);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
        }

    });

    ok = (Button)findViewById(R.id.OKButton);
    ok.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v)
        {
            parseOWLFile();
        }

    });
}

public void getfile(View view)
{
    Intent i1 = new Intent(this, FileChooser.class);
    startActivityForResult(i1, REQUEST_PATH);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(requestCode == REQUEST_PATH)
    {
        if(resultCode == RESULT_OK)
        {
            currentFile = data.getStringExtra("GetFileName");
            et1.setText(currentFile);
        }
    }
}

public void parseOWLFile()
{
    //String file = et1.getText().toString(); //Uncomment this when using on phone/tablet

    String file = "Antibiotics.owl";

    try
    {
        //System.out.println("File name is "+file);

        File f = new File(file);
        System.out.println("File name is "+f.toString());
        FileInputStream fis = new FileInputStream(f);

        OntModel base = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM );
        base.read(fis, null);

        String ns = base.getNsPrefixURI("Ontology");

        Resource r = base.getResource(ns+" Concept");
        OntClass theClass = r.as(OntClass.class);

        Individual indiv = base.createIndividual(ns+"IndivTest", theClass);

        for (Iterator<Resource> i = indiv.listRDFTypes(true); i.hasNext(); )
            System.out.println( indiv.getURI() + " is asserted in class " + i.next() );

        OntModel inf = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_MICRO_RULE_INF, base );

        Individual ind = inf.getIndividual( ns + "indivTest" );
        for (Iterator<Resource> i = ind.listRDFTypes(true); i.hasNext(); )
            System.out.println( ind.getURI() + " is inferred to be in class " + i.next() );



    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

}

我在项目的父目录中有文件“antiminaties.owl”。我正在模拟器上运行这个项目。

我如何摆脱这个异常并解析OWL文件?我仍然是Android编程的初学者。

共有1个答案

柴岳
2023-03-14

您必须将文件放入资产文件夹或原始文件夹,然后才能访问该文件。你可以在这个链接中阅读更多的Android应用程序模块。在这里你可以找到如何读取原始文件夹中的文件。

 类似资料:
  • null 我在用这个代码 但我一直得到这个错误

  • 方法总是给出这样的错误: 打开失败:ENOENT(没有这样的文件或目录)

  • 我不知道这里出了什么问题...我试着把这个写得更简洁些,但没有奏效。我在阅读了这个问题的其他建议后加入了所有额外的字符串。帮不上忙。不知道发生了什么。可能是权限相关的吗?AFAIK我正试图写入内存,这不需要特殊的权限? 它每次都是“制造dirs”,目录不是停留在制造,或者其他什么。当它到达myfile.createnewfile()时;它给出错误消息“Open Failed:ENOENT(没有这样

  • 我在节点Js中工作。当我尝试加载文件时:moviedata.json,如下行: 显示: 错误:ENOENT:没有这样的文件或目录,打开'./moviedata.json'在错误(本机)在Object.fs.open同步(fs.js:640: 18)在Object.fs.readFileSync(fs.js:508: 33)在对象。(/用户/dortiz/文档/NodeJS/pruebas/zw/a

  • 我有一个错误: 警告:require_once(../questionTypes/Question.php):无法打开流:第25行的/home/u949913003/public_html/includes/essential.php中没有此类文件或目录 致命错误:require_once():无法在/home/u949913003/public_html/includes/essential.p

  • 问题内容: 我从节点应用程序收到此错误: 我知道文件在那里,因为当我尝试使用确切的复制和粘贴路径打开文件时,它可以工作。我也知道应用程序使用的是正确的目录,因为它会在错误中输出它。 问题答案: 波浪形扩展是一件空壳的事情。编写正确的路径名(可能是yourusername )或使用