Home of www.RoboHobby.com Java robotics project www.CubesAndCrosses.com
Train your logic skills in geometry
Create and delete one-color crosses
Play for free right in your browser!
Home-made CNC machine
Our new project -
How to make home-made CNC router
PICkit2 - Tutorials, Experiments
PICkit 2 - Small size USB programmer
from Microchip.
How to use it as a demo device for
USB programming in C language




www.RoboHobby.com
Home of www.RoboHobby.com
Java robotics project


Portable Notebook Table
How to make Portable Notebook Table.
Do it yourself!

Java SE SWING JSR-296 Example
How to run background task in
JSR-296 SWING application
with 'busy animation' in the status bar.
Useful addition to Netbeans template.

Java ME Code Examples
Java ME Source code examples
for robotics projects


Run Java ME Code Examples
How to run our Java ME code examples
for robotics projects



www.RoboHobby.com
/*
 * J2ME code example. Saving Image on Local Disc Utils.
 * Part of www.RoboHobby.com Java robotics project.
 * www.RoboHobby.com
 */


package com.robohobby.me;

import java.util.*;
import javax.microedition.io.*;
import java.io.*;
import javax.microedition.io.file.*;


/**
 * @Ads.Taglet
 * DiscUtils
 * @author Oleg Lyubchenko

 */
public class DiscUtils {


    //byte[] imageByte;

    String appName = "Disc";

    CallBacks callBacks = CallBacks.getInstance();


    //Possible combinations:
    //"file:///CFCard/newfile.txt"

    //"file:///SDCard/newfileZZZ.txt"
    //"file:///0:/Applications/RoboHobbyJ2ME/img"+count+".jpg"
    //"file:///0:/Applications/RoboHobbyJ2ME/" + fileName

    //////2007-05-20 C75:
    //String filePath = "file:///0:/Data/Java/jam/Applications";
    //String filePath = "0:/Java/jam/Applications/";

    //Siemens S75:

    //RAM:
    //String filePath = "file:///0:/Pictures/DiscData/";
    //String filePath = "file:///0:/Data/Java/jam/Applications/Disc/";
    //String filePath = "file:///0:/DiscData/";


    //Siemens S75 RS MMC Card:
    String filePath = "file:///4:/Pictures/DiscData/";

    public DiscUtils()
    {}

    public void writeToDisk( String fileName, byte[] byteData )
    {
        try
        {
            FileConnection fconn = (FileConnection)Connector.open(

                    filePath + fileName
                    );

            // If no exception is thrown, then the URI is valid,

            // but the file may or may not exist.
            if (!fconn.exists())
                fconn.create();  // create the file if it doesn't exist

            java.io.DataOutputStream dOut = fconn.openDataOutputStream();

            dOut.write( byteData, 0, byteData.length );
            dOut.flush();
            dOut.close();

            callBacks.robotMIDlet.getInfoStringItem().setText( "writeToDisk() OK" );
            fconn.close();
        }
        catch ( IOException ioe )
        {
            callBacks.robotMIDlet.getInfoStringItem().setText( "Error ioe="+ioe );
        }

    }

   public void getRoots()
   {
      Enumeration drives = FileSystemRegistry.listRoots();

      String allRoots = "";

      int i = 0;
      while(drives.hasMoreElements())
      {
         String root = (String) drives.nextElement();
         allRoots += "#"+i+"="+root+"\r\n";
      }

      callBacks.robotMIDlet.getInfoStringItem().setText( allRoots );

   }


  /**

   * Checks - is there file with command on the disc

   */
  public boolean isDiskCommand()
  {
      boolean retValue = false;

        try
        {
            FileConnection fconn = (FileConnection)Connector.open(
                    filePath
                    );


            String listOfFiles = "";
            Enumeration en = fconn.list();

            while ( en.hasMoreElements() )
            {
                String fileName = (String)en.nextElement();
                listOfFiles += fileName + ",";
            }

            if ( listOfFiles.indexOf( "cmd.txt" ) >= 0 )
            {
                retValue = true ;
            }
            else

            {
                retValue = false ;
            }
            fconn.close();
        }
        catch ( IOException ioe )
        {
//            callBacks.robotMIDlet.getStateStringItem().setText( "isDiskCommand() Error ioe="+ioe );
        }

      return retValue;
  }

