Android写/读文本

时间:2010-11-05 02:10:28

标签: android

嘿伙计们,我有一个Android应用程序的代码,有趣的是数据永远不会等于数据,你们可以解释我为什么,如果以防万一给我新的读/写文件功能:/,谢谢

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

1 个答案:

答案 0 :(得分:2)

  1. 您不使用Java中的equals(==)运算符来比较字符串。他们是对象。您需要使用equals()
  2. System.out.println无法在Android上运行。使用Log