Hiper
Hiper

Reputation: 3

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 OutputStreamWriter(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;
}

Upvotes: 0

Views: 1064

Answers (1)

EboMike
EboMike

Reputation: 77752

  1. You don't compare string using the equals (==) operator in Java. They're objects. You need to use equals().
  2. System.out.println doesn't work on Android. Use Log.

Upvotes: 2

Related Questions