我正在创建一个应用程序,其中列出了教师以及他们在学校所做的所有事情。我有3个文件,一个类Teacher,它是一个包含7个字符串变量的对象,一个TeacherList类,我在其中获取.txt文件并从中提取我的数据以形成教师。这就是我的问题所在,我应该把文件放在哪里?下面是TeacherList代码:
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;
}
我应该把文件放在哪里,它仍然可以作为文件对象使用吗?
答案 0 :(得分:1)
如果你不知道把文件放在哪里,你可以简单地说
File list = new File("TeacherInformation.txt")
它将在本地保存,您可以以相同的方式访问它
答案 1 :(得分:0)
是固定文件还是要添加?如果已修复,请放入assets文件夹。有点像...
Scanner iScanner=new Scanner(getAssets().open("TeacherInformation.txt"));