写在SD卡上! Android,TXT数据

时间:2013-12-17 15:46:24

标签: java android inputstream outputstream

启动App时,我总是得到以下日志:

12-17 14:52:02.688: E/ThrottleService(287): problem during onPollAlarm: java.lang.IllegalStateException: problem parsing stats: java.io.FileNotFoundException: /proc/net/xt_qtaguid/iface_stat_all: open failed: ENOENT (No such file or directory)

我从我的主要活动开始一个意图,但在此之前:

Charakter ch = new Charakter(MainActivity.this,1, 2);

然后是启动方法:

Intent intent = new Intent(MainActivity.this,Charakteruebersicht.class);
            startActivity(intent);

这里的charakteruebersicht clas:

public class Charakteruebersicht extends Activity{

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.charakteruebersicht);
    TextView CT= (TextView) findViewById(R.id.tVCharakter);
    Charakter ch = new Charakter(Charakteruebersicht.this, 0, 0);
    Item it;
    Waffen waf = new Waffen();
    Rüstungen rüs = new Rüstungen();
    int waffe=waf.gibItem(ch.getaktuellerWaffenindex(Charakteruebersicht.this)).getWaffenschaden();
    int ruestung=rüs.gibItem(ch.getaktuellerRüstungsindex(Charakteruebersicht.this)).getRüstungswert();
    Log.d("Waffe", String.valueOf(waffe));
    Log.d("Rüstung", String.valueOf(ruestung));
    CT.setText("Der aktuelle Waffenschaden beträgt"+waffe +"!  Der aktuelle Rüstungswert beträgt:" +ruestung );[..]

这里有一个写入和读取无效:

private int leseWaffenindex(Context ct2) {
    String state = Environment.getExternalStorageState();
    File filesDir= new File(ct2.getCacheDir()+"/"+ "Waffen.txt");
     String datax = "0" ;
        try {

            FileInputStream fIn = new FileInputStream (filesDir) ;
            InputStreamReader isr = new InputStreamReader ( fIn ) ;
            BufferedReader buffreader = new BufferedReader ( isr ) ;

            String readString = buffreader.readLine ( ) ;
            while ( readString != null ) {
                datax =  readString ;
                readString = buffreader.readLine ( ) ;
            }

            isr.close ( ) ;
        } catch ( IOException ioe ) {
            ioe.printStackTrace ( ) ;
        }
        return Integer.parseInt(datax);
    }
private void writeWaffe(Context ct2, int aktuellerWaffenindex2) {
     File Meinspeicherort = new File(ct2.getCacheDir()+"/"+"Waffen.txt");
     BufferedWriter out=null;
     try {
        Meinspeicherort.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        out = new BufferedWriter(new FileWriter(Meinspeicherort));
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
     try {

            out.write ( aktuellerWaffenindex2 ) ;
            out.flush ( ) ;


        } catch ( Exception e ) {
            e.printStackTrace ( ) ;
        }finally
        {
            try {
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
       }
    }

1 个答案:

答案 0 :(得分:1)

您正在尝试打开位于系统文件夹中的文件。相反,您应该尝试在SDCard上或在应用程序的本地存储目录中创建文件。

请阅读此内容以获取帮助:http://developer.android.com/guide/topics/data/data-storage.html