写入文件错误 - Android Studio

时间:2016-05-01 22:06:14

标签: java android

我正在尝试写入某个文本文件,这是我用来执行此操作的代码

 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)

有人会善意纠正我出错的地方:)

2 个答案:

答案 0 :(得分:0)

嗯...在android中你应该将信息保存在数据库中或使用SharedPreferences。它非常简单:

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.");

如果您想恢复信息:

    <ons-navigator title="myApp" var="myNavigator">
        <ons-page id="mainPage">
            <ons-toolbar>
                <div class="center">Title</div>
            </ons-toolbar>

            <ons-list>
                <ons-list-item id="button" modifier="chevron">
                    Button Text
                </ons-list-item>
            </ons-list>
        </ons-page>
    </ons-navigator>

    <ons-template id="newPage.html">
        <ons-page id="newPage">
            <ons-toolbar>
                <div class="left">
                    <ons-back-button>Back</ons-back-button>
                </div>
                <div class="center">Page Title</div>
            </ons-toolbar>
            <button id="submit-btn" class="button button--large">Submit</button><br>
        </ons-page>
    </ons-template>

答案 1 :(得分:0)

第一个错误。你要求&#34;字符串&#34;在String baretext = fis.read(string.getBytes());中没有&#34;字符串&#34;在你的代码中

第二个错误。 Java中的Google ReplaceAll()方法。语法错误。

第三个错误。要写入文件,您需要使用字节数组。将您的字符串转换为字节数组并写入文件。

最后。这是一个糟糕的问题,我认为它应该被关闭,因为你没有进行任何搜索。所有这一切都很容易谷歌,并不需要任何知识来理解。