• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps File IO issues

jaychu

Lurker
I've been developping an app and I need to save internal data.
This has been quite a hit and miss adventure and I'm finally getting frustrated.

Here is what I have;

Code:
    private void ChkFile() {
    
    file = getBaseContext().getFileStreamPath(filename);
    
    String text;
    if(!file.exists())
    {
        text = "Welcome to the Application";
        try {
             FileWriter    DataFile = new FileWriter(filename);
            PrintWriter out = new PrintWriter(DataFile);
            out.print("Add New Entry...\n");
            out.close();
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
        toast.show();
    }
    else
    {
        text = "Welcome back!";
        Toast toast = Toast.makeText(getApplicationContext(), file.getPath(), Toast.LENGTH_SHORT);
        toast.show();
    }
        
    }

I do not understand why it seems like the file is being saved temporarily. I also don't think it's writing properly because it's being read elsewhere here:
Code:
 public String [] getData() 
    {    
        String path = Menu.file.getPath();
        String[] data = new String[GetN(path)];
        try
        {
        BufferedReader content = new BufferedReader(new FileReader(path));
            for(int i = 0;content.ready();i++)
                data[i]=content.readLine();

        }
        catch (IOException e){e.printStackTrace();}
        catch (Exception e) {e.printStackTrace();}
        
        return data;
    }
    private int GetN(String file)
    {
        int counter = 0;
        try
        {
            BufferedReader test = new BufferedReader(new FileReader(file));
            while(test.readLine()!=null)
                counter++;
        }
        catch(IOException ex)
        {
            System.out.println("FILE NOT FOUND");
        }
        return counter;
    
    }

Thanks for your help.
I've been trying to solve this for the past week.
 
Understand your frustration, weeks quite a bit to struggle. But please do post complete questions, like a little background would be nice or what the code is doing or is intended to do etc.

what makes you think it is temporary save?
second what makes you think write is not correct? Did you inspect the return or data in getData() in debugger or via log message.
 
Understand your frustration, weeks quite a bit to struggle. But please do post complete questions, like a little background would be nice or what the code is doing or is intended to do etc.

what makes you think it is temporary save?
second what makes you think write is not correct? Did you inspect the return or data in getData() in debugger or via log message.


Yeah sorry, this was early in the morning and I was just really frustrated so I wanted to post a bunch of my code to see if it looked wrong.

What I'm trying to do is have a menu that checks if a text file exists or not. If it does, then leave it. Else, create a new file with the words "Add New Entry" as the first line.

Then, when the user clicks a button, it takes them to a ListActivity which takes that textfile and reads it as a string array. Each line = Element.
I know for sure that getData and getN are working, as I've tested them on non-android based applications. What ends up happening is when I click to the list activity, null is returned. Nothing exists there. GetN is returning 0.

There are at some points when this program did work; but I attempted to optimize it and make it a bit more easier on the eyes and then it returned null again. The things I changed were all in the first activity and not in getN or getData methods.

I also believe that it's saving temporarily or perhaps, creating files non-stop and I just can't seem to find the directory properly, is because when I re-run the same program on several occasions, it always displays the first message rather than the second. = S

Any suggestions?
 
Hey Sorry, I manged to figure it out.

Here is the fix.
I forgot to actually create a file and address filewriter to the path.
Which might explains why it created null.

For anyone who wants to see the fix, here.

Code:
private void ChkFile() {
    
    file = getBaseContext().getFileStreamPath(filename);
    
    String text;
    if(!file.isFile())
    {
        text = "Welcome to the Application";
        try {
            file.createNewFile();
            FileWriter    DataFile = new FileWriter(file.getPath());
            PrintWriter out = new PrintWriter(DataFile);
            out.print("Add New Entry...\n");
            out.close();
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
        toast.show();
    }
    else
    {
        text = "Welcome back!";
        Toast toast = Toast.makeText(getApplicationContext(), file.getPath(), Toast.LENGTH_SHORT);
        toast.show();
    }
        
    }
 
Back
Top Bottom