Example usage for com.badlogic.gdx.files FileHandle reader

List of usage examples for com.badlogic.gdx.files FileHandle reader

Introduction

In this page you can find the example usage for com.badlogic.gdx.files FileHandle reader.

Prototype

public BufferedReader reader(int bufferSize) 

Source Link

Document

Returns a buffered reader for reading this file as characters.

Usage

From source file:CB_Core.Api.LiveMapQue.java

License:Open Source License

public static CB_List<Cache> loadDescLiveFromCache(SearchLiveMap requestSearch) {
    String path = requestSearch.descriptor.getLocalCachePath(LIVE_CACHE_NAME) + LIVE_CACHE_EXTENTION;
    String result = null;//w w  w.  j  a  va2 s  . com

    FileHandle fh = new FileHandle(path);

    try {

        BufferedReader br = fh.reader(1000);
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            sb.append(line);
            sb.append(System.lineSeparator());
            line = br.readLine();
        }
        result = sb.toString();
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // parseResult
    CB_List<Cache> cacheList = new CB_List<Cache>();
    if (result != null && result.length() > 0) {
        if (gpxFilename == null) {
            Category category = CoreSettingsForward.Categories.getCategory("API-Import");
            gpxFilename = category.addGpxFilename("API-Import");
        }

        SEARCH_API.ParseJsonResult(requestSearch, cacheList, apiLogs, apiImages, gpxFilename.Id, result,
                (byte) 1, true);
        return cacheList;
    }

    return null;
}

From source file:com.austinerb.project0.engine.JsonReader.java

License:Apache License

public JsonValue parse(FileHandle file) {
    try {//from w  w  w  . j a v a 2  s  .  co  m
        return parse(file.reader("UTF-8"));
    } catch (Exception ex) {
        throw new SerializationException("Error parsing file: " + file, ex);
    }
}

From source file:com.badlogic.gdx.tests.g3d.ShaderLoader.java

License:Apache License

protected ObjectMap<String, String> parse(final FileHandle file) {
    ObjectMap<String, String> result = new ObjectMap<String, String>();
    BufferedReader reader = file.reader(1024);
    String line;/*from w w w  .ja v  a  2 s . c  om*/
    String snipName = "";
    stringBuilder.setLength(0);
    int idx;
    try {
        while ((line = reader.readLine()) != null) {
            if (line.length() > 3 && line.charAt(0) == '[' && (idx = line.indexOf(']')) > 1) {
                if (snipName.length() > 0 || stringBuilder.length() > 0)
                    result.put(snipName, stringBuilder.toString());
                stringBuilder.setLength(0);
                snipName = line.substring(1, idx);
            } else
                stringBuilder.append(line.trim()).append("\r\n");
        }
    } catch (IOException e) {
        throw new GdxRuntimeException(e);
    }
    if (snipName.length() > 0 || stringBuilder.length() > 0)
        result.put(snipName, stringBuilder.toString());
    return result;
}

From source file:com.bladecoder.engine.model.World.java

License:Apache License

public void loadGameState(FileHandle savedFile) throws IOException {
    EngineLogger.debug("LOADING GAME STATE");

    if (!disposed)
        dispose();/*from  w  w w  .j a va 2s  . c om*/

    init();

    if (savedFile.exists()) {
        SerializationHelper.getInstance().setMode(Mode.STATE);

        JsonValue root = new JsonReader().parse(savedFile.reader("UTF-8"));

        Json json = new Json();
        json.setIgnoreUnknownFields(true);

        read(json, root);

        assetState = AssetState.LOAD_ASSETS;

    } else {
        throw new IOException("LOADGAMESTATE: no saved game exists");
    }
}

From source file:com.bladecoder.engine.ui.Recorder.java

License:Apache License

