如何使用 android 在不同的行中写入一个 txt 文件
How to write a txt file in different lines using android
我想知道如何将 3 个字符串(比如说字符串数组)打印到 android 中的 .txt 文件。 (例如第 1 行:"red",第 2 行:"blue",第 3 行:"green")。
此外,我想先检查该文件是否存在,如果存在,则什么都不做,否则在单独的行中使用三种颜色创建它。
要在不同的行上创建一个包含三种颜色的文件:
ArrayList<String> colors = new ArrayList<String>();
colors.put("red");
colors.put("blue");
colors.put("green");
File file= new File(root, filename);
FileWriter writer = new FileWriter(file);
for (String color : colors) {
writer.append(color);
writer.append("\n");
}
writer.flush();
writer.close();
最好的方法是创建一个单独的 class 文件并使用它 class 来管理所有内部文件。这将很有帮助,因为您不必每次要读取或写入内部文件时都重复编写长代码块。
1) 创建一个新的 Class。在 Android Studio 中,右键单击左侧的 MainActivity class,然后单击新建 --> Java Class。将此命名为 class 个文件。
2) 删除除第一行以外的所有代码(以"package"开头)并粘贴下面的代码。
import android.content.Context;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
public class Files {
Context context;
public Files(Context context){
this.context = context;
}
public void clear(final String path){
File dir = context.getFilesDir();
File files = new File(dir, path);
boolean deleted = files.delete();
}
public void write(final String path, final String[] text){
write(path, text[0]);
for(int i=1; i<text.length; i++){
append(path, text[i]);
}
}
public void append(final String path, final String[] text){
for(String s : text){
append(path, s);
}
}
public String[] read(final String path){
ArrayList<String> list = readArr(path);
return list.toArray(new String[list.size()]);
}
public void append(final String path, final String text){
FileOutputStream outputStream;
try {
outputStream = context.openFileOutput(path, Context.MODE_APPEND);
if(!read(path).isEmpty()){
outputStream.write(System.getProperty("line.separator").getBytes());
}
outputStream.write(text.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void write(final String path, final String text){
FileOutputStream outputStream = null;
try {
outputStream = context.openFileOutput(path, Context.MODE_PRIVATE);
outputStream.write(text.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public ArrayList<String> readArr(String path){
ArrayList<String> text = new ArrayList<String>();
FileInputStream inputStream;
try {
inputStream = context.openFileInput(path);
InputStreamReader isr = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(isr);
String line;
while ((line = bufferedReader.readLine()) != null) {
text.add(line);
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
return text;
}
public boolean isEmpty(final String path){
return (readArr(path).size()==0);
}
}
在您的 MainActivity class 中,在 onCreate 方法中创建一个文件实例:
Files file = new Files(this);
现在,您可以使用文件 class 中的所有方法。这是他们的工作:
String path = "example.txt"; //This is where the data will go
String[] exampleArrayOne = {"red", "green", "blue"};
String[] exampleArrayTwo = {"purple", "orange", "indigo"};
file.clear(path); //Clears a file
boolean isEmpty = file.isEmpty(path); //Will be true if the file is empty,
//otherwise false
file.write(path, "pink"); //Will clear the file and write pink on
//the first line
file.write(path, exampleArrayOne); //Will clear the file and write the array,
//each element on a new line
file.append(path, "yellow"); //Will NOT clear the file and will add
//"yellow" to the next empty line
file.append(path, exampleArrayTwo); //Will NOT clear the file and will add
//each element of the array to an empty
//line
确保您知道 write(path, array) 和 append(path, array) 方法之间的区别。 write(path, array) 将清除文件并覆盖它,而 append(path, array) 将添加到现有文件。
要从文件中读取,read(path) 将 return a String[];
String[] fileText = file.read(path);
最后,检查一个文件是否存在,否则添加它,你可以这样做:
String path = "whateverYouLike.txt";
String[] colors = {"red", "green", "blue"};
//If the file is empty, write the array to it
if(file.isEmpty(path)){
file.append(path, colors);
}
我想知道如何将 3 个字符串(比如说字符串数组)打印到 android 中的 .txt 文件。 (例如第 1 行:"red",第 2 行:"blue",第 3 行:"green")。
此外,我想先检查该文件是否存在,如果存在,则什么都不做,否则在单独的行中使用三种颜色创建它。
要在不同的行上创建一个包含三种颜色的文件:
ArrayList<String> colors = new ArrayList<String>();
colors.put("red");
colors.put("blue");
colors.put("green");
File file= new File(root, filename);
FileWriter writer = new FileWriter(file);
for (String color : colors) {
writer.append(color);
writer.append("\n");
}
writer.flush();
writer.close();
最好的方法是创建一个单独的 class 文件并使用它 class 来管理所有内部文件。这将很有帮助,因为您不必每次要读取或写入内部文件时都重复编写长代码块。
1) 创建一个新的 Class。在 Android Studio 中,右键单击左侧的 MainActivity class,然后单击新建 --> Java Class。将此命名为 class 个文件。
2) 删除除第一行以外的所有代码(以"package"开头)并粘贴下面的代码。
import android.content.Context;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
public class Files {
Context context;
public Files(Context context){
this.context = context;
}
public void clear(final String path){
File dir = context.getFilesDir();
File files = new File(dir, path);
boolean deleted = files.delete();
}
public void write(final String path, final String[] text){
write(path, text[0]);
for(int i=1; i<text.length; i++){
append(path, text[i]);
}
}
public void append(final String path, final String[] text){
for(String s : text){
append(path, s);
}
}
public String[] read(final String path){
ArrayList<String> list = readArr(path);
return list.toArray(new String[list.size()]);
}
public void append(final String path, final String text){
FileOutputStream outputStream;
try {
outputStream = context.openFileOutput(path, Context.MODE_APPEND);
if(!read(path).isEmpty()){
outputStream.write(System.getProperty("line.separator").getBytes());
}
outputStream.write(text.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void write(final String path, final String text){
FileOutputStream outputStream = null;
try {
outputStream = context.openFileOutput(path, Context.MODE_PRIVATE);
outputStream.write(text.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public ArrayList<String> readArr(String path){
ArrayList<String> text = new ArrayList<String>();
FileInputStream inputStream;
try {
inputStream = context.openFileInput(path);
InputStreamReader isr = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(isr);
String line;
while ((line = bufferedReader.readLine()) != null) {
text.add(line);
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
return text;
}
public boolean isEmpty(final String path){
return (readArr(path).size()==0);
}
}
在您的 MainActivity class 中,在 onCreate 方法中创建一个文件实例:
Files file = new Files(this);
现在,您可以使用文件 class 中的所有方法。这是他们的工作:
String path = "example.txt"; //This is where the data will go
String[] exampleArrayOne = {"red", "green", "blue"};
String[] exampleArrayTwo = {"purple", "orange", "indigo"};
file.clear(path); //Clears a file
boolean isEmpty = file.isEmpty(path); //Will be true if the file is empty,
//otherwise false
file.write(path, "pink"); //Will clear the file and write pink on
//the first line
file.write(path, exampleArrayOne); //Will clear the file and write the array,
//each element on a new line
file.append(path, "yellow"); //Will NOT clear the file and will add
//"yellow" to the next empty line
file.append(path, exampleArrayTwo); //Will NOT clear the file and will add
//each element of the array to an empty
//line
确保您知道 write(path, array) 和 append(path, array) 方法之间的区别。 write(path, array) 将清除文件并覆盖它,而 append(path, array) 将添加到现有文件。
要从文件中读取,read(path) 将 return a String[];
String[] fileText = file.read(path);
最后,检查一个文件是否存在,否则添加它,你可以这样做:
String path = "whateverYouLike.txt";
String[] colors = {"red", "green", "blue"};
//If the file is empty, write the array to it
if(file.isEmpty(path)){
file.append(path, colors);
}