对于Android,我是初学者。 关于写入文件,我遇到了一个问题。 我想将一个表单中的输入保存到文件中。 但是,我写的那段代码不是写在我的文件中。 有人可以帮帮我吗? 代码看起来像这样:
submit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { StringBuilder s = new StringBuilder(); s.append("Event name: " + editText1.getText() + "|"); s.append("Date: " + editText2.getText() + "|"); s.append("Details: " + editText3.getText() + "|"); File file = new File("D:\\config.txt"); try { BufferedWriter out = new BufferedWriter(new FileWriter(file, true), 1024); out.write(s.toString()); out.newLine(); out.close(); } catch (IOException e) { e.printStackTrace(); } } });所以,我有一个包含3个字段的表单:事件的名称,日期和描述。 这些我想保存到我的文件中。 我应该提一下,我使用模拟器进行测试。
I am a beginner when it comes to Android. I encountered a problem, regarding writing to a file. I want to save to a file the input I get in a form. However, the piece of code that I wrote is not writing in my file. Could anyone please help me? The code looks like that:
submit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { StringBuilder s = new StringBuilder(); s.append("Event name: " + editText1.getText() + "|"); s.append("Date: " + editText2.getText() + "|"); s.append("Details: " + editText3.getText() + "|"); File file = new File("D:\\config.txt"); try { BufferedWriter out = new BufferedWriter(new FileWriter(file, true), 1024); out.write(s.toString()); out.newLine(); out.close(); } catch (IOException e) { e.printStackTrace(); } } });So, I have a form containing 3 fields: the name of an event, the date and the description. These I want to save to my file. I should mention that I use an emulator for testing.
最满意答案
使用以下文件路径。 它会将文件写入存储的根文件夹。
String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); File file= new File(extStorageDirectory, "config.txt"); writeToFile("File content".getBytes(), file);将writeToFile
public static void writeToFile(byte[] data, File file) throws IOException { BufferedOutputStream bos = null; try { FileOutputStream fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(data); } finally { if (bos != null) { try { bos.flush (); bos.close (); } catch (Exception e) { } } } }不要忘记在AndroidManifest.xml添加以下权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />Use following path for file. It will write file to your root folder of storage.
String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); File file= new File(extStorageDirectory, "config.txt"); writeToFile("File content".getBytes(), file);writeToFile
public static void writeToFile(byte[] data, File file) throws IOException { BufferedOutputStream bos = null; try { FileOutputStream fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(data); } finally { if (bos != null) { try { bos.flush (); bos.close (); } catch (Exception e) { } } } }Don't forget to add following permission in AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />更多推荐
file,new,文件,电脑培训,计算机培训,IT培训"/> <meta name="description"
发布评论