Android写入文件不起作用(Android write to file not working)

编程入门 行业动态 更新时间:2024-10-06 22:26:04
Android写入文件不起作用(Android write to file not working)

对于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"

本文发布于:2023-08-07 11:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464127.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   文件   Android   working   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!