Home > Enterprise >  How to write to files in android studio? Where are the files stored?
How to write to files in android studio? Where are the files stored?

Time:03-31

How would I be able to create a new file which will have a different file name each time? Would it also be possible to add line breaks when writing to these files? Also, how would I be able to access this file?

package com.example.create_recipe;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;

import androidx.appcompat.app.AppCompatActivity;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStreamWriter;


public class MainActivity extends AppCompatActivity  {
    EditText editTxtRecipeName, editTxtEquipment, editTxtIngredients, editTxtMethod, editPersonalStory;
    Spinner spnCountries, spnHours, spnMinutes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void createRecipe(Context context) throws FileNotFoundException {

        //TODO Create new file - should it be named after the recipe name or a unique int id?
        String recipeName = editTxtRecipeName.getText().toString();
        String country = spnCountries.getSelectedItem().toString();
        String hours = spnHours.getSelectedItem().toString(); 
        String minutes = spnMinutes.getSelectedItem().toString();
        String  equipment = editTxtEquipment.getText().toString();
        String ingredients = editTxtIngredients.getText().toString();
        String method = editTxtMethod.getText().toString();
        String personalStory = editPersonalStory.getText().toString();
        
        //TODO Write to file, adding new line breaks between recipeName, equipment and so on. 
        
    }



}

CodePudding user response:

WHat you need is an UUID and use it like so

val uuid = UUID.randomUUID().toString()
val path = Environment.getExternalStorageDirectory().path   "/"   FILE_NAME
val file = File(path)
BufferedOutputStream(FileOutputStream(file.path)).use { stream ->
    stream.write(uuid.toByteArray())
}

Please noteEnvironment.getExternalStorageDirectory() will not work post API 29. This example is just meant to show the use of UUID to generate unique values to store

CodePudding user response:

Getting your app directory (ContextWrapper is an Application/Activity/Service):

String dir = ContextWrapper#getFilesDir().getAbsolutePath();

Obtaining the complete file path:

String path = dir   "/"   fileName   ".anything";

Obtaining a file-object:

File file = new File(path);

Saving a byte array:

Files.write(dir, content);

Or a file:

FileWriter writer = new FileWriter(file);

In java, linebreaks use the character '\n', you can use that.

Load a byte array using String name:

Files.readAllBytes(path);

Or a file:

FileReader reader = new FileReader(file);

You'll have to come up for a system to name your files. To check if a file exists, just create the file object and call

file.exists() && !file.isDirectory()

For naming your files, you'll need to come up with a system. If recipeName is unique, you can use that. You'll find something that uniquely identifies your Recipe.

  • Related