安卓中从txt文件读出信息

一、写一个NodeLocation类

         它的的功能是读出txt文件,要求txt文件中内容规范为以下格式:

要隔开的信息用英文逗号分隔,而且每一行的信息结束后要换行。

该类的代码如下:

public class NodeLocation {
     static  ArrayList<String> nodes = new ArrayList<>();
     private static int  iNum=0;
     private static String[] split = new String[2];

     public static ArrayList<String> txt2String(File file){
        try {
            //构造一个BufferedReader类来读取文件
            BufferedReader br = new BufferedReader(new FileReader(file));
            String s = null;

            //使用readLine方法,一次读一行,txt中的值分别是经度、纬度
            while ((s = br.readLine()) != null) {
                split = s.split(",");//每一行的两个值通过“,”来分割开,并存放到数组中
                nodes.add(split[0]);//将String型数组splite的信息添加到nodes中
                nodes.add(split[1]);
                iNum= iNum +2;
            }
            br.close();//要求读完信息之后要手动将BufferReader关闭
        } catch (Exception e) {
            e.printStackTrace();
        }
        return nodes;
    }

二、调用该类

Nodes = NodeLocation.txt2String(new File(Environment.
            getExternalStorageDirectory() + "/Position.txt"));
//将要用的文件放在根目录下,取名为Position.txt
//调用NodeLocation的txt2String方法,该方法返回一个ArrayList类型的数组

三、其他须知

        因为在定义txt2String的时候是String型的数组链表,所以我们在这里需要把String类型转化为Double类型,从而才能进行后续的计算,调用方法如下:

double c  = Double.parseDouble(Nodes.get(0));
//Nodes.get(0)是一个String类型的字符串,如果我们想将其变为Double型浮点数,
//只需调用Double.parseDouble()方法,想要变成int,则需用Integer.parseInt方法

小tips:

int  d = Integer.parseInt(Nodes.get(0));
//这个会报错,因为Nodes.get(0)="116.915872"
//这个数本来就不是int型,不能用该方法直接变为int


int  d = (int)Double.parseDouble(Nodes.get(0));
//可以加一个类型强转符,这样可以成功运行

 

好的,我可以帮你完成这个任务。首先,我们需要在Android Studio创建一个新的项目,然后按照以下步骤进行操作: 1. 在app的build.gradle文件添加以下依赖项: ``` implementation 'com.android.support:appcompat-v7:28.0.0' ``` 2. 在activity_main.xml文件添加一个EditText和两个Button控件,代码如下所示: ```xml <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入内容"/> <Button android:id="@+id/btnWrite" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="写入"/> <Button android:id="@+id/btnRead" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读取"/> ``` 3. 在MainActivity.java文件添加以下代码: ```java public class MainActivity extends AppCompatActivity { private EditText editText; private Button btnWrite, btnRead; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = findViewById(R.id.editText); btnWrite = findViewById(R.id.btnWrite); btnRead = findViewById(R.id.btnRead); btnWrite.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String text = editText.getText().toString(); writeToFile(text); } }); btnRead.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String text = readFromFile(); editText.setText(text); } }); } private void writeToFile(String text) { try { FileOutputStream fos = openFileOutput("my_file.txt", MODE_PRIVATE); fos.write(text.getBytes()); fos.close(); Toast.makeText(this, "写入成功!", Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); } } private String readFromFile() { String text = ""; try { FileInputStream fis = openFileInput("my_file.txt"); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); text = new String(buffer); } catch (IOException e) { e.printStackTrace(); } return text; } } ``` 4. 运行应用程序并测试。您可以输入一些文本,然后单击“写入”按钮将其写入文件。接下来,单击“读取”按钮,以从文件读取文本并在EditText显示它。您还可以使用DDMS(Dalvik Debug Monitor Service)查看应用程序的数据存储。 以上就是实现这个功能的全部步骤,希望能够帮助到您!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值