Java 如何在Android应用程序中访问简单的txt文件?
我正在创建一个应用程序,列出老师和他们在学校做的所有事情。我有3个文件,一个class Teacher,它是一个包含7个字符串变量的对象,一个TeacherList类,我在其中获取.txt文件并从中提取数据以形成教师。这就是我的问题所在,我应该把文件放在哪里?教师名单代码如下:Java 如何在Android应用程序中访问简单的txt文件?,java,android,android-layout,Java,Android,Android Layout,我正在创建一个应用程序,列出老师和他们在学校做的所有事情。我有3个文件,一个class Teacher,它是一个包含7个字符串变量的对象,一个TeacherList类,我在其中获取.txt文件并从中提取数据以形成教师。这就是我的问题所在,我应该把文件放在哪里?教师名单代码如下: package com.mthebron.mthapp; import java.io.File; import java.io.FileNotFoundException; import java.util.Scann
package com.mthebron.mthapp;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TeacherList {
public static Teacher[] MakeList() throws FileNotFoundException{
Teacher[] teachers=new Teacher[71];
File list=new File("C:/Users/Gareth/Desktop/TeacherInformation.txt");
Scanner iScanner=new Scanner(list);
StringBuilder teachersBuilder=new StringBuilder();
while(iScanner.hasNext()) {
teachersBuilder.append(iScanner.nextLine());
teachersBuilder.append("\n");
}
String fullList=teachersBuilder.toString();
String[] seperatedList=fullList.split("'");
String[] blah=seperatedList[0].split("\n");
String temp=blah[0].substring(3, 9);
String name=temp;
String department=blah[1];
String planning=blah[2];
String club=blah[3];
String sport=null;
String email=blah[5];
String website=null;
teachers[0]=new Teacher(name, department, planning, club, sport, email, website);
System.out.println(teachers[0].getSportsCoached());
for (int i = 1; i < seperatedList.length; i++) {
blah=seperatedList[i].split("\n");
name=blah[1];
department=blah[2];
if (blah[3].equals("null")) {
planning=null;
}else planning=blah[3];
if (blah[4].equals("null")) {
sport=null;
}else sport=blah[4];
if (blah[5].equals("null")) {
club=null;
}else club=blah[5];
if (blah[6].equals("null")) {
email=null;
}else email=blah[6];
if (blah[7].equals("null")) {
website=null;
}else website=blah[7];
teachers[i]=new Teacher(name, department, planning, sport, club, email, website);
}
return teachers;
}
package com.mthebron.mthapp;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
公开课教师名单{
公共静态教师[]MakeList()引发FileNotFoundException{
教师[]教师=新教师[71];
文件列表=新文件(“C:/Users/Gareth/Desktop/TeacherInformation.txt”);
扫描仪iScanner=新扫描仪(列表);
StringBuilder教师Builder=新建StringBuilder();
while(iScanner.hasNext()){
append(iScanner.nextLine());
teachersBuilder.append(“\n”);
}
String fullList=teachersBuilder.toString();
字符串[]separatedList=fullList.split(“”);
字符串[]blah=separatedList[0]。拆分(“\n”);
字符串temp=blah[0]。子字符串(3,9);
字符串名称=temp;
字符串部门=废话[1];
字符串规划=废话[2];
弦乐棒=废话[3];
字符串sport=null;
字符串email=blah[5];
字符串=null;
教师[0]=新教师(姓名、部门、计划、俱乐部、体育、电子邮件、网站);
System.out.println(教师[0].getSportsCoached());
对于(int i=1;i
我应该把文件放在哪里,它还能作为文件对象使用吗?如果你不知道把文件放在哪里,你可以简单地说
File list = new File("TeacherInformation.txt")
它将保存在本地,你可以用同样的方式访问它,如果你不知道把文件放在哪里,你可以说
File list = new File("TeacherInformation.txt")
它将保存在本地,您可以用相同的方式访问它。它是一个固定的文件还是您正在添加到其中?如果是固定的,请放入资产文件夹。类似于
Scanner iScanner=new Scanner(getAssets().open("TeacherInformation.txt"));
它是一个固定的文件还是您正在添加到其中?如果是固定的,请放入资产文件夹。类似于
Scanner iScanner=new Scanner(getAssets().open("TeacherInformation.txt"));
“local”在哪里,比如android项目的哪个文件夹中?那么“local”在哪里,比如android项目的哪个文件夹中?当我添加这一行时,我得到一个错误,说getAssets不是一个方法,文件是固定的,我将它添加到assets文件夹中你需要一个“上下文”。我假设您是直接从活动中调用的。请参见此处:当我添加此行时,我收到一个错误,该错误表示getAssets不是方法。文件已修复,我将其添加到资产文件夹中。您需要“上下文”。我假设您是直接从活动中调用的。请参见此处: