使用fileinputstream在android上的filenotfoundexception

时间:2014-04-15 22:22:18

标签: java android filenotfoundexception fileinputstream fileoutputstream

好吧,我正在尝试访问文本文件并读取数据并将其保存到列表(DSLL),但我不断收到filenotfoundexception,这是调用该文件的代码部分。

package com.example.student_lists;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Locale;

import com.resources.student_list.DSLL;
import com.resources.student_list.FileManager;
import com.resources.student_list.Student;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;
    static EditText inputSearch;
    static DSLL<Student> studentList;
    FileManager file = new FileManager();
    FileInputStream fileInput; 
    FileOutputStream fileOutput; 

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

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by
            // the adapter. Also specify this Activity object, which implements
            // the TabListener interface, as the callback (listener) for when
            // this tab is selected.
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }

        inputSearch = (EditText) this.findViewById(R.id.inputSearch);

        try {
            fileInput = openFileInput("student.txt");
            Log.e("FILE INPUT","FOUND!!");
            studentList = file.read(fileInput,getApplicationContext());
            Log.e("_____",studentList.toString());
        } 
        catch (Exception e) {
            e.printStackTrace();
        }
    }

这里是logcat:

正如您将看到的,前几个错误是filenotfoundexceptions,然后我得到了nullexception错误,因为我无法从文件中读取任何内容。

04-15 18:17:29.061: D/dalvikvm(4233): Late-enabling CheckJNI
04-15 18:17:29.081: D/OpenGLRenderer(445): Flushing caches (mode 1)
04-15 18:17:29.091: D/dalvikvm(4233): Debugger has detached; object registry had 1 entries
04-15 18:17:29.151: W/System.err(4233): java.io.FileNotFoundException: /data/data/com.example.student_lists/files/student.txt: open failed: ENOENT (No such file or directory)
04-15 18:17:29.151: W/System.err(4233):     at libcore.io.IoBridge.open(IoBridge.java:406)
04-15 18:17:29.151: W/System.err(4233):     at java.io.FileInputStream.<init>(FileInputStream.java:78)
04-15 18:17:29.151: W/System.err(4233):     at android.app.ContextImpl.openFileInput(ContextImpl.java:610)
04-15 18:17:29.151: W/System.err(4233):     at android.content.ContextWrapper.openFileInput(ContextWrapper.java:159)
04-15 18:17:29.151: W/System.err(4233):     at com.example.student_lists.MainActivity.onCreate(MainActivity.java:90)
04-15 18:17:29.151: W/System.err(4233):     at android.app.Activity.performCreate(Activity.java:4466)
04-15 18:17:29.151: W/System.err(4233):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1092)
04-15 18:17:29.151: W/System.err(4233):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1924)
04-15 18:17:29.151: W/System.err(4233):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
04-15 18:17:29.151: W/System.err(4233):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
04-15 18:17:29.151: W/System.err(4233):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
04-15 18:17:29.151: W/System.err(4233):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 18:17:29.151: W/System.err(4233):     at android.os.Looper.loop(Looper.java:137)
04-15 18:17:29.151: W/System.err(4233):     at android.app.ActivityThread.main(ActivityThread.java:4447)
04-15 18:17:29.151: W/System.err(4233):     at java.lang.reflect.Method.invokeNative(Native Method)
04-15 18:17:29.151: W/System.err(4233):     at java.lang.reflect.Method.invoke(Method.java:511)
04-15 18:17:29.151: W/System.err(4233):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-15 18:17:29.151: W/System.err(4233):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-15 18:17:29.151: W/System.err(4233):     at dalvik.system.NativeStart.main(Native Method)
04-15 18:17:29.151: W/System.err(4233): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
04-15 18:17:29.151: W/System.err(4233):     at libcore.io.Posix.open(Native Method)
04-15 18:17:29.151: W/System.err(4233):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
04-15 18:17:29.151: W/System.err(4233):     at libcore.io.IoBridge.open(IoBridge.java:390)
04-15 18:17:29.151: W/System.err(4233):     ... 18 more
04-15 18:17:29.171: D/OpenGLRenderer(445): Flushing caches (mode 0)
04-15 18:17:29.191: D/AndroidRuntime(4233): Shutting down VM
04-15 18:17:29.191: W/dalvikvm(4233): threadid=1: thread exiting with uncaught exception (group=0x40a631f8)
04-15 18:17:29.191: E/AndroidRuntime(4233): FATAL EXCEPTION: main
04-15 18:17:29.191: E/AndroidRuntime(4233): java.lang.NullPointerException
04-15 18:17:29.191: E/AndroidRuntime(4233):     at com.example.student_lists.MainActivity$DummySectionFragment.onCreateView(MainActivity.java:298)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.view.View.measure(View.java:12728)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4704)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.view.View.measure(View.java:12728)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.view.View.measure(View.java:12728)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4704)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2129)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.view.View.measure(View.java:12728)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1096)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2474)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.os.Looper.loop(Looper.java:137)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at android.app.ActivityThread.main(ActivityThread.java:4447)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at java.lang.reflect.Method.invokeNative(Native Method)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at java.lang.reflect.Method.invoke(Method.java:511)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-15 18:17:29.191: E/AndroidRuntime(4233):     at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案