  /**

   * Reads file with command.
   * Returns the text of the command.

   */
  public String readDiskCommand()
  {
      String retValue = "";

        try
        {
            FileConnection fconn = (FileConnection)Connector.open(
                    this.filePath + "cmd.txt"
                    );

                java.io.DataInputStream dIn = fconn.openDataInputStream();

                byte b[] = new byte[20];

                if ( dIn.read(b) != -1 )
                {
                    int cmd = b[0];
                    //int period = b[1] - 48; //48 should be removed in final version.

                    //int period = b[1];

                    if ( cmd == 'L' )
                    {
                        //cameraForm.messageItem.setText( "readDiskCommand() cmd=L period="+period );
                        //cameraForm.useCommand( "L", period );

//                        callBacks.robotMIDlet.getStateStringItem().setText( "L" );
                        retValue = "L";
                    }
                    else if ( cmd == 'R' )
                    {
                        //cameraForm.messageItem.setText( "readDiskCommand() cmd=R period="+period );

                        //cameraForm.useCommand( "R", period );
//                        callBacks.robotMIDlet.getStateStringItem().setText( "R" );
                        retValue = "R";
                    }
                    else if ( cmd == 'F' )
                    {
                        //cameraForm.messageItem.setText( "readDiskCommand() cmd=F period="+period );

                        //cameraForm.useCommand( "F", period );
//                        callBacks.robotMIDlet.getStateStringItem().setText( "F" );
                        retValue = "F";
                    }
                    else if ( cmd == 'B' )
                    {
                        //cameraForm.messageItem.setText( "readDiskCommand() cmd=B period="+period );

                        //cameraForm.useCommand( "B", period );
//                        callBacks.robotMIDlet.getStateStringItem().setText( "B" );
                        retValue = "B";
                    }

                }

                dIn.close();

            fconn.close();
        }
        catch ( IOException ioe )
        {
            retValue = "Error ioe=" + ioe;
        }
        catch ( Exception e )
        {
            retValue = "Error e=" + e;
        }

      return retValue;
  }

  /**

   * Delets file with command from disk.
   */

  public void deleteDiskCommand()
  {
      boolean isDeleted = false;

      int i = 0;
      while ( !isDeleted && i < 20 )
      {

        try

        {
            FileConnection fconn = (FileConnection)Connector.open(
                    this.filePath + "cmd.txt"
                    );

            if ( fconn.exists() )
            {
                fconn.delete();
                isDeleted = true;
                //cameraForm.messageItem.setText( "deleteDiskCommand() cmd.txt DELETED. i=" + i );

            }
            else
            {
                //Nothing to delete - no such a file
            }

            try
            {
                fconn.close();
            }
            catch ( Exception e )
            {
                //cameraForm.messageItem.setText( "deleteDiskCommand() fconn.close() Exception e="+e );

            }

        }
        catch ( IOException ioe )
        {
            //cameraForm.messageItem.setText( "deleteDiskCommand() Exception ioe="+ioe );
        }

        i++;
      }

  }

}








Google
 



Contact us:


My Skype name is oleg_skp
Start Skype text chatStart Skype text chat
View Skype profileView Skype profile
Start Skype voice callStart Skype voice call
Add me to your Skype contact listAdd to Skype contact list
Leave voicemail using SkypeLeave voicemail using Skype
Send a file using SkypeSend a file using Skype
Note: To use these links
the Skype program should
be installed on your computer.
You can load it for free from
their official site:
Contact via Skypewww.Skype.com
------------------