我编写了简单的代码,用于在内部将数据保存到文本文件中。但是,运行代码后,我不知道在哪里可以找到所需的文件。另外,我在日志中发现了一条错误消息
"SPAN_EXCLUSIVE_EXCLUSIVE"
package com.example.saving_files;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
File file;
FileOutputStream fos;
String FlieName = "output.text";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
file = new File(FlieName);
try {
fos = openFileOutput(FlieName, MODE_PRIVATE);
fos.write(122);
} catch (FileNotFoundException e) {
Log.d("output", file.getAbsolutePath());
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
file = getFilesDir();
Log.d("output_path", file.getAbsolutePath());
}
}
答案 0 :(得分:0)
检查此代码和评论;
File sdcard = Environment.getExternalStorageDirectory();
File f = new File(sdcard, "/yourfile");
if (!f.exsist()) {
f.createNewFile();
// Use outwriter here, (outputstream) search how to write into a text file in java code
}
答案 1 :(得分:0)
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard, "filename");
FileOutputStream f = new FileOutputStream(file);
此代码将在SD的根目录中创建新文件。别忘了添加
<uses-permission>
在您的清单中写入SD