I am trying to write a file prgram that works execpt reading part.
Although it works, it's structure is very complex and unsystematic.
Please give me suggestions how to correct that if you don't mind.
I am just a beginner and luck experiecne of writing programs.

In the program, I used static variables.
As far as I know, if i use static, the class name must be used before the static variable.
But when I use class name, it gives error.
When I use just the reference of the instance of the class, it does not give error. But it give the information that
"The sattic field ***** should be accessed in a static way". How should I do that?

in the program, if the user choose get file content, the program will show all the files and folders of the folder he choose.
if the user choose back, it will go back the path u have choosen.

Code:
package tfilereadwrite;

public class TFileReadWriteMIDlet extends MIDlet {
	static TFileReadWriteMIDlet tfilereadwritemidlet;
	Display display;
	Mainform mainform=new Mainform("Reading&Writing Files");

	public TFileReadWriteMIDlet() {
		TFileReadWriteMIDlet .tfilereadwritemidlet=this;
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void startApp() throws MIDletStateChangeException {
		display=Display.getDisplay(this);		
		display.setCurrent(mainform);
		
	}

}
Code:
package tfilereadwrite;

public class Mainform extends Form  {
	Command cmdbrowse;
	Command cmdback;
	Command cmdgetcontent;
	Command cmdread;
	Command cmdwrite;
	TextField txtfield;
	TextField txtpath;
	ChoiceGroup objlist;
	FileThread filethread=null;	

	public Mainform(String title) {
		super(title);
		cmdbrowse=new Command("browse",Command.SCREEN,0);
		cmdback=new Command("back", Command.SCREEN,0);
		cmdgetcontent=new Command("get file content",Command.SCREEN,1);
		txtfield=new TextField("","",100,TextField.ANY);
		txtpath=new TextField("Path","",100,TextField.ANY);
		objlist=new ChoiceGroup("Contents",ChoiceGroup.EXCLUSIVE);
		cmdread=new Command("read",Command.SCREEN,1);
		cmdwrite=new Command("write",Command.SCREEN,1);		
		addCommand(cmdbrowse);		
		append(txtfield);
		append(txtpath);
		append(objlist);
		setCommandListener(new mainformcommandlistener());
	}

	public Mainform(String title, Item[] items) {
		super(title, items);
		// TODO Auto-generated constructor stub
	}	

}
Code:
package tfilereadwrite;
/////////////////mainformcommandlistener is for command "browse"
////////////////FileCommandHandler is for command back and getcontent
public class mainformcommandlistener implements CommandListener {

	public void commandAction(Command c, Displayable d) {
		
		Mainform mainform=(Mainform)d;
		if(c==mainform.cmdbrowse)
		{			
			mainform.removeCommand(mainform.cmdbrowse);
			
			mainform.addCommand(mainform.cmdback);
			mainform.addCommand(mainform.cmdgetcontent);
			mainform.addCommand(mainform.cmdread);
			mainform.addCommand(mainform.cmdwrite);
			
			
			mainform.setCommandListener(new FileCommandHandler());///for cmdback and cmdgetcontent
			mainform.filethread=new FileThread();
			Thread thread=new Thread(mainform.filethread);
			thread.start();
						
		}

	}

}
Code:
package tfilereadwrite;
public class FileCommandHandler implements CommandListener {
	
	public FileCommandHandler() {
		// TODO Auto-generated constructor stub
	}

	public void commandAction(Command c, Displayable d) {
		Mainform mainform=(Mainform)d;		
				
		if(c==mainform.cmdback)
		{
			mainform.filethread.back=1;
			//mainform.filethread.path=mainform.filethread.prevpath;
			mainform.filethread=new FileThread();
			Thread thread=new Thread(mainform.filethread);
			thread.start();
		}
		
		if(c==mainform.cmdgetcontent)
		{
			//mainform.objlist.deleteAll();
			int index=mainform.objlist.getSelectedIndex();
			String filedir=mainform.objlist.getString(index);
			if(!filedir.endsWith("/"))  ////////////if it is not a dir, cannot show the content of dir
			{
				Alert alert=new Alert("Please choose a directory");
				TFileReadWriteMIDlet.tfilereadwritemidlet.display.setCurrent(alert);
			}
			else
			{
			mainform.filethread=new FileThread(filedir);
			Thread thread=new Thread(mainform.filethread);
			thread.start();
			}
		}//if(c==mainform.cmdgetcontent)
		
		if(c==mainform.cmdread)
		{
			int index=mainform.objlist.getSelectedIndex();
			String filedir=mainform.objlist.getString(index);
			
			if(filedir.endsWith("/"))
			{
				Alert alert=new Alert("Please choose a file to read");
				TFileReadWriteMIDlet.tfilereadwritemidlet.display.setCurrent(alert);
			}
			else
			{
				mainform.filethread.readwrite=1;
				mainform.filethread=new FileThread(filedir);
				Thread thread=new Thread(mainform.filethread);
				thread.start();
			}
		}

	}

}
Code:
package tfilereadwrite;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;

public class FileThread implements Runnable {
     FileConnection fileconnection;
     DataInputStream datainputstream;
     DataOutputStream dataoutputstream;
     //FileInputStream fin;
	String filedir=null;
	static int readwrite=0;
	static String path=null;
	static String prevpath=null;
	static int back=0;
	static int read=0;

