الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

كيفية التعامل مع الملفات النصية

بدأه computersiences في 10 ديسمبر 2011 · 2 رد · 1,282 مشاهدة · في JavaME
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

اريد أن اقراء ملف نصي لكن بطريقة خاصة وحظ القيم في متغير لكي اتعامل معه لاحقاً، الملف مرفق، وفي هذا الكلاس طريقة حفظ البيانات في الملف

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.location.*;


public class TrackMe extends MIDlet implements CommandListener, LocationListener {

  private Display display;
  private Form form;
  String time;
  public String date;
  public String information;
  WriteMIDlet wm;
  double latitude;
  double longitude;
  double Lat;
  double Lon;
  boolean HadWritten;

  private Command exit = new Command("Exit", Command.EXIT, 1);
  private int sec = 100;
  private StringItem info = new StringItem("Time & Location:","unknown");
  private LocationProvider locationProvider = null;


  public TrackMe(){
    display = Display.getDisplay (this);//this form
    form = new Form("TrackMe");
    form.addCommand(exit);
    form.setCommandListener(this);
    form.append(info);
    latitude = 0;
    longitude = 0;
    HadWritten = false;

    try {
         locationProvider = LocationProvider.getInstance(null);
         locationProvider.setLocationListener(this, sec, -1, -1);

	} catch (Exception e) {
	  exit();
    }
  }


  public void commandAction(Command c, Displayable s) {
    if (c == exit) {
  	  exit();
	}
  }

  public void startApp () {
    display.setCurrent(form);

  }

  public void pauseApp () {}

  public void destroyApp (boolean forced) {}

  public void exit(){
    destroyApp(false);
    notifyDestroyed();
  }

  public void locationUpdated(LocationProvider provider, Location location){
    try {

   if (location != null && location.isValid() ) {

      QualifiedCoordinates qc = location.getQualifiedCoordinates();
      Lat = qc.getLatitude() ;
      Lon =qc.getLongitude() ;

      String str = Double.toString(Lat);
      str = str.substring(0, 8);
      Lat =  Double.parseDouble(str);

      str = Double.toString(Lon);
      str = str.substring(0, 8);
      Lon = Double.parseDouble(str);


      Time tm = new Time (location.getTimestamp());
      time = tm.GetTime();
      date = tm.GetDate();



      if( latitude != Lat && longitude != Lon  )
        {
         wm = new WriteMIDlet() ;

         latitude = Lat ;
         longitude = Lon ;

         if (wm.FileSize () > 0)
            information = "\n" + date + "\t" + time + "\t"  + latitude + "\t" +  longitude ;
         else
            information = date + "\t" + time + "\t"  + latitude + "\t" +  longitude ;

         wm.SaveFile(information);
         HadWritten = false;
        }
     else if (HadWritten == false)
         {
         wm = new WriteMIDlet() ;

         information = "\t&" ;
         wm.SaveFile(information);
         HadWritten = true ;
         }

        info.setText("\n" + date+
        "\n" + time + "\n" +
        "Lat: " + Lat + "\n" +
        "Lon: " + Lon );

    }else
{
      Time tm = new Time ();
      time = tm.GetTime();
      date = tm.GetDate();

      wm.SaveFile("\n" + date + "\t" + time +"*");
//info.setText("Location API failed ");
}
}catch(Exception e)
{
info.setText("Exceptiondd:"+e);
}
  }

كذلك هذا الكلاس

import javax.microedition.io.file.*;
import javax.microedition.io.*;
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class WriteMIDlet{
private String photos = "fileconn.dir.photos";
private String path;
 FileConnection fconn;
 String url ;

public WriteMIDlet(){
path = System.getProperty(photos);
url = path +  "TrackMe.txt";
       try {
            fconn = (FileConnection) Connector.open(url, Connector.READ_WRITE);

        } catch (IOException ex) {
            ex.printStackTrace();
        }
}

    public long FileSize () throws IOException{
             return fconn.fileSize();
    }


public void SaveFile(String information) {
       try {
              byte data[] = information.getBytes();
              if (!fconn.exists()) {
                     fconn.create();
              }
              //append
              OutputStream ops = fconn.openOutputStream(fconn.fileSize());
              ops.write(data);
              ops.flush();
              ops.close();
              fconn.close();
       }
       catch (IOException ioe) {
       System.out.println("IOException: "+ioe.getMessage());
       }
       catch (SecurityException se) {
       System.out.println("Security exception:" + se.getMessage());
       }
}

TrackMe.txt

#2

أخي العزيز نصيحتي لك بدل الملفات النصية تعامل مع ملفات XML فهي مخصصة لتخزين البيانات واسترجاعها وبطريقة سهلة وسلسة تغنيك عن TXT :)

تحتاج لمكتبة خارجية KXML اقرأ الـ Documentation الخاص بها ليست معقدة حتى ان حجمها ليس بالكبير الذي يؤثر على حجم البرنامج

موقع المكتبة :

http://kxml.sourceforge.net/

2

سبحان الله وبحمده ... سبحان الله العظيم

#3

هذا مثال على xml باستخدام حزمة sax من كورس نوكيا

في الملف المرفق تجد مثال مفتوح مع شفرة وملفي pdf يشرحان الكود

مع jad و jar وكل شي جاهز ماعليك سوى فهم بسيط للكود

رابط التحميل :

http://www.4shared.com/file/o6bczRzN/xml.html

1

سبحان الله وبحمده ... سبحان الله العظيم

مواضيع مشابهة