come posso scrivere un file dall’app alla cartella Test?

ho creato la cartella Test nella radice della memoria interna. come posso scrivere un file dall’app a questa cartella Test? (per esempio whatsapp.)

public class ReadWriteFile {
    public static void ReadWriteFile(String text, Context context) {
        DateFormat timeFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
        Date currentDate = new Date();
        text = timeFormat.format(currentDate) + text;
        try {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("/test/test.log", Context.MODE_WORLD_WRITEABLE));
            outputStreamWriter.write(text);
            outputStreamWriter.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

EN From: How can I write a file from the app to folder Test?

More similar articles:

3 Comments

  1. usando questo codice puoi creare la tua directory

    String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Folder_Name";
            File dir = new File(path);
            try {
                if (!dir.exists())
                    dir.mkdir();
            } catch (Exception e) {
                e.printStackTrace();
            }

    e usando questo salvare il file insdie

    String targetPdf = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Folder_Name/" + filename;

    e salvare il file

    new FileOutputStream(targetPdf)

    spero che questo vi aiuterà.

  2. esempio per scrivere file di testo nella cartella:

    File mDir = new File(getCacheDir(), "yourFolder");
    mDir.mkdir();
    
    
    try {
            String root_path = Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/MyFolder/";
            File root = new File(root_path);
            if (!root.exists()) {
                root.mkdirs();
            }
            File f = new File(root_path + "mttext.txt");
            if (f.exists()) {
                f.delete();
            }
            f.createNewFile();
    
            FileOutputStream out = new FileOutputStream(f);
    
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

Leave a Reply

Your email address will not be published. Required fields are marked *