android - 如何从 Firebase 数据库制作 .txt 文件?
问题描述
databaseReference.child("chat").child(chatName).addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
for(DataSnapshot snapshot : dataSnapshot.getChildren())
{
Log.d("ChatActivity", "ValueEventListener : "+chatName + snapshot.getValue() + "\n");
}
}
当我运行 Log.d
2019-11-26 16:23:32.928 2599-2599/com.test.myapplication D/ChatActivity: ValueEventListener : TESTCHAT{time= AM 7:23, message=test1, userName=USER1}
2019-11-26 16:23:32.928 2599-2599/com.test.myapplication D/ChatActivity: ValueEventListener : TESTCHAT{time= AM 7:23, message=test2, userName=USER1}
我想制作从“Firebase 数据库聊天日志”中获取的 txt 文件,我
该怎么办?
解决方案
要创建一个 txt 文件,然后在 for 循环中执行以下操作:
try {
File logTxt = new File("log.txt");
FileOutputStream is = new FileOutputStream(logTxt);
OutputStreamWriter osw = new OutputStreamWriter(is);
Writer w = new BufferedWriter(osw);
w.write(snapshot.getValue(String.class));
w.close();
} catch (IOException e) {
System.err.println("Problem writing to the file log.txt");
}
初始化 File 的一个新实例,并为其指定要创建的 txt 文件的路径,然后使用 将数据BufferedWriter
写入snapshot
文件。
推荐阅读
- python - 从 R 到 Python:突变数据帧中的 ifelse 条件
- laravel - 使用 Laravel 从资源中的数据透视表中查找相关表的值很热门
- ios - 是否有任何经验法则来决定是覆盖 UIView 的绘制功能,还是在实现子视图时使用 CAShapeLayer?
- flutter - Flutter - 如何在本地保存数据?
- powershell - 递归替换win10文件夹和文件中的非标准字符
- php - 如何在 PHP 中使用依赖注入?
- reactjs - 我不明白为什么这个扬声器组件需要 React.memo 来防止额外的渲染
- python - 如何使用正则表达式找到具有可变长度前缀的重叠匹配?
- visual-studio-code - Visual Studio Code 删除自动完成
- jquery - 将方法存储在将在“if”条件下使用的变量中是不好的做法还是不正确?