File Empty并始终返回null值

时间:2014-04-08 02:16:43

标签: java android file file-io

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

首先,当应用程序加载它时,调用它作为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;
}

然后用户点击“列表”选项卡,他们被带到列表名称菜单,一旦他们点击它调用类openList()的列表,下面的方法是我得到错误的方法,没有堆栈跟踪,因为它不是编译错误,它只是做错了(逻辑可能):

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;
}

getAisleNumbers方法:

    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;
}

connectArrays方法:

    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;


}

}

0 个答案:

没有答案