Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android上保存字符串输入_Java_Android - Fatal编程技术网

Java 在Android上保存字符串输入

Java 在Android上保存字符串输入,java,android,Java,Android,是的,我一直在试图找到一个解决方案,但由于某些原因,它不起作用 简而言之,我要做的是将用户输入的每个输入字符串保存到一个文件中。每次再次创建活动时,我都希望将这些字符串重新输入到对象的新实例中 这段代码是我用来创建文件并从中读取信息的代码,用于activity的onCreate()方法 try { String brain = "brain"; File file = new File(this.getFilesDir(), brain);

是的,我一直在试图找到一个解决方案,但由于某些原因,它不起作用

简而言之,我要做的是将用户输入的每个输入字符串保存到一个文件中。每次再次创建活动时,我都希望将这些字符串重新输入到对象的新实例中

这段代码是我用来创建文件并从中读取信息的代码,用于activity的onCreate()方法

    try {

        String brain = "brain";
        File file = new File(this.getFilesDir(), brain);
        if (!file.exists()) {
            file.createNewFile();
        }      

        BufferedReader in = new BufferedReader(new FileReader(file));
        String s; // This feeds the object MegaAndroid with the strings, sequentially 
        while ((s = in.readLine()) != null) {
            MegaAndroid.add(s);
        }
        in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
之后,每次用户输入一些文本时,字符串都会保存到文件中:

    try {

        String brain = "brain";
        File file = new File(this.getFilesDir(), brain);
        if (!file.exists()) {
            file.createNewFile();
        }

    BufferedWriter out = new BufferedWriter(new FileWriter(file));
    out.write(message); // message is a string that holds the user input
    out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
但是,由于某些原因,每次应用程序被终止时,数据都会丢失。 我做错了什么


编辑:另外,如果我要从另一个类访问此文件,我该如何访问

这就是我读取文件的方法

try{

        File sdCard = Environment.getExternalStorageDirectory();
        File dir = new File (sdCard.getAbsolutePath() + "/whereyouwantfile");
        dir.mkdirs();

        Log.d(TAG,"path: "+dir.getAbsolutePath());
        File file = new File(dir, "VERSION_FILENAME");

        FileInputStream f = new FileInputStream(file);

        //FileInputStream fis = context.openFileInput(VERSION_FILENAME);
        BufferedReader reader = new BufferedReader(new InputStreamReader(f));

        String line = reader.readLine();
        Log.d(TAG,"first line versions: "+line);
        while(line != null){
            Log.d(TAG,"line: "+line);
            //Process line how you need 
            line = reader.readLine();
        }

        reader.close();
        f.close();
    }
    catch(Exception e){
        Log.e(TAG,"Error retrieving cached data.");

    }
以下是写作的要点

try{

        File sdCard = Environment.getExternalStorageDirectory();
        File dir = new File (sdCard.getAbsolutePath() + "/whereyouwantfile");
        dir.mkdirs();
        File file = new File(dir, "CONTENT_FILENAME");

        FileOutputStream f = new FileOutputStream(file);

        //FileOutputStream fos = context.openFileOutput(CONTENT_FILENAME, Context.MODE_PRIVATE);
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(f));

        Set<String> keys = Content.keySet();

        for(String key : keys){

            String data = Content.get(key);
            Log.d(TAG,"Writing: "+key+","+data);

            writer.write(data);
            writer.newLine();
        }

        writer.close();
        f.close();

    }
    catch(Exception e){
        e.printStackTrace();
        Log.e(TAG,"Error writing cached data.");

    }
试试看{
文件sdCard=Environment.getExternalStorageDirectory();
File dir=新文件(sdCard.getAbsolutePath()+“/whereyouwantfile”);
dir.mkdirs();
文件=新文件(目录,“内容\文件名”);
FileOutputStream f=新的FileOutputStream(文件);
//FileOutputStream fos=context.openFileOutput(CONTENT\u FILENAME,context.MODE\u PRIVATE);
BufferedWriter writer=新的BufferedWriter(新的OutputStreamWriter(f));
Set keys=Content.keySet();
用于(字符串键:键){
字符串数据=Content.get(key);
Log.d(标记“写入:“+key+”,“+data”);
writer.write(数据);
writer.newLine();
}
writer.close();
f、 close();
}
捕获(例外e){
e、 printStackTrace();
Log.e(标记“写入缓存数据时出错”);
}

如果你不想让其他人看到你的文件,你可以使用私有模式,但在调试时查看它们通常很有用。

正如我们在推荐部分中讨论的,代码的主要问题是,在截断文件时执行
FileWriter
操作之前发生了
FileReader
操作。要维护要将写入操作设置为追加的文件内容,请执行以下操作:

BufferedWriter out = new BufferedWriter(new FileWriter(file,true));
out.write(message);
out.newLine();
out.close();
但是,如果EditText上的每个条目都被接收到,然后被发送到文件中,那么您只需在它旁边一个字节接一个字节地写入数据。很容易获得类似的内容

这是第1行,这是第2行

而不是想要的

这是第1行
这是第2行


这可以通过让BufferedWriter在每次写入文件后传递一个换行来纠正。

在关闭它之前尝试调用.flush()。发布日志,请问文件中保存的内容是什么???@raghasood我认为
BufWriter.close()
会隐式调用
flush()
。@PremGenError:为什么不会呢?bufferedWriter“out”使用文件“file”作为参数。。。这不是应该的吗?是的,工作得很好。非常感谢你,你是个救命恩人!