Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Guyz, please help me with my project..its all about file handling in java-J2me .I've got this problem..At first I can read the data inside the .txt in my src as my path..I have this code...

Java
private String file() {

        InputStream is = getClass().getResourceAsStream("file.txt");
        StringBuffer sb = new StringBuffer();
        //String readline;

        try {

            int chars, i = 0;
            while ((chars = is.read()) != -1) {
                sb.append((char) chars);

            }

            return sb.toString();


        } catch (Exception e) {
        }

        return null;
    }







now, i'm having a problem in writing txt coming from the txtfield in java-j2me to .txt file.. ..

dont know how to implement with the writing so i have this code..it can determine f the .txt file exist but errors occured when i will try to run it again..

i tried to put if( fc.exist) ---}else { fc.create but it doesn't work :(..


Java
public void writeFile() {
        try {
            OutputConnection con = (OutputConnection) Connector.open("file:///root1/myfile.txt", Connector.WRITE);

            FileConnection fc = (FileConnection)con;
            fc.create();

            OutputStream out = con.openOutputStream();
            PrintStream ps = new PrintStream(out);
            ps.println("Line1");
            ps.close();
            con.close();
            Alert alert = new Alert("Success", "Data Written successfully", null, null);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        } catch (ConnectionNotFoundException error) {
            System.out.println(error.getMessage());
        } catch (IOException error) {
            Alert alert = new Alert("Error", error.toString(), null, null);
            alert.setTimeout(Alert.FOREVER);
            alert.setType(AlertType.ERROR);
            display.setCurrent(alert);
        }
    }




pleass help me...


thanks ahead!! Godbless..
Posted
Updated 10-Feb-12 19:48pm
v4

1 solution

if( fc.exist) then fc.create

Unless Java is insane, this is backwards. If it DOESN'T exist, you want to create it.

Surely there's a way to read text files in java that's not one character at a time ?

According to google, this[^] tells you how to write files with J2ME
 
Share this answer
 
v2
Comments
jhencer111108 10-Feb-12 17:00pm    
hmmm i mean

}else{

fc.creat(); :(

yes there's way but i'm having trouble because it's J2me ..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900