如何写入 android studio 中的文件?文件存储在哪里?
How to write to files in android studio? Where are the files stored?
我怎样才能创建一个每次都具有不同文件名的新文件?写入这些文件时是否也可以添加换行符?另外,我怎样才能访问这个文件?
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.
}
}
你需要的是一个UUID并像这样使用它
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())
}
请注意Environment.getExternalStorageDirectory()
将不起作用post API 29. 这个例子只是为了展示使用 UUID 生成唯一值来存储
正在获取您的应用程序目录(ContextWrapper 是一个 Application/Activity/Service):
String dir = ContextWrapper#getFilesDir().getAbsolutePath();
获取完整文件路径:
String path = dir + "/" + fileName + ".anything";
获得一个file-object:
File file = new File(path);
保存一个字节数组:
Files.write(dir, content);
Or a file:
FileWriter writer = new FileWriter(file);
在java中,换行符使用字符'\n',您可以使用它。
使用字符串名称加载字节数组:
Files.readAllBytes(path);
Or a file:
FileReader reader = new FileReader(file);
你必须想出一个系统来命名你的文件。要检查文件是否存在,just create the file object and call
file.exists() && !file.isDirectory()
要命名文件,您需要想出一个系统。如果 recipeName 是唯一的,您可以使用它。您会找到可以唯一标识您的食谱的东西。
我怎样才能创建一个每次都具有不同文件名的新文件?写入这些文件时是否也可以添加换行符?另外,我怎样才能访问这个文件?
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.
}
}
你需要的是一个UUID并像这样使用它
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())
}
请注意Environment.getExternalStorageDirectory()
将不起作用post API 29. 这个例子只是为了展示使用 UUID 生成唯一值来存储
正在获取您的应用程序目录(ContextWrapper 是一个 Application/Activity/Service):
String dir = ContextWrapper#getFilesDir().getAbsolutePath();
获取完整文件路径:
String path = dir + "/" + fileName + ".anything";
获得一个file-object:
File file = new File(path);
保存一个字节数组:
Files.write(dir, content);
Or a file:
FileWriter writer = new FileWriter(file);
在java中,换行符使用字符'\n',您可以使用它。
使用字符串名称加载字节数组:
Files.readAllBytes(path);
Or a file:
FileReader reader = new FileReader(file);
你必须想出一个系统来命名你的文件。要检查文件是否存在,just create the file object and call
file.exists() && !file.isDirectory()
要命名文件,您需要想出一个系统。如果 recipeName 是唯一的,您可以使用它。您会找到可以唯一标识您的食谱的东西。