[英]Make a txt file in internal storage in android
i want to make one folder on root and put txt file and append data ' 我想在root上创建一个文件夹并放入txt文件并附加数据'
My java code 我的java代码
public void generateNoteOnSD(String sFileName, String sBody){
try
{
String fileName = "error";
String headings = "Hello, world!";
String path = "/data/root/";
File file = new File(path, fileName+".txt");
if (!file.exists()) {
file.mkdirs();
}
File gpxfile = new File(file, sFileName);
FileWriter writer = new FileWriter(gpxfile);
writer.append(sBody);
writer.flush();
writer.close();
// Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
}
catch(IOException e)
{
Log.d("file error", ""+e.getMessage());
}
}
I am getting file not found exception 我收到文件未找到异常
Please help me how can create a file on root folder in internal storage 请帮助我如何在内部存储中的根文件夹上创建文件
Try this function. 试试这个功能。
public void wrtieFileOnInternalStorage(Context mcoContext,String sFileName, String sBody){
File file = new File(mcoContext.getFilesDir(),"mydir");
if(!file.exists()){
file.mkdir();
}
try{
File gpxfile = new File(file, sFileName);
FileWriter writer = new FileWriter(gpxfile);
writer.append(sBody);
writer.flush();
writer.close();
}catch (Exception e){
}
}
Try something like this - 尝试这样的事情 -
public Boolean writeToSD(String text){
Boolean write_successful = false;
File root=null;
try {
// <span id="IL_AD8" class="IL_AD">check for</span> SDcard
root = Environment.getExternalStorageDirectory();
Log.i(TAG,"path.." +root.getAbsolutePath());
//check sdcard permission
if (root.canWrite()){
File fileDir = new File(root.getAbsolutePath());
fileDir.mkdirs();
File file= new File(fileDir, "samplefile.txt");
FileWriter filewriter = new FileWriter(file);
BufferedWriter out = new BufferedWriter(filewriter);
out.write(text);
out.close();
write_successful = true;
}
} catch (IOException e) {
Log.e("ERROR:---", "Could not write file to SDCard" + e.getMessage());
write_successful = false;
}
return write_successful;
}
From here - http://www.coderzheaven.com/2012/09/06/read-write-files-sdcard-application-sandbox-android-complete-example/ 从这里 - http://www.coderzheaven.com/2012/09/06/read-write-files-sdcard-application-sandbox-android-complete-example/
In this statement: 在这个声明中:
File gpxfile = new File(file, sFileName);
file should be directory (you use .txt
file here). 文件应该是目录(这里使用
.txt
文件)。
Also, read this . 另外,请阅读此内容 。 You can store you file in
/data/data/<your.package.name>/
dir (trying to use /data/root/
will cause Permission denied error). 您可以将文件存储在
/data/data/<your.package.name>/
dir中(尝试使用/data/root/
将导致Permission denied错误)。
Try this. 尝试这个。 Using this, I have created log file in SD card.
使用这个,我在SD卡中创建了日志文件。
public void writeFile(String text){
File tarjeta = Environment.getExternalStorageDirectory();
File logFile = new File(tarjeta.getAbsolutePath()+"/", "log.txt");
if (!logFile.exists())
{
try
{
logFile.createNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append(text);
buf.newLine();
buf.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.