0

我正在开发一个需要从内部存储中保存/读取文件的应用程序。但它在同一个 TextView 中读取了我的所有数据。有人可以告诉我如何在 2 个文本视图中显示数据,或者告诉我如何将一个数据放在另一个数据下。

这是我保存数据的代码:

private void SaveMode() {

            String FILENAME ;
            String Strin1= textview1.getText().toString();
            String String2= textview2.getText().toString();


            EditText filename1 = (EditText) findViewById(R.id.filename);

            FILENAME = filename1.getText().toString();
            if (FILENAME.contentEquals("")){
                FILENAME = "UNTITLED";
            }



            String1 = textview1.getText().toString();
            String2= textview2.getText().toString();

            FileOutputStream fos = null;


            try {
                fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            try {
                fos.write("Strin1.getBytes()); 
                fos.write(String2.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }                   


            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

这是我读取数据的代码:

private void getFilenames() {
    String[] filenames = getApplicationContext().fileList();
    List<String> list = new ArrayList<String>();
    for(int i = 0; i<filenames.length; i++){
        //Log.d("Filename", filenames[i]);
        list.add(filenames[i]);
    }
    ArrayAdapter<String> filenameAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, list);
    spinner.setAdapter(filenameAdapter);
}

public void SpinnerClick(View v) {

    String selectFile = String.valueOf(spinner.getSelectedItem());
    openFile(selectFile);

}

private void openFile(String selectFile) {

    showData = (TextView) findViewById(R.id.show_data);
    TextView showData1 = (TextView) findViewById(R.id.show_data1);
    String value = "";
    FileInputStream fis;


    try {
        fis = openFileInput(selectFile);
        byte[] input = new byte[fis.available()];
        while(fis.read(input) != -1){

            value += new String(input);
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    showData.setText(value);        
}

编辑 我试图像这样编辑我的阅读代码,但没有运气

private void openFile(String selectFile) {

    TextView showData = (TextView) findViewById(R.id.show_data);
    TextView showData2 = (TextView) findViewById(R.id.show_data2);
    String value = "";      
    String[] strArray =  value.split(";");

    try {
        FileInputStream fis = openFileInput(selectFile);
        byte[] input = new byte[fis.available()];
        while(fis.read(input) != -1){


            value += new String(input);
        }

        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    showData.setText(value);
    showData.setText(strArray[0]);
    showData2.setText(strArray[1]);
}

编辑 2

让它与 Shobhit Puri 代码一起使用

4

2 回答 2

1

首先,在保存数据时,您可以在这两个字符串之间插入一个分隔符。确保分隔符不是您的 textViews 中预期的分隔符。

保存时:

String string3 = ";";

try {
    fos.write("Strin1.getBytes()); 
    fos.write("String3.getBytes()); 
    fos.write(String2.getBytes());
} catch (IOException e) {
    e.printStackTrace();
} 

然后,当您尝试将其读入value字符串时, split 正在使用.split函数。例如:

String[] strArray =  value.split(";");

strArray[0]将给出第一个 textview 的刺痛,strArray[1]并将给出第二个。

更新

private void openFile(String selectFile) {

    TextView showData = (TextView) findViewById(R.id.show_data);
    TextView showData2 = (TextView) findViewById(R.id.show_data2);
    String value = "";      

    try {
        FileInputStream fis = openFileInput(selectFile);
        byte[] input = new byte[fis.available()];
        while(fis.read(input) != -1){


            value += new String(input);
        }

        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    String[] strArray =  value.split(";");

    showData.setText(strArray[0]);
    showData2.setText(strArray[1]);
}
于 2013-07-29T12:16:37.727 回答
0
try 
                {
                    FileInputStream fis = new FileInputStream(myInternalFile);
                    DataInputStream in = new DataInputStream(fis);
                    BufferedReader br =  new BufferedReader(new InputStreamReader(in));
                    String strLine;
                    while ((strLine = br.readLine()) != null) {
                     myData = myData + strLine;
                }
                    in.close();
                   } catch (IOException e) {
                    e.printStackTrace();
                   }
                   myInputText.setText(myData);
于 2013-07-29T12:11:14.223 回答