Java 写入文件错误-Android Studio
我正在尝试写入一个特定的文本文件,下面是我正在使用的代码Java 写入文件错误-Android Studio,java,android,Java,Android,我正在尝试写入一个特定的文本文件,下面是我正在使用的代码 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button clickButton = (Button) findViewById(R.id.Btn); if( clickButton != nu
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button clickButton = (Button) findViewById(R.id.Btn);
if( clickButton != null){
clickButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText customerBox = (EditText) findViewById(R.id.TextBox);
String customerId = ""; if (customerId != null){customerId = customerBox.getText().toString();}
try {
FileInputStream fis = openFileInput("Registry.txt");
String baretext = fis.read(string.getBytes());
fis.close();
int pos = baretext.indexOf(customerId);
baretext.ReplaceAll(pos, "NO", "YES", 1);
FileOutputStream fos = openFileOutput("Registry.txt",MODE_WORLD_WRITEABLE);
fos.write(baretext);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
});
}
}
然而,我得到以下错误
Error:(42, 52) error: cannot find symbol variable string
Error:(45, 33) error: cannot find symbol method
ReplaceAll(int,String,String,int)
Error:(47, 28) error: no suitable method found for write(String)
method FileOutputStream.write(int) is not applicable
(actual argument String cannot be converted to int by method invocation
conversion)
method FileOutputStream.write(byte[],int,int) is not applicable
(actual and formal argument lists differ in length)
method OutputStream.write(int) is not applicable
(actual argument String cannot be converted to int by method invocation
conversion)
method OutputStream.write(byte[],int,int) is not applicable
(actual and formal argument lists differ in length)
method OutputStream.write(byte[]) is not applicable
(actual argument String cannot be converted to byte[] by method invocation
conversion)
有人能帮我纠正一下我的错误吗:好吧。。。在android中,您应该将信息保存在数据库中或使用SharedReferences。其实很简单:
SharedPreferences sp = getSharedPreferences(SHARED_PREF, getApplicationContext().MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString(SP_NAME, etName.getText().toString());
editor.commit();
如果要恢复信息,请执行以下操作:
SharedPreferences sp = getSharedPreferences(SHARED_PREF,getApplicationContext().MODE_PRIVATE);
String cadName = sp.getString(SP_NAME, "");
if (cadName.length()>0){
Log.d("HELENA","info saved : "+cadName);
}
else
Log.d("HELENA","No info saved.");
第一个错误。您正在调用字符串baretext=fis.readstring.getBytes中的字符串;当代码中没有字符串这样的东西时 第二个错误。Java中的Google ReplaceAll方法。语法错误 第三个错误。要写入文件,需要使用字节数组。将字符串转换为字节数组并写入文件
最后。这是一个糟糕的问题,我认为应该关闭它,因为您没有进行任何搜索。所有这些对谷歌来说都很容易,不需要太多的知识就可以理解。这是完全错误的,不是OP所要求的。他需要写一个文件。在Android中,可能会建议删除此问题,因为它根本不显示任何搜索工作。您似乎希望使用文本文件,就像使用数据库或共享引用一样。那么,你为什么不以正确的方式做事呢?