Baca Tulis File

Tulis File

public boolean tulisFile(String txt){
try {
FileConnection fconn = (FileConnection)Connector.open("file:///SDCard/data.conf",Connector.READ_WRITE);
if (!fconn.exists())
fconn.create();  // create the file if it doesn't exist
OutputStream os =fconn.openOutputStream();
os.write(txt.getBytes());
os.close();
fconn.close();
return true;
}
catch (IOException ioe) {
return false;
}
}

baca file

public String bacaFile(){
try {
InputStream inputStream = null;
String resultString = null;
FileConnection fileConnection = (FileConnection) Connector.open( "file:///SDCard/data.conf");
if (fileConnection.exists()) {
inputStream = fileConnection.openInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
while (true) {
int bytesRead = inputStream.read( buffer, 0, 1024 );
if (bytesRead == -1)
break;
byteArrayOutputStream.write( buffer, 0, bytesRead );
}
byteArrayOutputStream.flush();
byte[] result = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
resultString = new String(result);
return resultString;
}else{
fileConnection.create();
return null;
}

}
catch (IOException ioe) {
System.out.println(ioe.toString());
return null;
}
}

untuk import nya silahkan cari sendiri 😀
kalo pake eclipse akan mudah menemukannya

About iBNuX
Single fighter Developer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: