Java 文件为空,并始终返回空值

Java 文件为空,并始终返回空值,java,android,file,file-io,Java,Android,File,File Io,我有几个方面可能是我的问题的根源,我希望我只是愚蠢,忘记了一个小细节。但基本上,我正在制作一个应用程序,从一个数据(txt)文件中读取数据并将其放入一个hashmap,然后使用该hashmap获得一个与字符串一起的相应整数。如果您感到困惑,那么在代码中会更有意义 因此,首先,当应用程序加载时,它作为MainActivity类中的第一个方法之一调用返回true或false的方法firstTime(): private boolean firstTime() { //checks i

我有几个方面可能是我的问题的根源,我希望我只是愚蠢,忘记了一个小细节。但基本上,我正在制作一个应用程序,从一个数据(txt)文件中读取数据并将其放入一个hashmap,然后使用该hashmap获得一个与字符串一起的相应整数。如果您感到困惑,那么在代码中会更有意义

因此,首先,当应用程序加载时,它作为MainActivity类中的第一个方法之一调用返回true或false的方法firstTime():

    private boolean firstTime() {
    //checks if the needed folders are in place
    File storelist = new File(this.getFilesDir()  + File.separator +"Stores");
    File listnames = new File(this.getFilesDir()  + File.separator +"Lists");
    System.out.println("Store File: "+new File(storelist+File.separator + "Sample Fresh.txt").getAbsolutePath());
    if(storelist.isDirectory() && listnames.isDirectory() && new File(storelist+File.separator + "Sample Fresh.txt").exists()){
        return false;
    }
    else{
        return true;
    }
}
然后,如果它返回true,它将调用一个安装活动,并初始化一些文件,是的,我知道整个数组输入是一个很大的不,但它除了点(我希望)之外,我正在尝试,暂时,保持所有的文件内部,很快我将移动到服务器设置,但作为一个示例/测试版,我不想这样做。以下方法generateSampleStore位于InstallActivity类中:

private File generateSampleStore(String path) {
    File file = new File(path);
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(file,true);
    } catch (FileNotFoundException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    String array[] = new String[56];
    array[0] = "Store Fresh";
    array[1] = "1234 Hickory Dr. Philadelphia Pa. 12345";
    array[2] = "(123)-555-6789";
    array[3] = "24 Hours";
    array[4] = "Ketchup Aisle 1";
    array[5] = "Salt Aisle 5";
    array[6] = "Sugar Aisle 5";
    array[7] = "Peanut Butter Aisle 5";
    array[8] = "Vanilla Aisle 5";
    array[9] = "Mustard Aisle 1";
    array[10] = "Spaghetti Aisle 6";
    array[11] = "Bananas Produce-Front Right";
    array[12] = "Pam Aisle 5";
    array[13] = "Frosted Flakes Aisle 4";
    array[14] = "Bacon Deli-Back Right";
    array[15] = "Eggs Dairy-Back Left";
    array[16] = "Yogurt Dairy-Back Left";
    array[17] = "Soy Sauce Aisle 7";
    array[18] = "Coca Cola Aisle 3";
    array[19] = "Coffee Aisle 3";
    array[20] = "Tuna Aisle 1";
    array[21] = "Pickles Deli-Back Right";
    array[22] = "Hot Cocoa Aisle 5";
    array[23] = "Steak Sauce Aisle 1";
    array[24] = "All Spice Aisle 11";
    array[25] = "Elbow Macaroni Aisle 6";
    array[26] = "Lemon Juice Produce-Front Right";
    array[27] = "Garlic Produce-Front Right";
    array[28] = "Garlic Salt Aisle 5";
    array[29] = "Cheese Dairy-Back Left";
    array[30] = "Italian Dressing Aisle 1";
    array[31] = "Onion Produce-Front Right";
    array[32] = "Chicken Breasts Meat-Back Center";
    array[33] = "Ranch Dressing Aisle 1";
    array[34] = "Bread Crumbs Aisle 12";
    array[35] = "Pancake Mix Aisle 5";
    array[36] = "Cake Mix Aisle 5";
    array[37] = "Apple Sauce Aisle 5";
    array[38] = "Food Coloring Aisle 5";
    array[39] = "Apples Produce-Front Right";
    array[40] = "Potato Chips Aisle 4";
    array[41] = "Salsa Aisle 7";
    array[42] = "Ice Tea Aisle 3";
    array[43] = "Corn Syrup Aisle 5";
    array[44] = "Egg Noodles Aisle 6";
    array[45] = "Butter Dairy-Back Left";
    array[46] = "Whole Wheat Bread Aisle 12";
    array[47] = "White Bread Aisle 12";
    array[48] = "Frozen Peas Aisle 13";
    array[49] = "Vanilla Ice Cream Aisle 15";
    array[50] = "Chocolate Ice Cream Aisle 15";
    array[51] = "Chrry Water Ice Aisle 15";
    array[52] = "Shredded American Cheese Dairy-Back Left";
    array[53] = "Whipped Cream Dairy-Back Left";
    array[54] = "Ground Beef Meat-Back Center";
    array[55] = "Frozen Pizza Aisle 14";


    for(int i = 0 ; i < array.length; i ++){
    try {
        fos.write('\n');
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        fos.write(array[i].getBytes());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        fos.flush();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
    try {
        fos.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return file;
}
私有文件生成示例存储(字符串路径){
文件=新文件(路径);
FileOutputStream=null;
试一试{
fos=新文件输出流(文件,真);
}捕获(FileNotFoundException e2){
//TODO自动生成的捕捉块
e2.printStackTrace();
}
字符串数组[]=新字符串[56];
数组[0]=“存储新鲜”;
数组[1]=“1234山胡桃树博士,宾夕法尼亚州费城,12345”;
数组[2]=“(123)-555-6789”;
数组[3]=“24小时”;
数组[4]=“番茄酱通道1”;
数组[5]=“盐通道5”;
数组[6]=“糖通道5”;
数组[7]=“花生酱通道5”;
数组[8]=“5”;
数组[9]=“芥末通道1”;
数组[10]=“意大利面通道6”;
数组[11]=“香蕉生产右前”;
数组[12]=“Pam通道5”;
数组[13]=“磨砂片通道4”;
数组[14]=“Bacon Deli Back Right”;
数组[15]=“左后”;
数组[16]=“左后”;
数组[17]=“酱油通道7”;
数组[18]=“可口可乐通道3”;
数组[19]=“咖啡通道3”;
数组[20]=“金枪鱼通道1”;
数组[21]=“泡菜熟食店右后”;
数组[22]=“热可可通道5”;
数组[23]=“牛排酱通道1”;
数组[24]=“所有香料通道11”;
数组[25]=“肘部通心粉通道6”;
数组[26]=“柠檬汁生产右前”;
数组[27]=“大蒜农右前”;
数组[28]=“大蒜盐通道5”;
数组[29]=“奶酪乳制品背面左”;
数组[30]=“意大利梳妆台通道1”;
数组[31]=“洋葱生产右前”;
数组[32]=“鸡胸肉背中心”;
数组[33]=“牧场更衣通道1”;
数组[34]=“面包屑通道12”;
数组[35]=“煎饼混合通道5”;
数组[36]=“蛋糕混合通道5”;
数组[37]=“苹果酱通道5”;
数组[38]=“食用色素通道5”;
数组[39]=“苹果生产右前方”;
数组[40]=“薯片通道4”;
数组[41]=“萨尔萨走道7”;
数组[42]=“冰茶通道3”;
数组[43]=“玉米糖浆通道5”;
数组[44]=“蛋面通道6”;
数组[45]=“黄油乳制品后左”;
数组[46]=“全麦面包通道12”;
数组[47]=“白面包通道12”;
数组[48]=“冷冻豌豆通道13”;
数组[49]=“香草冰淇淋通道15”;
数组[50]=“巧克力冰淇淋通道15”;
数组[51]=“Chrry水冰通道15”;
数组[52]=“左后切碎的美国奶酪乳制品”;
数组[53]=“鲜奶乳制品背面左侧”;
数组[54]=“碎牛肉后中”;
数组[55]=“冷冻比萨饼通道14”;
for(int i=0;i
然后用户点击“列表”选项卡,他们被带到一个列表名菜单,一旦他们点击一个列表,它就会调用openList()类,下面的方法就是我得到错误的方法,没有stacktrace,因为这不是编译错误,只是做了错误的事情(逻辑可能):

onCreate()调用中相关的部分:

try {
            store = new ShoppingList(name, this).getStore();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    try {
        items = getItems();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if(!(store.equalsIgnoreCase("none"))){
        try {
            Aislenum = getAisleNumbers(new Store(store, this));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        hasAislenums = true;
    }else{
        //do nothing
    }

    if(items[0] != null){
        if(hasAislenums){
        both = connectArrays(items, Aislenum);
        setListAdapter(new ArrayAdapter<String>(this, R.layout.singleitem , both));
        }else{
        setListAdapter(new ArrayAdapter<String>(this, R.layout.singleitem , items));
        }
    }
    else{
        String New[] = new String[2];
        New[0] = "This list is empty, add your first item";
        New[1] = "by clicking the add button above";
        setListAdapter(new ArrayAdapter<String>(this, R.layout.singleitem , New));  
    }
    }

private String[] connectArrays(String[] item, String[] aislenum) {
    String toReturn[] = new String[item.length];

    for(int i = 0; i < item.length; i ++){
        toReturn[i] = item[i] + " " + aislenum[i];
    }
    return toReturn;
}
试试看{
store=new ShoppingList(名称,this).getStore();
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
试一试{
items=getItems();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
if(!(store.equalsIgnoreCase(“none”)){
试一试{
Aislenum=getAisleNumbers(新店(店,本店));
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
Hasaiselums=真;
}否则{
//无所事事
}
如果(项目[0]!=null){
if(Hasaiselenums){
两者均=连接阵列(项目、通道);
setListAdapter(新的ArrayAdapter(this,R.layout.singleitem,两者));
}否则{
setListAdapter(新的ArrayAdapter(this,R.layout.singleitem,items));
}
}
否则{
字符串New[]=新字符串[2];
新建[0]=“此列表为空,请添加您的第一项”;
新建[1]=“通过单击上面的添加按钮”;
setListAdapter(新的ArrayAdapter(this,R.layout.singleitem,new));
}
}
私有字符串[]连接数组(字符串[]项,字符串[]Aisenum){
String toReturn[]=新字符串[item.length];
对于(int i=0;i    private String[] getAisleNumbers(Store store) {
String num[] = new String[items.length];
for(int i = 0; i < items.length; i ++){
    num[i] = store.getAisle(items[i]);
}

    return num;
}
    private String[] connectArrays(String[] item, String[] aislenum) {
    String toReturn[] = new String[item.length];

    for(int i = 0; i < item.length; i ++){
        toReturn[i] = item[i] + " " + aislenum[i];
    }
    return toReturn;
}
package dev.shaw.MyShoppingPlanner;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;

import android.content.Context;

/*
* All store files must be written in the following format!
* 
* Header:
* 
* line 0: Store name
* line 1: Store address Format for address: ((#) Street name (rd.ln.,ave., etc.) city state zipcode
* line 2: phone number
* line 3: hours
* 
* Item Formats:
* 
* ItemName AisleNumber
* Cherios 9
* 
*/
public class Store {
Dir dir = new Dir();
private static String storename;
private static File storefile;
private static HashMap<String, String> aislenums = new HashMap<String,String>();
public Store(String store, Context context) throws IOException {
    File file = new File(context.getFilesDir() + File.separator + "Stores" +File.separator + store + ".txt");
    storename = store;
    if(file.exists()){
        //do nothing
    }else{
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    storefile = file;
    aislenums = intAilseNums();


}
private HashMap<String, String> intAilseNums() throws IOException {
    System.out.println("Store File: "+storefile.getAbsolutePath());
    HashMap<String, String> hm = new HashMap<String,String>();
    FileInputStream fis = new FileInputStream(storefile);
    InputStreamReader reader = new InputStreamReader(fis);
    BufferedReader br = new BufferedReader(reader);
    String line = "";
    br.readLine();
    br.readLine();
    br.readLine();
    br.readLine();
    while(true){
    line = br.readLine();
    if(line == null){
        System.out.println("Line is now null");
        break;
    }
    StringTokenizer st = new StringTokenizer(line);
    String item = "";
    for(int i = 0; i < st.countTokens()- 2; i ++){
     item = item + " " + st.nextToken();
    }
    String num = st.nextToken() + " " + st.nextToken();
    hm.put(item, num);
    }

    /*br.reset();
    System.out.println(br.readLine());*/
    br.close();
    return hm;
}
public String getAisle(String item){
    return aislenums.get(item);
}
public  String getName() throws IOException{
    FileInputStream fis = new FileInputStream(storefile);
    InputStreamReader is = new InputStreamReader(fis); 
    BufferedReader br = new BufferedReader(is);
    return br.readLine();
}
public  String getAddress() throws IOException{
    String address = "";
    FileInputStream fis = new FileInputStream(storefile);
    InputStreamReader is = new InputStreamReader(fis); 
    BufferedReader br = new BufferedReader(is);
    br.readLine();
    address = br.readLine();
    br.close();
    return address;
}
public  String getStreetName() throws IOException{
    String name = "";
    FileInputStream fis = new FileInputStream(storefile);
    InputStreamReader is = new InputStreamReader(fis); 
    BufferedReader br = new BufferedReader(is);
    br.readLine();
    String line = br.readLine();
    StringTokenizer st = new StringTokenizer(line);
    st.nextToken();
    name = st.nextToken() + " " + st.nextToken();
    br.close();
    return name;
}
public  String getCity() throws IOException{
    String city = "";
    FileInputStream fis = new FileInputStream(storefile);
    InputStreamReader is = new InputStreamReader(fis); 
    BufferedReader br = new BufferedReader(is);
    br.readLine();
    String line = br.readLine();
    StringTokenizer st = new StringTokenizer(line);
    st.nextToken();
    st.nextToken();
    st.nextToken();
    city = st.nextToken();

    br.close();
    return city;
}
public  String getState() throws IOException{
    String state = "";
    FileInputStream fis = new FileInputStream(storefile);
    InputStreamReader is = new InputStreamReader(fis); 
    BufferedReader br = new BufferedReader(is);
    br.readLine();
    String line = br.readLine();
    StringTokenizer st = new StringTokenizer(line);
    st.nextToken();
    st.nextToken();
    st.nextToken();
    st.nextToken();
    state  = st.nextToken();
    br.close();
    return state;
}
public  String getZipCodeAsString() throws IOException{
    String zipcode = "";
    FileInputStream fis = new FileInputStream(storefile);
    InputStreamReader is = new InputStreamReader(fis); 
    BufferedReader br = new BufferedReader(is);
    br.readLine();
    String line = br.readLine();
    StringTokenizer st = new StringTokenizer(line);
    st.nextToken();
    st.nextToken();
    st.nextToken();
    st.nextToken();
    st.nextToken();
    zipcode  = st.nextToken();
    br.close();
    return zipcode;
}
public  int getZipCodeAsInteger() throws IOException{
    int zipcode = 0;
    FileInputStream fis = new FileInputStream(storefile);
    InputStreamReader is = new InputStreamReader(fis); 
    BufferedReader br = new BufferedReader(is);
    br.readLine();
    String line = br.readLine();
    StringTokenizer st = new StringTokenizer(line);
    st.nextToken();
    st.nextToken();
    st.nextToken();
    st.nextToken();
    st.nextToken();
    zipcode  = Integer.parseInt(st.nextToken());
    br.close();
    return zipcode;
}
public  String getPhoneNumber() throws IOException{
    String num = "";
    FileInputStream fis = new FileInputStream(storefile);
    InputStreamReader is = new InputStreamReader(fis); 
    BufferedReader br = new BufferedReader(is);
    br.readLine();
    br.readLine();
    num = br.readLine();
    br.close();
    return num;
}
public static  File getFile(){
    return storefile;
}
public  String getHours() throws IOException{
    FileInputStream fis = new FileInputStream(storefile);
    InputStreamReader is = new InputStreamReader(fis); 
    BufferedReader br = new BufferedReader(is);
    br.readLine();
    br.readLine();
    br.readLine();
    String hours = br.readLine();
    br.close();
    return hours;


}