×

Discussion Board

Results 1 to 11 of 11
  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    Red face very simple middlet class to write to write a text file

    hi there,
    my mobile project package address is c:\netbeansprojects\mymidlet\. what piece of code should i write down so it will create and write date to a file in this address d:\somefolder\text.txt.
    please note as it will go on a cell phone how should i obtain drive letter of the memory stick.
    thank a million

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    7

    Re: very simple middlet class to write to write a text file

    I don't know muh about I/O of J2ME, but NetBeans 6.0 has a "PDA demo" application in Mobility examples. It may help you to find an easy way, maybe.

    Good luck.
    JavaTurca

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    37

    Re: very simple middlet class to write to write a text file

    First explore the root directories using the following piece of code,
    Code:
        public Enumeration getRootFoldersList()
        {
            Enumeration roots = FileSystemRegistry.listRoots();
            return roots;
        } // getRootFoldersList
    And
    Then iterate through each directory and show the child directory of it...
    And analyze what the directory structure of your mobile is, using 'List'
    Code:
        public Enumeration getChildList(String path) throws IOException
        {
            path = "file:///" + path;        
            FileConnection conn = (FileConnection) Connector.open(path, Connector.READ);
            Enumeration childList = conn.list();        
            conn.close();
            
            return childList;
        } // getChildList
    Huzefa

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    Wink Re: very simple middlet class to write to write a text file

    hi afezuh_z,
    first thank you vary much for your professional piece of code.
    after i get list of directories, I need to write a file to one of them....for example(assuming c: is for phone internal ,memory) to c:\\solderNo1\report.txt
    I tried to do this using fileoutputstream but it did not work do you know that piece of code?
    thanks

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    Wink Re: very simple middlet class to write to write a text file

    hi eddai,
    thank you very much for your answer, i will check it out.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    37

    Re: very simple middlet class to write to write a text file

    ------------------------------------------------

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    37

    Re: very simple middlet class to write to write a text file

    Code:
            private String filePath; // Absolute File Path
    	public void createFile(String dirPath, fileName) throws IOException{
    
    		String dirPath= null; 
    
    		String pathPrefix = "file:///";
    		filePath= pathPrefix +dirPath + fileName;//"fileName.txt";
    
    		FileConnection fileInfo= (FileConnection)Connector.open(filePath, Connector.READ_WRITE);
                    
    		if(!fileInfo.exists())
    			fileInfo.create();
                    
    	        fileInfo.close();
    	} // createFile
    	
    	public void writeMsg(String str)
    	{
    		OutputStream fos= null;
            	try {                
    			fos= ((FileConnection)Connector.open(filePath)).openOutputStream();		
    			fos.write(str.getBytes());
    			fos.close();
    		} catch (IOException e) {			
    		} // catch
    	} // WriteMsg
    Huzefa

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    Lightbulb Re: very simple middlet class to write to write a text file

    hello,
    this peice of code is enough and it was what i needed:

    public class NewClass {
    private String filePath; // Absolute File Path
    public void createFile(String dirPath, fileName) throws IOException{
    String dirPath= null;

    String pathPrefix = "file:///";
    filePath= pathPrefix +dirPath + fileName;//"fileName.txt";

    FileConnection fileInfo= (FileConnection)Connector.open(filePath, Connector.READ_WRITE);

    if(!fileInfo.exists())
    fileInfo.create();

    fileInfo.close();
    } // createFile
    }
    netbean produce a red cross at beginning of the highlighted lines and says " <identifier> expected.
    ????what should I do?????

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    37

    Re: very simple middlet class to write to write a text file

    public void createFile(String dirPath, String fileName)

    Code:
    
    import java.io.IOException;
    import javax.microedition.io.Connector;
    import javax.microedition.io.file.FileConnection;
    
    public class NewClass {
        private String filePath; // Absolute File Path
        public void createFile(String dirPath, String fileName) throws IOException{
    
            String pathPrefix = "file:///";
            filePath= pathPrefix +dirPath + fileName;//"fileName.txt";
    
            FileConnection fileInfo= (FileConnection)Connector.open(filePath, Connector.READ_WRITE);
    
            if(!fileInfo.exists())
                fileInfo.create();
    
            fileInfo.close();
        } // createFile
    } // class NewClass
    Huzefa

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    Thumbs up Re: very simple middlet class to write to write a text file

    hey man, THAT WORKS,
    thank you alot for this cooperation, im new to this site so if there is a way to vote for others tell me to do that for you.
    thank you again

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    Talking Re: very simple middlet class to write to write a text file

    hello,
    I am stock again mixing these source codes. (
    It seems I should had named my question "a very hard midlet to write a text file ouside the package"
    The first two give compilation error the third one compiles without errors does not creat a sample file like textfile.txt
    Here we have the source code for following methods:

    1.getRootFoldersList();//to obtain cell phone's drive letters
    2.getChildList();// to obtain directory listing
    3.createFile(String dirPath, String fileName); //to write a file

    Now I need a body to put all of the above things inside and be able to call them whenever needed.

    Well, I have created a Mobile Application Project in Netbeans 5.5.1. It contains only one package called "hello" and there exists only one file in the package called "HelloMidlet.java".

    Anyway I try to mix the above source codes and put them in the HelloMidlet.java I fail.

    The first two methods are only to show us how absolute paths in a cell phone look like. We then pass an absolute directory path to the third method so it writes a file to that address. Giving the absolute directory path to the third method could be done manually (presenting the absolute path in the third method's source code).

Similar Threads

  1. [announce] PyUIQ. Python for UIQ 2.1 and 3.x
    By OscarBernabeu in forum Symbian
    Replies: 35
    Last Post: 2008-01-17, 09:12
  2. Replies: 1
    Last Post: 2005-05-26, 14:22
  3. Reading A Text File Woes
    By _nomad_ in forum Symbian
    Replies: 1
    Last Post: 2005-02-02, 10:09
  4. Replies: 0
    Last Post: 2002-07-24, 16:11
  5. Replies: 0
    Last Post: 2002-06-10, 12:24

Posting Permissions

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