2011-08-02 205 views
0

我寫了一個簡單的文件保存系統。但它不起作用。我之前寫過它,它工作。可能我忘了一些東西。有人能告訴我我忘了什麼嗎? 謝謝! XMLandroid:文件保存系統

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    android:id="@+id/hello" 
    /> 
<Button android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:id="@+id/btn" 
     android:text="save" 
     android:layout_gravity="center_vertical|center_horizontal"/> 
</LinearLayout> 

活動:

package com.android.savefiletest; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

public class SaveFileTestActivity extends Activity { 
    /** Called when the activity is first created. */ 
    FileService file; 
    TextView hello; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button btn = (Button)findViewById(R.id.btn); 
     hello = (TextView)findViewById(R.id.hello); 
     btn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       try { 
        file.save("1.txt", hello.getText().toString()); 
        Toast.makeText(SaveFileTestActivity.this, "success", 1).show(); 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
        Toast.makeText(SaveFileTestActivity.this, "error", 1).show(); 
       } 
      } 
     }); 
    } 
} 

FileService.java

package com.android.savefiletest; 

import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 

import android.content.Context; 

public class FileService{ 
    private Context context; 

    public FileService(Context context) { 
     this.context = context; 

    } 

    public void save(String filename, String content) throws Exception{ 
     FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE); 
     outStream.write(content.getBytes()); 
     outStream.close(); 
    } 
} 

回答

0

Activity

你忘了初始化文件

添加

file = new FileService(this); 
+0

啊.....是的,我真的很愚蠢。非常感謝! :) – nich

0

如果你想保持它如此簡單,你可以去這個鏈接看看。

http://developer.android.com/guide/topics/data/data-storage.html

或最終使用,

File sdcard = Environment.getExternalStorageDirectory(); 
File file = new File(sdcard,"count.txt"); 

hours=Integer.toString(calendar.get(Calendar.HOUR_OF_DAY)); 
    minutes=Integer.toString(calendar.get(Calendar.MINUTE)); 
    seconds=Integer.toString(calendar.get(Calendar.SECOND)); 
    try { 
     output = new BufferedWriter(new FileWriter(file)); 

    output.write(hours); 
    output.flush(); 
    output.write(" "); 
    output.flush(); 
    output.write(minutes); 
    output.flush(); 
    output.write(" "); 
    output.flush(); 
    output.write(seconds); 
    output.close(); 
    } catch (IOException e1) { 

     e1.printStackTrace(); 
    }