# Thread: very simple middlet class to write to write a text file

1. ## 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. ## 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.

3. ## 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

4. ## 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. ## 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. ## Re: very simple middlet class to write to write a text file

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

7. ## 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";

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

8. ## 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";

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. ## 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";

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

fileInfo.close();
} // createFile
} // class NewClass

10. ## 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. ## 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).

#### Posting Permissions

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