Android Write / Read Text
Hey guys, I have this code for an android application, the funny thing is that data is never equal to datas, can you guys explain me why, and if in case give me new read / write file functions :/ , thanks
String datas = "volume of";
writesettings(datas);
String data = readsettings();
String data2 = "volume of";
if (data == datas) {
System.out.println("success");
}
System.out.println(data);
System.out.println(data.length());
System.out.println(datas.length());
}
// Write Settings
public void writesettings(String data){
try {
FileOutputStream fOut = openFileOutput("settings.dat", MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWrit开发者_如何学JAVAer(fOut);
osw.write(data);
osw.flush();
osw.close();
}catch(Exception e){
e.printStackTrace(System.err);
}
}
public String readsettings(){
try {
FileInputStream fIn = openFileInput("settings.dat");
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[9];
isr.read(inputBuffer);
String readString = new String(inputBuffer);
datax = readString;
isr.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return datax;
}
- You don't compare string using the equals (==) operator in Java. They're objects. You need to use
equals()
. System.out.println
doesn't work on Android. UseLog
.
精彩评论