我希望我能够将用户输入的内容放入文本文件中,然后在用户登录时读取,是的,这是密码。首选项很多使用共享或sqlite但我已经没有使用文件...所以当我按下按钮时,内容应该写在文本文件中。或者当我使用文件浏览器时,没有...感谢您的帮助!这是我的代码:
public class MDPinterne extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mdpinterne);
Button button = (Button) findViewById((R.id.button3));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText mdp = (EditText) findViewById(R.id.editText2);
String chmdp = mdp.getText().toString();
try {
// open myfilename.txt for writing
OutputStreamWriter out = new OutputStreamWriter(openFileOutput("mdpsecurity",0));
// write the contents on mySettings to the file
out.write(chmdp);
// close the file
out.close();
} catch (java.io.IOException e) {
//do something if an IOException occurs.
}
}
});
}
}
答案 0 :(得分:-1)
试试这个:
File root = new File(Environment.getExternalStorageDirectory(), "YOUR_FOLDER");
if (!root.exists()) {
root.mkdirs();
}
try
{
File mFile;
mFile = new File(root, "YOUR_FILE_NAME.TXT");
mFileWriter = new FileWriter(mFile);
mFileWriter.append("YOUR TEXT");
mFileWriter.flush();
mFileWriter.close();
}
catch(IOException e) {
e.printStackTrace();
}
并且不要忘记权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />