[英]Libgdx:Save and getback files in android
我有一個文本作為字符串,我必須將其保存到一個新的或已經創建的文件夾中,並使用自己的文件名擴展名。 用戶也應該能夠復制和使用這些文件。我已經遍歷了許多教程,但大多數都令人困惑。libgdx ( android平台)中是否有任何簡單的方法可以將帶有我自己的擴展名的字符串另存為文件並從中獲取內存中的文件列表。 我需要的是:
救:
public boolean saveToFile(String fileName)
{
FileHandle fileHandle = Gdx.files.local(fileName + ".txt");
BufferedWriter bw = new BufferedWriter(fileHandle.writer(false));
try
{
bw.append("stuff");
}
catch (IOException e)
{
e.printStackTrace();
return false;
}
finally
{
try
{
bw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return true;
}
閱讀:
FileHandle f = Gdx.files.local(fileName + ".txt");
BufferedReader r = null;
try
{
r = new BufferedReader(f.reader());
String line = null;
while((line = r.readLine()) != null)
{
//read the lines
}
}
catch (GdxRuntimeException e)
{
//file does not exist yet
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
if (r != null)
{
try
{
r.close();
}
catch (IOException e)
{
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.