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

时间:2015-02-23 00:05:44

标签: java android filenotfoundexception

我正在尝试使用解析.owl文件(OWL文件)并显示类和子类的Android应用程序代码。当我尝试打开文件时,我不断收到以下异常。

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();
    }
}

}

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

如何摆脱此异常并解析OWL文件?我还是Android编程的初学者。

1 个答案:

答案 0 :(得分:3)

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