在附加文本文件中的数据并不在Android应用工作 [英] Append data in text file does not work in android application

查看:260
本文介绍了在附加文本文件中的数据并不在Android应用工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code对追加利用GPS获得的新的位置数据,每一次位置的变化。

 如果(isExternalStorageWritable()){
    如果(myFile.exists()){
        尝试{
            FileOutputStream中FOUT =新的FileOutputStream(MYFILE);
            OutputStreamWriter myOutWriter =新OutputStreamWriter(FOUT);
            myOutWriter.append(location.getLatitude()+,+ location.getLongitude()+,+ nodeCounter +\\ n);
            myOutWriter.close();
            fOut.close();
            nodeCounter ++;
        }赶上(例外五){        }
    }其他{
        尝试{
            myFile.createNewFile();
        }赶上(IOException异常五){
            e.printStackTrace();
        }
    }
}其他
{
    Log.i(logGPSData,错误);
}

问题是,追加不工作,因为每次一个新行被插入时,previous行被覆盖,所以我的文件包含总是一条线,即使我收集很多的GPS数据。


解决方案

第二个参数是指如果文本应该附加到现有文件时,请更改code以下行:

 的FileOutputStream FOUT =新的FileOutputStream(MYFILE);
OutputStreamWriter myOutWriter =新OutputStreamWriter(FOUT);

 的FileOutputStream FOUT =新的FileOutputStream(MYFILE,真);
OutputStreamWriter myOutWriter =新OutputStreamWriter(FOUT,真);

从文档:

 公开的FileOutputStream(字符串名称,布尔追加)
                 抛出FileNotFoundException异常

创建一个文件输出流写入到具有指定名称的文件。如果第二个参数为true,则将字节写入文件,而不是开始的结束。一个新FileDescriptor对象被创建重新present此文件连接。
首先,如果有安全管理器,它的checkWrite方法调用名作为参数。<​​/ P>

如果该文件存在,但它是一个目录,而不是一个常规文件,不存在,但不能被创建,或者无法打开任何其他原因则抛出FileNotFoundException。

参数:

1)名称 - 与系统有关的文件名

2)追加 - 如果为true,则将字节写入文件末尾而不是开头

抛出:

1)FileNotFoundException异常 - 如果文件存在,但是一个目录,而不是一个普通文件,不存在,但不能被创建,或不能打开任何其他原因

2)抛出:SecurityException - 如果安全管理器存在并且其checkWrite方法拒绝写入访问文件

由于

JDK1.1

I'm using the following code for append new position data obtained by GPS, each time position change.

if (isExternalStorageWritable()) {
    if (myFile.exists()) {
        try {
            FileOutputStream fOut = new FileOutputStream(myFile);
            OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
            myOutWriter.append(location.getLatitude()+", "+location.getLongitude()+", "+nodeCounter+"\n");
            myOutWriter.close();
            fOut.close();
            nodeCounter++;
        } catch (Exception e) {

        }
    } else {
        try {
            myFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}else
{
    Log.i("logGPSData","Error");
}

The problem is that the append does not work, since each time a new row is inserted, the previous row is overwritten, so my file contains always one line, even if I collect many gps data.

解决方案

The second argument means if text should be appended to the existing file, change the following lines in your code:

FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);

to:

FileOutputStream fOut = new FileOutputStream(myFile,true);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut,true);

From the Docs:

public FileOutputStream(String name,boolean append)
                 throws FileNotFoundException

Creates a file output stream to write to the file with the specified name. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection. First, if there is a security manager, its checkWrite method is called with name as its argument.

If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

Parameters:

1) name - the system-dependent file name

2) append - if true, then bytes will be written to the end of the file rather than the beginning

Throws:

1)FileNotFoundException - if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.

2)SecurityException - if a security manager exists and its checkWrite method denies write access to the file.

Since:

JDK1.1

这篇关于在附加文本文件中的数据并不在Android应用工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