簡體   English   中英

Libgdx:在Android中保存和獲取文件

[英]Libgdx:Save and getback files in android

我有一個文本作為字符串,我必須將其保存到一個新的或已經創建的文件夾中,並使用自己的文件名擴展名。 用戶也應該能夠復制和使用這些文件。我已經遍歷了許多教程,但大多數都令人困惑。libgdxandroid平台)中是否有任何簡單的方法可以將帶有我自己的擴展名的字符串另存為文件並從中獲取內存中的文件列表。 我需要的是:

  1. 將文件保存到新文件夾或現有文件夾中的功能(用戶可用)。將String作為參數。
  2. 獲取指定文件夾中“ .my”文件列表的另一個功能。

救:

    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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM