android java 写入文件,java - 在Android上写入外部文件-文件未注册,但Java可以读取 - 堆栈内存溢出...

我正在尝试写入Android的外部txt(或csv)文件。 我可以运行一个应用程序,将其关闭,然后再次运行,然后readData()会将已存储的内容读回到我的日志中。 但是,dirFile(文件目录)在我的Android文件中不显示(即使我将其连接到计算机并进行搜索)。

不过,有一点有趣的事情:如果我清除日志(类似于Eclipse中显示的打印语句列表)并断开手机与计算机的连接,然后重新连接,该日志就会以我曾经写入文件的所有内容重新出现(即使后来我改写了它)...但是该应用程序甚至没有运行!

这是我的代码。 请帮助我理解为什么找不到我的文件!

(注意:我尝试将“ myFile.txt”扩展名追加到目录中,但这只会导致EISDIR异常。)

public void writeData(String dirName){

try

{

File root = new File(getExternalFilesDir(null), dirName);

// Writes to file

//

// The "true" argument allows the file to be appended. Without this argument (just root),

// the file will be overwritten (even though we later call append) rather than appended to.

FileWriter writer = new FileWriter(root, true);

writer.append("Append This Text\n");

writer.flush();

writer.close();

// Checks if we actually wrote to file by reading it back in (appears in Log)

//readData(dirName);

}

catch(Exception e)

{

Log.v("2222", "2222 ERROR: " + e.getMessage());

}

}

如果您有兴趣,这是我编写的读取数据的函数:

public void readData(String dirName){

try

{

File root = new File(getExternalFilesDir(null), dirName);

// Checks to see if we are actually writing to file by reading in the file

BufferedReader reader = new BufferedReader(new FileReader(root));

try {

String s = reader.readLine();

while (s != null) {

Log.v("2222", "2222 READ: " + s);

s = reader.readLine();

}

}

catch(Exception e) {

Log.v("2222", "2222 ERROR: " + e.getMessage());

}

finally {

reader.close();

}

}

catch(Exception e) {

Log.v("2222", "2222 ERROR: " + e.getMessage());

}

}

谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值