java 如何在 Android 上将数据保存到文本文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5688741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:20:17  来源:igfitidea点击:

How to save data to a text file on Android?

javaandroidmysql

提问by roro

I need to save user-entered data like name and age to a text file on Android, in order that I can then save it to MySQL. How can I do this? Any help please thank you.

我需要将用户输入的数据(如姓名和年龄)保存到 Android 上的文本文件中,以便我可以将其保存到 MySQL。我怎样才能做到这一点?任何帮助请谢谢。

回答by Seva Alekseyev

Filesystems of Android phone/emulator and of your computer are separate. Even if it's the emulator, there's no directory that both Python code and Android Java can read.

Android 手机/模拟器和您的计算机的文件系统是分开的。即使是模拟器,也没有Python代码和Android Java都可以读取的目录。

EDIT based on comment:

根据评论编辑:

So when the game goes live, are you still going to use your (or player's) PC as a server? What you're facing is a very common app design; the Python code should reside on a Web service, you should use HTTP on Android to communicate to it, and forget about text files.

那么当游戏上线时,您是否仍将使用您(或玩家)的 PC 作为服务器?您面临的是一个非常常见的应用程序设计;Python 代码应该驻留在 Web 服务上,您应该在 Android 上使用 HTTP 与它通信,而不必考虑文本文件。

Read up on Android's HttpClient class, and on setting up Python on a Web server.

阅读 Android 的 HttpClient 类,以及在 Web 服务器上设置 Python。

回答by debracey

Presumably he wants to how to load/save the data to the phone's SD card:

大概他想知道如何将数据加载/保存到手机的 SD 卡:

Android how to use Environment.getExternalStorageDirectory()

Android 如何使用 Environment.getExternalStorageDirectory()

With that you can read the text file that is placed on the SD card by the user. Python and MySQL don't run on android (not on a stock phone anywyay) so if you want to store the data from the text file into Android's built in database, you need to use SQLite - which is provided by the framework:

有了它,您可以读取用户放置在 SD 卡上的文本文件。Python 和 MySQL 不能在 android 上运行(无论如何都不能在手机上运行),因此如果您想将文本文件中的数据存储到 Android 的内置数据库中,您需要使用 SQLite - 它由框架提供:

http://developer.android.com/guide/topics/data/data-storage.html(SQL info is at the bottom)

http://developer.android.com/guide/topics/data/data-storage.html(SQL信息在底部)

A good example of how to use the DB is here:

如何使用数据库的一个很好的例子是这里:

http://www.screaming-penguin.com/node/7742

http://www.screaming-penguin.com/node/7742

Your quesiton wasn't really that specific -- but if you need to send the data from the phone to an external database (ie on some server) then it's a whole different ball game. In that case, it is probably easiest to use some kind of URL based call on the server. (ie. REST)

您的问题并不是那么具体——但是如果您需要将数据从手机发送到外部数据库(即在某个服务器上),那么这是一个完全不同的球类游戏。在这种情况下,在服务器上使用某种基于 URL 的调用可能是最简单的。(即休息)

-- Dan

——丹

回答by Heiko Rupp

I would add some sort of "web-interface" in front of the MySQL database that takes some content e.g. encoded in XML or JSON. On the Android side, after the user has entered the data, open a HttpConnection to the server and transmit that data.

我会在 MySQL 数据库前面添加某种“网络界面”,它采用一些内容,例如以 XML 或 JSON 编码。在 Android 端,在用户输入数据后,打开一个到服务器的 HttpConnection 并传输该数据。