tokenex.txt
hong gil dong,20,수영,요리,1,abcd,1234|hong gil soon,30,독서,등산,2,efgh,5678
class MainActivity
package com.example.choi.mystudy26_1;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class MainActivity extends AppCompatActivity {
final static String TAG="MainActivity";
EditText editText2;
Button button1;
StringBuffer parsingText=new StringBuffer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText2=(EditText)findViewById(R.id.editText2);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
parser();
Log.i(TAG, parsingText.toString());
editText2.setText(parsingText.toString());
}
});
}
private void parser(){
Log.i(TAG, "parser()");
InputStream is =getResources().openRawResource(R.raw.tokenex);
InputStreamReader isr =new InputStreamReader(is);
BufferedReader reader=new BufferedReader(isr);
try{
StringBuffer stringBuffer =new StringBuffer();
String str=null;
while ((str=reader.readLine())!=null){
stringBuffer.append(str);
}
Log.i(TAG, "sb : "+ stringBuffer.toString());
String sting=stringBuffer.toString();
StringTokenizer tokenizer =new StringTokenizer(sting, "|");
Log.i(TAG, "Token count :" +tokenizer.countTokens());
while (tokenizer.hasMoreElements()){
String tokenStr =tokenizer.nextToken();
Log.i(TAG, "Token String(|) : " + tokenStr);
StringTokenizer tokenizer2=new StringTokenizer(tokenStr, ",");
parsingText.append("이름 : " +tokenizer2.nextToken() +"\n");
Log.i(TAG, "Token count : " +tokenizer2.nextToken());
while (tokenizer2.hasMoreElements()){
String tokenStr2=tokenizer2.nextToken();
Log.i(TAG, "Token string(,) : " +tokenStr2);
parsingText.append(tokenStr2 +" ");
}
parsingText.append("\n\n");
Log.i(TAG, "==================");
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if(reader!=null) reader.close();
if(isr!=null)isr.close();
if(is!=null)is.close();
}catch (Exception e2){
e2.printStackTrace();
}
}
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.choi.mystudy26_1.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/editText2"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_above="@+id/button1" />
<Button
android:text="토큰 파싱"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/button1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@android:color/holo_blue_dark"
android:textColor="@android:color/background_light" />
</RelativeLayout>
댓글 ( 4)
댓글 남기기