1. Download our Official Android App: Forums for Android!

Apps File IO issues

Discussion in 'Android Development' started by jaychu, May 2, 2012.

  1. jaychu

    jaychu Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    May 2, 2012

    May 2, 2012
    3
    0
    5
    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 (Text):
    1.     private void ChkFile() {
    2.    
    3.     file = getBaseContext().getFileStreamPath(filename);
    4.    
    5.     String text;
    6.     if(!file.exists())
    7.     {
    8.         text = "Welcome to the Application";
    9.         try {
    10.              FileWriter    DataFile = new FileWriter(filename);
    11.             PrintWriter out = new PrintWriter(DataFile);
    12.             out.print("Add New Entry...\n");
    13.             out.close();
    14.            
    15.         } catch (FileNotFoundException e) {
    16.             // TODO Auto-generated catch block
    17.             e.printStackTrace();
    18.         } catch (IOException e) {
    19.             // TODO Auto-generated catch block
    20.             e.printStackTrace();
    21.         }
    22.         Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
    23.         toast.show();
    24.     }
    25.     else
    26.     {
    27.         text = "Welcome back!";
    28.         Toast toast = Toast.makeText(getApplicationContext(), file.getPath(), Toast.LENGTH_SHORT);
    29.         toast.show();
    30.     }
    31.        
    32.     }
    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 (Text):
    1.  
    2.  public String [] getData()
    3.     {    
    4.         String path = Menu.file.getPath();
    5.         String[] data = new String[GetN(path)];
    6.         try
    7.         {
    8.         BufferedReader content = new BufferedReader(new FileReader(path));
    9.             for(int i = 0;content.ready();i++)
    10.                 data[i]=content.readLine();
    11.  
    12.         }
    13.         catch (IOException e){e.printStackTrace();}
    14.         catch (Exception e) {e.printStackTrace();}
    15.        
    16.         return data;
    17.     }
    18.     private int GetN(String file)
    19.     {
    20.         int counter = 0;
    21.         try
    22.         {
    23.             BufferedReader test = new BufferedReader(new FileReader(file));
    24.             while(test.readLine()!=null)
    25.                 counter++;
    26.         }
    27.         catch(IOException ex)
    28.         {
    29.             System.out.println("FILE NOT FOUND");
    30.         }
    31.         return counter;
    32.    
    33.     }
    Thanks for your help.
    I've been trying to solve this for the past week.
     

    Advertisement

  2. wubbzy

    wubbzy Well-Known Member
    Rank:
    None
    Points:
    63
    Posts:
    109
    Joined:
    Dec 19, 2011

    Dec 19, 2011
    109
    32
    63
    *nix kernel's by day
    Silicon Valley
    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.
     
  3. jaychu

    jaychu Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    May 2, 2012

    May 2, 2012
    3
    0
    5


    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?
     
  4. jaychu

    jaychu Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    May 2, 2012

    May 2, 2012
    3
    0
    5
    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 (Text):
    1. private void ChkFile() {
    2.    
    3.     file = getBaseContext().getFileStreamPath(filename);
    4.    
    5.     String text;
    6.     if(!file.isFile())
    7.     {
    8.         text = "Welcome to the Application";
    9.         try {
    10.             file.createNewFile();
    11.             FileWriter    DataFile = new FileWriter(file.getPath());
    12.             PrintWriter out = new PrintWriter(DataFile);
    13.             out.print("Add New Entry...\n");
    14.             out.close();
    15.            
    16.         } catch (FileNotFoundException e) {
    17.             // TODO Auto-generated catch block
    18.             e.printStackTrace();
    19.         } catch (IOException e) {
    20.             // TODO Auto-generated catch block
    21.             e.printStackTrace();
    22.         }
    23.         Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
    24.         toast.show();
    25.     }
    26.     else
    27.     {
    28.         text = "Welcome back!";
    29.         Toast toast = Toast.makeText(getApplicationContext(), file.getPath(), Toast.LENGTH_SHORT);
    30.         toast.show();
    31.     }
    32.        
    33.     }
    34.  
     
  5. wubbzy

    wubbzy Well-Known Member
    Rank:
    None
    Points:
    63
    Posts:
    109
    Joined:
    Dec 19, 2011

    Dec 19, 2011
    109
    32
    63
    *nix kernel's by day
    Silicon Valley
    awesome...nothing like solving your own problem.
     

Share This Page

Loading...