0

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. 
        
    }



}
jrrj
  • 17
  • 3
  • 1. You can use random file name generation, or set some constant name and change only the number of this file. For instance: File1, File2, File3. To do this, you need to check for the existence of the file. 2. In order to get a file you can implement a list that will contain all the files. It is best to create a separate folder for saving files and read only it. – ACTPOHOM Mar 29 '22 at 15:50

2 Answers2

0

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

Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31
0

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.

Cactusroot
  • 1,019
  • 3
  • 16