	public FileThread() {
		/*Enumeration enumeration=FileSystemRegistry.listRoots();
		while(enumeration.hasMoreElements())
		{
			TFileReadWriteMIDlet.tfilereadwritemidlet.mainform.objlist.append(enumeration.nextElement().toString(), null);
		}*/
		//filedir=null;
		//path=null;
		//prevpath=null;
	}

	public FileThread(String filedir) {
		this.filedir=filedir;
	}

	public void run() {
		if(readwrite!=0)
		{
			if(readwrite==1)read();return;
		}
		
		TFileReadWriteMIDlet.tfilereadwritemidlet.mainform.objlist.deleteAll();
		
		System.out.println("********"+filedir);
		if(filedir==null)
		{
			if(back==0)
			{
			Enumeration enumeration=FileSystemRegistry.listRoots();
			while(enumeration.hasMoreElements())
			{
				TFileReadWriteMIDlet.tfilereadwritemidlet.mainform.objlist.append(enumeration.nextElement().toString(), null);
			}
			}//if(back==0)
			
			else
			{
				
					try {
						
						fileconnection=(FileConnection)Connector.open(path);
						prevpath=fileconnection.getPath();
						fileconnection=(FileConnection)Connector.open("file://"+prevpath,Connector.READ_WRITE);
						path=fileconnection.getURL();
						Enumeration enumeration=fileconnection.list();
						while(enumeration.hasMoreElements())
						{
							System.out.println("*******");
							TFileReadWriteMIDlet.tfilereadwritemidlet.mainform.objlist.append(enumeration.nextElement().toString(),null);
						}
						
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}		
					//} catch (IOException e) {
						// TODO Auto-generated catch block
						//e.printStackTrace();
					//}
					
					
				
			}
		}//if(filedir==null)
		
		else
		{
			try {
				
					
				if(path==null)
				{
				fileconnection=(FileConnection) Connector.open("file:///"+filedir, Connector.READ_WRITE);
				path=fileconnection.getURL();
				System.out.println("Inside if path==null"+path);
				}
				else
				{
					
					fileconnection=(FileConnection) Connector.open(path+filedir, Connector.READ_WRITE);
					//prevpath=fileconnection.getPath();
					//System.out.println("get path"+prevpath);
					//System.out.println("file://"+prevpath+filedir);
					//fileconnection.setFileConnection("file://"+prevpath+filedir);
					prevpath=path;
					path=fileconnection.getURL();
				}
				
				System.out.println("path="+path);
				Enumeration enumeration=fileconnection.list();
				while(enumeration.hasMoreElements())
				{
					System.out.println("*******");
					TFileReadWriteMIDlet.tfilereadwritemidlet.mainform.objlist.append(enumeration.nextElement().toString(),null);
				}
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}//else
		
		TFileReadWriteMIDlet.tfilereadwritemidlet.mainform.txtpath.setString(path);/////to show the current path
		back=0;
	}

	private void read() {
		System.out.println("read");
		try {
			
			
			fileconnection=(FileConnection)Connector.open(path+filedir);
			InputStream is=fileconnection.openInputStream();
			ByteArrayOutputStream baos=new ByteArrayOutputStream();
			byte[]buffer=new byte[1024];
			int bytesRead=0;
			while((bytesRead=is.read(buffer))!=-1)
			{
				baos.write(buffer,0,bytesRead);
				TFileReadWriteMIDlet.tfilereadwritemidlet.mainform.txtfield.setString(baos.toString());
			}
			
			//TFileReadWriteMIDlet.tfilereadwritemidlet.mainform.txtfield.setString(baos.toString());
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		readwrite=0;
		
		
	}

}