简体   繁体   English

在android中的内部存储中创建一个txt文件

[英]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.

相关问题 从 Android 内部存储上的 .txt 文件中读取文本作为字符串 - Read text from .txt file on Android internal storage as string Android Studio:使用EditText在内部存储中编辑.txt文件 - Android Studio: Editing a .txt file in internal storage with EditText 将整数值保存在内部存储的txt文件中 - Save the value of an integer in a txt file in internal storage 将 txt 文件保存到 Oculus Go 内部存储 - Saving a txt file to the Oculus Go Internal Storage 将内部存储中的txt文件与保管箱保管箱api中的txt文件同步 - sync txt file in internal storage with txt file in dropbox dropbox api 如何从内部存储读取和写入来自 android 10 及更高版本的 txt 文件 - How to I read and write a txt file from android 10 and above from internal storage 如何在Android上的内部存储上创建文件夹并在该文件夹中写入简单的.txt文件 - How to create a folder and write a simple .txt file in that folder on Internal Storage on Android 如何将一个TXT文件从Assets文件夹复制到SD /内部存储Android C#Xamarin - How to copy one TXT File from Assets folder to SD / Internal Storage Android C# Xamarin Android内部存储未保存文件 - Android internal storage not saving file Android将文件保存到内部存储 - Android save file to internal storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM