Kjava local file read/write demo

Source: Internet
Author: User

Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. outputstream;

Import javax. microedition. Io. connector;
Import javax. microedition. Io. file. fileconnection;
Import javax. microedition. lcdui. Command;
Import javax. microedition. lcdui. commandlistener;
Import javax. microedition. lcdui. display;
Import javax. microedition. lcdui. displayable;
Import javax. microedition. lcdui. form;
Import javax. microedition. lcdui. stringitem;
Import javax. microedition. lcdui. textfield;
Import javax. microedition. MIDlet. MIDlet;
Import javax. microedition. MIDlet. midletstatechangeexception;

Public class fileoperations extends MIDlet implements commandlistener {
Form = new form ("fileoperations ");

Stringbuffer sb = new stringbuffer ();

String filepath = "file: // E:/1.txt"; // the path of the file to be read and written, such as Sony Ericsson and Nokia.

// String filepath = "file: // root1/1.txt"; // file path simulator to be read/written

// Stringitem Si = new stringitem (null, "data to be written :");

Stringitem info = new stringitem (null, null );

Textfield TF = new textfield ("data to be written", "test", 100, textfield. Any );

Command exit = new command ("exit", command. Exit, 1 );

Command write = new command ("Write File", command. OK, 1 );

Command READ = new command ("Read File", command. OK, 1 );

Public fileoperations (){

// Form. append (SI );
Form. append (TF );
Form. addcommand (write );
Form. addcommand (read );
Form. addcommand (exit );
Form. setcommandlistener (this );
}

Protected void destroyapp (Boolean arg0 ){

}

Protected void pauseapp (){

}

Protected void Startapp () throws midletstatechangeexception {
Display. getdisplay (this). setcurrent (form );

}

/**
* Add text
*
* @ Param Str
* Text to be added
*/
Private void addstr (string Str ){
SB. append (STR + "/N ");
Info. settext (sb. tostring ());
}

/**
* Write a file
*
* @ Param B
*/
Private void write (byte [] B ){
Addstr ("path" + filepath );
Write (filepath, B );
}

Private void write (string path, byte [] B ){
Addstr ("Write File ");
Fileconnection fc = NULL;
Outputstream OS = NULL;
Try {
Fc = (fileconnection) connector. Open (PATH );

If (! FC. exists () // if the file does not exist
FC. Create (); // create a file
Else
FC. truncate (0); // clear file data

OS = FC. openoutputstream ();
OS. Write (B); // Write File data
OS. Close ();
OS = NULL;
Addstr ("completed file writing ");
FC. Close ();
Fc = NULL;

} Catch (ioexception e ){
E. printstacktrace ();
Addstr ("file writing exception" + E. tostring ());

} Catch (securityexception e ){
E. printstacktrace ();
Addstr ("permission error during file writing ");

} Finally {
If (OS! = NULL ){
Try {
OS. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
OS = NULL;
}

If (FC! = NULL ){
Try {
FC. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
Fc = NULL;
}

}

}

/**
* Read files
*
*/
Private void read (){
Addstr ("path" + filepath );
Read (filepath );
}

Private void read (string path ){
Addstr ("Read File ");
Fileconnection fc = NULL;
Inputstream is = NULL;
Try {
Fc = (fileconnection) connector. Open (PATH );

If (! FC. exists () {// if the file does not exist
Addstr ("file does not exist ");
Return;
}

Is = FC. openinputstream ();
Byte [] B = new byte [(INT) FC. filesize ()];
Is. Read (B); // read the file data
Addstr ("File Content :");
Addstr (new string (B ));
Addstr ("finished reading the file ");
Is. Close ();
Is = NULL;
FC. Close ();
Fc = NULL;

} Catch (ioexception e ){
E. printstacktrace ();
Addstr ("an exception occurred while reading the file" + E. tostring ());

} Catch (securityexception e ){
E. printstacktrace ();
Addstr ("permission error occurred during file reading ");

} Finally {
If (is! = NULL ){
Try {
Is. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
Is = NULL;
}

If (FC! = NULL ){
Try {
FC. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
Fc = NULL;
}
}
}

/**
* Clear information
*
*/
Private void Cls (){
SB. Delete (0, SB. Length ());
Form. deleteall ();
// Form. append (SI );
Form. append (TF );
Form. append (Info );
}

Public void commandaction (command C, displayable arg1 ){
If (C = write) {// write a file
New thread (){
Public void run (){
CLS ();
Write (Tf. getstring (). getbytes ());
}
}. Start ();

} Else if (C = read) {// read the file
New thread (){
Public void run (){
CLS ();
Read ();
}
}. Start ();

} Else if (C = exit) {// exit
Destroyapp (true );
Yydestroyed ();

}

}
}

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/kf156/archive/2009/09/30/4621413.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.