@SuppressWarnings("unchecked")
public void load() {
    String gameStateFileName = fileName + GAMESTATE_EXT;
    String recordFileName = fileName + RECORD_EXT;

    FileHandle verbsFile = EngineAssetManager.getInstance().getUserFile(recordFileName);

    if (!verbsFile.exists())
        verbsFile = Gdx.files.internal("tests/" + recordFileName);

    if (verbsFile.exists()) {
        // LOAD GAME STATE IF EXISTS
        FileHandle gameStateFile = EngineAssetManager.getInstance().getUserFile(gameStateFileName);

        if (!gameStateFile.exists())
            gameStateFile = Gdx.files.internal("tests/" + gameStateFileName);

        if (gameStateFile.exists())
            try {
                World.getInstance().loadGameState(gameStateFile);
            } catch (IOException e) {
                EngineLogger.error(e.getMessage());
            }/*  w w  w .  j  a v a2 s .  c o m*/
        else
            EngineLogger.debug("LOADING RECORD: no saved file exists");

        // LOAD VERBS
        list = new Json().fromJson(ArrayList.class, TimeVerb.class, verbsFile.reader("UTF-8"));
    } else {
        EngineLogger.error("LOADING RECORD: no record file exists");
    }
}

From source file:com.ttgames.Blacksmith.Services.NameGenerator.java

License:Open Source License

public static String getRomanName() {
    if (nameGenerator == null) {
        FileHandle syllableFile = Gdx.files.internal("data/name-generator/syllables-roman");
        nameGenerator = new NameGenerator(syllableFile.file(), syllableFile.reader(255));
    }/*from w  w w . j a v  a 2s.c om*/

    String fname = nameGenerator.compose((int) Math.ceil(Math.random() * 3));
    String lname = nameGenerator.compose((int) Math.ceil(Math.random() * 4));

    return fname + " " + lname;
}

From source file:net.noviden.towerdefense.Maps.MapReader.java

License:Open Source License

public static net.noviden.towerdefense.Maps.Map createMapFromFile(FileHandle pFileHandle) {

    if (_tokenMap == null) {
        _tokenMap = new HashMap<String, Integer>();

        _tokenMap.put("STARTPATH", 0);
        _tokenMap.put("STARTSETTINGS", 2);
    }/*  ww  w .  j  av a2s. c om*/

    ArrayList<Path> paths = new ArrayList<Path>();
    MapSettings mapSettings = null;
    String name = pFileHandle.name();

    BufferedReader reader = pFileHandle.reader(20);

    String s;

    try {
        while (reader.ready()) {
            s = reader.readLine().trim();

            // catch bad input, attempt to continue
            if (!_tokenMap.containsKey(s)) {
                continue;
            }

            switch (_tokenMap.get(s)) {
            case 0:
                Path tmpPath = parsePathFromReader(reader);
                if (tmpPath != null)
                    paths.add(tmpPath);

                break;
            case 2:
                mapSettings = parseSettingsFromReader(reader);

                break;
            }
        }

    } catch (Exception e) {
        e.printStackTrace();

        return null;
    }

    // error: no paths supplied on map
    if (paths.size() == 0) {
        return null;
    }

    // error: map settings aren't supplied in file
    if (mapSettings == null) {
        //            mapSettings = new MapSettings();
    }

    // convert arraylist to array -- bleh
    Path[] returnablePaths = new Path[paths.size()];
    for (int i = 0; i < returnablePaths.length; i++)
        returnablePaths[i] = paths.get(i);

    return new net.noviden.towerdefense.Maps.Map(returnablePaths, name, mapSettings);
}

From source file:org.bladecoder.bladeengine.model.World.java

License:Apache License

public void loadGameState(FileHandle savedFile) {
    EngineLogger.debug("LOADING GAME STATE");

    if (!disposed)
        dispose();//from  www . j  a va  2  s .co m

    init();

    if (savedFile.exists()) {
        assetState = AssetState.LOAD_ASSETS;

        new Json().fromJson(World.class, savedFile.reader("UTF-8"));

    } else {
        EngineLogger.error("LOADGAMESTATE: no saved game exists");
    }
}

