简体   繁体   English

更改或删除移动存储中文件txt中的单行

[英]Change or delete single row in a file txt on the mobile storage

I'm developing an application for save more accounts. 我正在开发一个用于保存更多帐户的应用程序。 I don't use database but i use file... My question is if is possible change or delete a single row in the file txt on the storage of my phone??? 我不使用数据库,但使用文件...我的问题是,是否可以更改或删除手机存储中文件txt中的一行?

You can edit a file only if you have read and write access to it. 只有拥有文件读写权限,您才能编辑文件。 You mentioned in the comments that you have already read and written to a file. 您在注释中提到已经读取并写入文件。 To edit what you have to do is read the content from the file, edit it and write it again. 要编辑您要做的是从文件中读取内容,对其进行编辑然后再次写入。 To achieve that just repeat what you did when reading/writing to it 要做到这一点,只需重复读/写时所做的操作即可

To edit a file's content, you need to read it from disk, create a new FileInputStream with it, edit your stream then create a new FileOutPutSteam and write to it, like below: 要编辑文件的内容,您需要从磁盘读取文件,使用它创建一个新的FileInputStream,编辑流,然后创建一个新的FileOutPutSteam并对其进行写入,如下所示:

File file = f;
FileInputStream fin;
fin = new FileInputStream(file);
byte fileContent[] = new byte[(int)file.length()];
fin.read(fileContent)
//Convert your stream to string, manipulate it and convert it back to a stream
FileOutputStream fos = new FileOutputStream(f.getAbsolutePath());
fos.write(my_out_stream);
fin.close();
fos.close();

To convert your FileInputStream to a string use: https://stackoverflow.com/a/15161590/10089348 要将FileInputStream转换为字符串,请使用: https : //stackoverflow.com/a/15161590/10089348

And to convert back it to a FileOutputStream use https://stackoverflow.com/a/4069932/10089348 并将其转换回FileOutputStream,请使用https://stackoverflow.com/a/4069932/10089348

Hope it helps :) 希望能帮助到你 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM