在android中拆分字符串

时间:2012-03-03 22:29:35

标签: java android string

我在分割一行文字并在Edittext中显示时遇到一些麻烦。

这是我到目前为止所拥有的。

try {
            // open the file for reading
            InputStream instream = new FileInputStream("/sdcard/folder/file.txt");

            // if file the available for reading
            if (instream != null) {
              // prepare the file for reading
              InputStreamReader inputreader = new InputStreamReader(instream);
              BufferedReader buffreader = new BufferedReader(inputreader);

              String csvRecord;
              while ((csvRecord = buffreader.readLine()) != null) {
              String[] csvFields = csvRecord.split(",");
              String display = csvFields[0] + " " + csvFields[3] + "\r\n";

                EditText results = (EditText) findViewById(R.id.results);
                results.setText(toString.display);
                }

            }}
             finally {
            }

任何帮助都会很棒

问候

1 个答案:

答案 0 :(得分:2)

通过使用setText,您可以将输出重置为最后一行。使用:

results.append( display );

StringBuffer output = new StringBuffer();

while ( ( csvRecord = buffreader.readLine() ) != null )
{
    String[] csvFields = csvRecord.split( "," );
    output.append( csvFields[0] + " " + csvFields[3] + "\n" );
}

EditText results = (EditText)findViewById( R.id.results );
results.setText( output.toString() );