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

public class WinFile {
	public String name;
	
	public WinFile(){
		
	}
	
	public void init(String name){
		this.name = name;
	}
	
	public void save(String s){
		Saver sa = new Saver();
		sa.s = s;
		sa.name = name;
		Thread th = new Thread(sa);
		th.start();
	}
	
	public void read(String varName, Script scr){
		try{
			FileConnection fc = (FileConnection) Connector.open(name);
			DataInputStream dis = fc.openDataInputStream();
			String ret = dis.readUTF();
			returnStringRead(varName, ret, scr);
		} catch (Exception e){}
	}
	
	private void returnStringRead(String varName, String str, Script scr){
		scr.FileReaded(varName, str);
	}
	
	class Saver implements Runnable {
		public String s;
		public String name;
		
		public void run(){
			try{
				FileConnection fco = (FileConnection) Connector.open(name);
				if(!fco.exists()){
					fco.create();
				}
				DataOutputStream dos = fco.openDataOutputStream();
				dos.writeUTF(s);
				dos.close();
				dos = null;
				fco.close();
				fco = null;
			} catch (Exception e){e.printStackTrace();}
		}
		
	}

}
		
	