gpt4 book ai didi

java - 如何在 FileOutputStream 中保存两个以上的字符串并在不同的 TextView 中读取这些字符串?

转载 作者:行者123 更新时间:2023-11-30 11:22:11 26 4
gpt4 key购买 nike

我想将三个字符串保存在手机的内部存储器中。但是我在获取输出时遇到了问题。下面是程序。我只写下了重要的部分,没有写下其他不重要的代码。

Spinner 中传递了这三个字符串中的 string1(此处未显示,因为这对我来说不是问题)。

现在我想在 textview1 上加载 string2,在 textview2 上加载 string3。我在 textview1 上得到的输出是 string2string3 并且在 textview2 上我也得到了 string2string3

谁能帮我在 textview1 上获取 string2 和在 textview2 上获取 string3

PS:你看到输出上的微调器,那是我的输出。我只是留在那里让你明白我已经在微调器中有 string1。

写.java

    EditText filename, entry, pass;
String FILENAME, JOUR, PASSWORD;
filename = (EditText) findViewById(R.id.editText3);
entry = (EditText) findViewById(R.id.editText1);
pass = (EditText) findViewById(R.id.editText2);
public void onClick(View arg0) {
// TODO

Auto-generated method stub
FILENAME = filename.getText().toString();
if (FILENAME.contentEquals("")){
FILENAME = "passwordprotect";
}
JOUR = entry.getText().toString();
PASSWORD = pass.getText().toString();

try {
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(JOUR.getBytes());
fos.write(PASSWORD.getBytes());
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Read.java

 TextView entry, passentry;
entry = (TextView)findViewById(R.id.textView1);
passentry = (TextView)findViewById(R.id.textView2);

private void openFile(String selectFile) {
// TODO Auto-generated method stub


String value = "";

FileInputStream fis;

try {
fis = openFileInput(selectFile);
byte[] input = new byte[fis.available()];
while(fis.read(input) != -1){
value += new String(input);

}
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
entry.setText(value);
passentry.setText(value);
}

我得到的输出。

Spinner->string1
textview1->string2string3
textview2->string2string3

最佳答案

确定一些东西来划分这两个字符串。

根据您在这些 TextView 中存储的文本类型,不同的东西可能就足够了。例如,您可以简单地使用换行符来分隔两者。然后你可以将你的字符串 value 分成两部分,将每个部分存储在各自的 TextView 中。

基本上以某种方式划分文件中的字符串,然后将您的 value 字符串分成两部分,并将每个部分存储到相应的 TextView 中。

另一方面,考虑到您只是存储字符串,您是否考虑过使用 Android Preferences

关于java - 如何在 FileOutputStream 中保存两个以上的字符串并在不同的 TextView 中读取这些字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21835531/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com