首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android:文件保存系统

android:文件保存系统
EN

Stack Overflow用户
提问于 2011-08-02 18:22:50
回答 2查看 326关注 0票数 0

我写了一个简单的文件保存系统。但它不起作用。我以前写过它,它起作用了。可能是我忘了什么。谁能告诉我我忘了什么?谢谢!XML

代码语言:javascript
复制
<?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>

活动:

代码语言:javascript
复制
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

代码语言:javascript
复制
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();
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-02 18:31:31

Activity

你忘记初始化文件了

添加

代码语言:javascript
复制
file = new FileService(this);
票数 0
EN

Stack Overflow用户

发布于 2011-08-02 18:31:36

如果你想让它变得简单,你可以去这个链接看看。

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

或者最终使用这个,

代码语言:javascript
复制
 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();
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6910557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档