×

Discussion Board

Results 1 to 11 of 11
  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    10

    How to parse txt file?

    I need to parse a txt file to make every line a string in a vector. I tried this:

    Code:
    import java.io.*
    .
    .
    .
    BufferedReader r = new BufferedReader(new FileReader(file /*String*/));
    but it doesn't work.

    error: cannot resolve symbol class FileReader and BufferedReader

    Are those classes unknown to j2me

    If they are, why are they mentioned here: http://wwwhome.cs.utwente.nl/~vriesa...io/Reader.html

    ?

    Thanks, Manuel

  2. #2
    Regular Contributor
    Join Date
    Aug 2003
    Location
    uk
    Posts
    232
    http://j2medevices.com/documentation/midp1.0/

    Its not working because its not part of midp1.0.

  3. #3
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    "J2ME" is a big term that actually includes a range of technologies for different types of devices.

    "The Java 2 Platform, Micro Edition, Foundation Profile is a J2ME Connected Device Configuration (CDC) profile."

    That's from the documentation you linked to.

    The phones run the Mobile Information Device Profile (MIDP) which is a Connected Limited Device Configuration (CLDC) profile. I haven't seen any devices that implement the Foundation Profile.

    shmoove

  4. #4
    Regular Contributor
    Join Date
    Aug 2003
    Location
    uk
    Posts
    232
    Yes but I bet he is compiling against midp1.0 libraries :P

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    10
    Hmmm, is there any HowTo, showig how lines of a txt file are written into strings?

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    10
    Originally posted by alex_crowther
    Yes but I bet he is compiling against midp1.0 libraries :P
    According to the properties of my (Netbeans 4) Project, I'm workig with MIDP-2.0 device profile.

  7. #7
    Regular Contributor
    Join Date
    Aug 2003
    Location
    uk
    Posts
    232
    Close enough, when it comes to file handling, midp-2.0 has not added anything useful that I can recall.

    To get you started heres a method to load a file into memory, for simplicity I have stripped it of error checking:

    Code:
    // --------------------------------------------------------------------- //
    public byte[] LoadFile(String fileName)
    {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        
        try
        {
            InputStream in = this.getClass().getResourceAsStream(fileName);
            
            byte aoBuffer[] = new byte[512];
    
            int nBytesRead,t=0;
            
            while ( (nBytesRead = in.read(aoBuffer)) > 0 ) 
            {
                t += nBytesRead;
                out.write(aoBuffer, 0, nBytesRead);
            }
    
            in.close();
        }
        catch(Exception e){}
    
        return out.toByteArray();
    }
    // --------------------------------------------------------------------- //
    There are numerous ways you can do this, but no one easy one.

    IBear in mind, the more flexible your text file is, the more complex your code will have to be to parse it (and the large your code will be).

    My advice would be convert it into a "char" array and then find the lines manually by looking for '\n' and convert these back into strings.

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    10
    Thank you!

    herw is my solution for parsing a vector with the lines form the txt


    Code:
    package src;
    
    import java.io.*;
    import java.util.*;
    import javax.microedition.io.*;
    
    public class MMParser {
        private String datei = null;
            
        MMParser(String s){
            datei = s;
        }
        
        public Vector readTxt() {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Vector lines = new Vector(); 
        
        try
        {
            InputStream in = this.getClass().getResourceAsStream(datei);
            
            byte aoBuffer[] = new byte[512];
    
            int nBytesRead,t=0;
    
            while ( (nBytesRead = in.read(aoBuffer)) > 0 ) 
            {
                t += nBytesRead;
                out.write(aoBuffer, 0, nBytesRead);
                
                lines = splitText(out.toString());  
            }
    
            in.close();
        }
        catch(Exception e){}
    
        
    
        return lines;
        
        }
        
        private Vector splitText(String s){
            Vector v= new Vector();
            String x=new String();
        
            for(int i=0; i<s.length();i++){
                if(s.charAt(i)=='#'){ //indicates comment from # to end of line
                    while(s.charAt(i)!='\n'){
                        i++;
                    }
                }
    
                if (s.charAt(i)=='\n'){
                    v.addElement(x.trim());
                    x = new String();
                }
                else
                    x+=s.charAt(i);  
            }
    
        return v;
        }
    }

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    5
    Manuel,

    It is not really complicated. I use Sun WTK to develop J2ME. So, rirst, the string file should be in the res directory so that it is properly loaded into the JAR archive. Works with MIDP 1.0.

    Code to read the whole text file into memory.

    // determine the size of the file
    int dsize = 0;
    InputStream is = getClass().getResourceAsStream("/string.txt");
    byte b = (byte)is.read();
    while (b != -1) {
    dsize ++;
    b = (byte)is.read();
    }
    is.close();

    // create byte array to hold the file contents
    byte buf[] = new byte[dsize];

    // read the contents into the byte array
    is = getClass().getResourceAsStream("/string.txt");
    is.read(buf);
    is.close();

    // convert the array into string
    mytext = new String(buf);

    When it comes to splitting the string, we dont have string tokenizer in J2ME so you have to invent one yourself. It is also not complicated because the string api has the methods to find certain characters (separator characters e.g. newline '\n') and then split based on that.

    int index4 = 0, index5 = 0;
    while (index5 != -1) {
    index5 = mystring.indexOf('\n', index4);

    // add the last line to vector
    if (index5 == -1) {
    thisLine = mystring.substring(index4);

    // add to vector
    myVector.add(thisLine);
    break;
    }

    // add every line to vector
    thisLine = mystring.substring(index4, index5);
    myVector.add(thisLine);

    index4 = index5 + 1;
    }

    This has worked for me perfectly with a converted 2 MB access catalogue database on Nokia 9500.

    Jonas

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    10
    Hey Jonas,

    the code I submitted works too.

    I just posted it, so other can use it, when searching this board for a textparser solution. Now they'll find two!

    Manuel

  11. #11
    Registered User
    Join Date
    Jun 2008
    Posts
    2

    Re: How to parse txt file?

    I used some different classes. Here goes the code:

    Code:
    InputStream inputStream = this.getClass().getResourceAsStream("somefile.txt");
    
    try {
    
    	InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");//my file was utf8
    	StringBuffer stringBuffer = new StringBuffer();
    	Vector lines = new Vector();
    
    	int c;
    	c = inputStreamReader.read();
    	while(c != -1){
    		stringBuffer.append((char)c);
    		c = inputStreamReader.read();
    		//looks for the end of the line
    		if (c == '\n' || c == -1) {
    			String line = stringBuffer.toString();
    				lines.addElement(line);
    			}
    			// clean buffer
    			stringBuffer.setLength(0);
    		}
    	}
    
    } catch (UnsupportedEncodingException e) {
    	e.printStackTrace();
    } catch (IOException e) {
    	e.printStackTrace();
    } finally {
    	try {
    		inputStream.close();
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×