• 读取文件,截取字符串


    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;

    import com.exaple.vo.Company;

    import android.os.Bundle;
    import android.os.Handler;
    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.Toast;

    public class NewActivity extends Activity implements OnClickListener {
    Handler ha=new Handler(){
    public void handleMessage(android.os.Message msg) {
    Base1 ba=new Base1(list,NewActivity.this);
    listvew.setAdapter(ba);
    };
    };
    private List<Company> list;
    private ListView listvew;
    //String str;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new);
    // 找到控件
    Button button1 = (Button) findViewById(R.id.button1);
    Button button2 = (Button) findViewById(R.id.button2);
    Button button3 = (Button) findViewById(R.id.button3);
    listvew = (ListView) findViewById(R.id.listview);
    //监听
    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
    button3.setOnClickListener(this);



    SharedPreferences sha = getSharedPreferences("lo", 0);
    Editor edit = sha.edit();
    edit.putString("aa", "ss");
    edit.commit();
    new Thread(){
    public void run() {
    try {
    // 读取
    InputStream is = getAssets().open("data.txt");
    StringBuffer sb=new StringBuffer();
    String str;
    //用bufferreader按行读取
    BufferedReader bf = new BufferedReader(new InputStreamReader(
    is));

    while ((str = bf.readLine()) != null) {
    sb.append(str);

    }
    /*//量身定做的读取方法
    int length=is.available();
    byte[] buffer = new byte[length];
    is.read(buffer);
    String str = new String(buffer, 0, length);
    sb.append(str);
    */
    System.out.println(sb+"=============");
    String data = sb.toString();
    String[] datas = data.split("#");
    System.out.println(datas.length);
    for(int i=0;i<datas.length;i++)
    {
    System.out.println(datas[i]);
    }
    Company con=new Company();
    con.setName(datas[0]);
    con.setHangye(datas[1]);
    con.setAddress(datas[2].substring(0, 2));
    System.out.println(con.getName()+"+++="+con.getHangye()+"==="+con.getAddress()+"=======");

    Company con1=new Company();
    con1.setName(datas[2].substring(2));
    con1.setHangye(datas[3]);
    con1.setAddress(datas[4].substring(0, 2));
    System.out.println(con1.getName()+"+++="+con1.getHangye()+"==="+con1.getAddress()+"=======");
    Company con2=new Company();
    con2.setName(datas[4].substring(2));
    con2.setHangye(datas[5]);
    con2.setAddress(datas[6]);
    System.out.println(con2.getName()+"+++="+con2.getHangye()+"==="+con2.getAddress()+"=======");
    list = new ArrayList<Company>();
    list.add(con);
    list.add(con1);
    list.add(con2);
    ha.sendMessage(ha.obtainMessage(1, list));

    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    };
    }.start();

    }

    @Override
    public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button1:

    break;
    case R.id.button2:

    break;
    case R.id.button3:

    break;

    }

    }

    }

  • 相关阅读:
    物联网相关期刊会议
    Java O O第十一天
    mac下安装伪分布hadoop2.6.0和hbase1.0.1.1
    Ubuntu下使用openssl为apache2配置ssl
    ubuntu下安装hadoop
    ubuntu 添加应用到Dash启动器
    Ubuntu12.04中安装Oracle JDK和NetBeans的方法
    Ubuntu12.04中在桌面建立指向网页的链接文件的方法
    Ubuntu12.04中Gvim无法固定到启动器的解决办法
    Ubuntu中apt-get安装或更新软件错误的解决办法
  • 原文地址:https://www.cnblogs.com/jsonfan/p/5236275.html
Copyright © 2020-2023  润新知