From source file:org.bladecoder.bladeengine.ui.Recorder.java

License:Apache License

@SuppressWarnings("unchecked")
public void load(String name) {
    String gameStateFileName;/* w ww  .ja va 2  s  .c o m*/
    String recordFileName;

    if (name != null) {
        gameStateFileName = name + "." + GAMESTATE_FILENAME;
        recordFileName = name + "." + RECORD_FILENAME;
    } else {
        gameStateFileName = GAMESTATE_FILENAME;
        recordFileName = RECORD_FILENAME;
    }

    FileHandle verbsFile = EngineAssetManager.getInstance().getUserFile(recordFileName);

    if (!verbsFile.exists())
        verbsFile = Gdx.files.internal("tests/" + recordFileName);

    if (verbsFile.exists()) {
        // LOAD GAME STATE IF EXISTS
        FileHandle gameStateFile = EngineAssetManager.getInstance().getUserFile(gameStateFileName);

        if (!gameStateFile.exists())
            gameStateFile = Gdx.files.internal("tests/" + gameStateFileName);

        if (gameStateFile.exists())
            World.getInstance().loadGameState(gameStateFile);
        else
            EngineLogger.error("LOADING RECORD: no saved file exists");

        // LOAD VERBS
        list = new Json().fromJson(ArrayList.class, TimeVerb.class, verbsFile.reader("UTF-8"));
    } else {
        EngineLogger.error("LOADING RECORD: no record file exists");
    }
}

From source file:se.theodor.quiz.GameField.java

License:Apache License

public void loadQuiz(String fileName) {
    FileHandle fh = Util.getQuizFileHandle(fileName);
    try {/*from ww w . j av  a 2s.  c om*/
        BufferedReader reader = fh.reader(8192);
        categories = new Array<Category>();
        Array<Question> questions = new Array<Question>();
        int questionsNumber = 0;
        String line;
        while ((line = reader.readLine()) != null) {
            if (line.startsWith("//")) {
                continue;
            }
            String[] args = line.split(":");
            if (args[0].equals("column")) {
                if ((categories.size - 1) >= 0) {
                    categories.get(categories.size - 1).setQuestions(questions, QUESTION_TEXT_BUTTON_STYLE);
                    questionsNumber = questions.size;
                    questions = new Array<Question>();
                }
                categories.add(new Category(args[1]));
            } else if (args[0].equals("row")) {
                questions.add(new Question(categories.get(categories.size - 1), args[1], args[2],
                        Integer.parseInt(args[3]), Integer.parseInt(args[4]), QUESTION_TEXT_BUTTON_STYLE));
            }
        }
        categories.get(categories.size - 1).setQuestions(questions, QUESTION_TEXT_BUTTON_STYLE);
        reader.close();
        questionsNumber = categories.get(0).getQuestions().size;
        int categoryIndex = 0;
        int categoryWidth = Gdx.graphics.getWidth() / categories.size;
        for (final Category c : categories) {
            final int categoryIndex2 = categoryIndex;
            TextButton button = new TextButton(c.getName(), QUESTION_TEXT_BUTTON_STYLE);
            button.setBounds(categoryIndex * categoryWidth, Gdx.graphics.getHeight() - 150, categoryWidth, 50);
            c.setButton(button, false);
            c.getButton().setDisabled(true);
            stage.addActor(c.getButton());
            for (int i = 0; i < questionsNumber; i++) {
                final int index = i;
                stage.addActor(c.getQuestions().get(i).getButton());
                c.getQuestions().get(i).getButton().addListener(new InputListener() {
                    @Override
                    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                        if (button == Buttons.LEFT) {
                            return true;
                        }
                        return false;
                    }

                    @Override
                    public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                        openQuestion(c.getQuestions().get(index), c.getQuestions().get(index).getButton(),
                                categoryIndex2, index);
                    }
                });
            }
            categoryIndex++;
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (NumberFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}