Can't read/write text file to and from String Array

Put your problem here if it does not fit any of the other categories.

Can't read/write text file to and from String Array

Postby g33k499 » Fri Dec 25, 2009 11:44 pm

I'm able to read and write from a comma separated file just fine. But, I need to able able to split each line in a text file into an array item.

Does anyone have any sample code for reading and writing a text file line by line, that actually works? I have tried so many sample of code and I can't get any of them to work.

Thanks
g33k499
Freshman
Freshman
 
Posts: 9
Joined: Fri Dec 18, 2009 10:23 pm

Top

Postby g33k499 » Sun Dec 27, 2009 5:24 pm

Well, I tinkered my way through my problem with reading and writing files line by line. But, now I have a strange issue.

Using,

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         int i = 0;
  3.  
  4.         while((read = buf.readLine())!= null){
  5.  
  6.        //Do Something Which each line of data read
  7.  
  8.               arData[i] = read;
  9.  
  10.         }
  11.  
  12.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


I am able to populate array item 0 with the last line in my text file. But, I want to place each line from my file into their own array item. But when I use the following code, the app will crash.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. int i = 0;
  3.  
  4.         while((read = buf.readLine())!= null){
  5.  
  6.        //Do Something Which each line of data read
  7.  
  8.               arData[i] = read;
  9.  
  10.           i++;  //<----- This is the only change.
  11.  
  12.         }
  13.  
  14.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


When I debug, it doesn't crash. It just hangs and never provides me with any information. Any thoughts?
g33k499
Freshman
Freshman
 
Posts: 9
Joined: Fri Dec 18, 2009 10:23 pm

Postby SteffenL » Sat Jan 02, 2010 8:24 pm

Hello,

You're not showing us how you define arData. Are you sure your array is large enough?
If arData is not large enough, your app will naturally crash because of the index i out of bounds.

I've had (actually, still having) a break from Android now but I might need it myself some time, so I'll give it a try myself now.. I'll let you know what I find out. :)

Update
Okay, figured a way to read a text file line by line here.. I hope you can use it. :)
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. BufferedReader reader;
  2.  
  3. try
  4.  
  5. {
  6.  
  7.         reader = new BufferedReader(new FileReader("/sdcard/Text.txt"));
  8.  
  9. }
  10.  
  11. catch (FileNotFoundException e)
  12.  
  13. {
  14.  
  15.         // file was not found
  16.  
  17.         return;
  18.  
  19. }
  20.  
  21.  
  22.  
  23. List<String> lines = new ArrayList<String>();
  24.  
  25. try
  26.  
  27. {
  28.  
  29.         String line;
  30.  
  31.         while ((line = reader.readLine()) != null)
  32.  
  33.         {
  34.  
  35.                 lines.add(line);
  36.  
  37.         }
  38.  
  39. }
  40.  
  41. catch (IOException e)
  42.  
  43. {
  44.  
  45.         // can't read from the file
  46.  
  47.         return;
  48.  
  49. }
  50.  
  51.  
  52.  
  53. for (String line : lines)
  54.  
  55. {
  56.  
  57.         // walk through the list of text lines and do something with them
  58.  
  59.         Toast.makeText(this, line, Toast.LENGTH_SHORT).show();
  60.  
  61. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Now, if you really want to use an array, make sure you initialize it with enough array elements first.
I never really studied Java, so I need to try it out to know exactly how to do it. But it should be something similar to this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String[] arData = new String[<number here>];
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


I'm surprised that nobody answered you sooner. Too busy with New Year celebration maybe. Hehe. :)

Update 2
For writing, you can use BufferedWriter.
Regards,
Steffen
SteffenL
Junior Developer
Junior Developer
 
Posts: 23
Joined: Wed Dec 16, 2009 9:52 am

Postby g33k499 » Mon Jan 04, 2010 3:59 am

I'm sure that is my problem, I am trying to define arData as:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public String[] arData;  //I was hoping that by not adding a number it would be able to programmatically grow is size.
  3.  
  4.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


I'm trying it that way because I need it to be a public variable that I can populate from another .java file by using something like:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.   for (int i = 0; i < manager.arData.length; i++){
  3.  
  4.      if (manager.arData[i] == ""){
  5.  
  6.        manager.arData[i] =  strReadLine.trim();
  7.  
  8.        break;
  9.  
  10.      }                  
  11.  
  12.    }
  13.  
  14.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
g33k499
Freshman
Freshman
 
Posts: 9
Joined: Fri Dec 18, 2009 10:23 pm

Postby SteffenL » Mon Jan 04, 2010 1:05 pm

Arrays do not automatically expand, but Lists do.
If you really just need to add items to a list, using List.add() is much simpler than using an array, if you ask me. :)
Regards,
Steffen
SteffenL
Junior Developer
Junior Developer
 
Posts: 23
Joined: Wed Dec 16, 2009 9:52 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 10 guests