gpt4 book ai didi

java - 我如何在 Android 应用程序中访问一个简单的 txt 文件?

转载 作者:行者123 更新时间:2023-11-30 04:29:04 27 4
gpt4 key购买 nike

我正在创建一个应用程序,其中列出了教师以及他们在我学校所做的所有事情。我有 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;
}

我应该把文件放在哪里,它还能作为文件对象工作吗?

最佳答案

如果你不知道把文件放在哪里你可以简单地说

File list = new File("TeacherInformation.txt")

并且会保存到本地,你可以用同样的方式访问

关于java - 我如何在 Android 应用程序中访问一个简单的 txt 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8031209/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com