API內部檔案讀取

來源:互聯網
上載者:User

標籤:

直接上代碼吧

嘗試將項目複製後建一個新的項目,結果總是有問題,不過可以把原項目轉換為新項目,方法如下:

1.項目右鍵在android tools 有個 rename application package選項,這是改包名的,改完這個就是兩個不同的應用

2.要想兩個工程共存在Eclipse下的話,就還要選中項目 按F2 就可以改 項目的名字。

 

主要解決在上一個文檔中,路徑data/data/com.swust.intern這個不能寫錯的問題,所以直接使用Google提供的API擷取環境變數,防止讀錯。

 getFilesDir(),返回一個檔案路徑:

data/data/com.swust.ApiIntern/files路徑

修改前:

    File file =new File("data/data/com.swust.intern/info.txt");

修改後:
    File file =new File(getFilesDir(),"info.txt");

這樣在data/data/com.swust.ApiIntern/files(多個檔案夾)下便有了info.txt

代碼:

package com.swust.intern;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStreamReader;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;import com.swust.APIintern.R;public class MainActivity extends Activity {    private EditText et_name;    private EditText et_pwd;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //已經是全域變數,應用啟動時擷取ID        et_name = (EditText) findViewById(R.id.et_name);        et_pwd = (EditText) findViewById(R.id.et_pwd);                //應用啟動後,讀取存檔的使用者名稱和密碼        readAccount();    }    public void readAccount(){        //File file =new File("data/data/com.swust.intern/info.txt");        File file =new File(getFilesDir(),"info.txt");        //判讀檔案是否存在        if(file.exists()){            try{                FileInputStream fis =new FileInputStream(file);                //把位元組流轉化為字元流                BufferedReader br =new BufferedReader(new InputStreamReader(fis));                //讀取txt檔案的使用者名稱和密碼                //讀取一行                String text =br.readLine();                //使用“##”標識切割,得到字串數組                String[] s = text.split("##");                                                et_name.setText(s[0]);                et_pwd.setText(s[1]);                            }catch (Exception e){                e.printStackTrace();            }        }            }    public void login(View v){                CheckBox cb      = (CheckBox)findViewById(R.id.cb);        String name=et_name.getText().toString();        String pwd =et_pwd.getText().toString();        //判讀複選框是否被勾選        if(cb.isChecked()){        //"data/data/com.swust.intern"是內部儲存空間路徑        //File file =new File("data/data/com.swust.intern/info.txt");            File file =new File(getFilesDir(),"info.txt");            FileOutputStream fos;            try{                fos = new FileOutputStream(file);                //加“##”是為了讀取時好分割                fos.write((name + "##" +pwd).getBytes());                fos.close();            }catch (Exception e){                e.printStackTrace();            }        }        //System.out.println("登陸成功");        //建立多士對話方塊        /* 第一個參數上context,而activity本來就是context的子類,所以直接填this*/        /*第三個參數為持續顯示時間,只有LENGTH_SHORT(2S)和LENGTH_LONG(5s)兩種選擇*/        /*Toast t= Toast.makeText(this, "登陸成功", Toast.LENGTH_SHORT);        //顯示多士對話方塊        t.show();*/                Toast.makeText(this, "登陸成功", Toast.LENGTH_SHORT).show();    }}

 

API內部檔案讀取

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.