Войти или зарегистрироваться
  • Вы находитесь в сообществе Rubukkit. Мы - администраторы серверов Minecraft, разрабатываем собственные плагины и переводим на различные языки плагины наших коллег из других стран.

Помогите Как из List загрузить в файл и обратно(Location)

Rider3217

Активный участник
Пользователь
Баллы
98
Имя в Minecraft
Planet0111
Как из List загрузить в файл локации и потом из файла вернуть локации в List?
 
P.S. Мне вообще пофиг.
Тогда нужно написать сериализер(не знаю как точно), т.е. ты пишешь функцию, которая преобразовывает нужные тебе значения в строку и пишешь функцию, которая парсит строку, извлекая значения.
 
Java:
public static boolean writeLocation(double x, double y, double z, String worldName, File file)
{
    FileOutputStream fileStream = null;
    DataOutputStream out = null;

    try
    {
        fileStream = new FileOutputStream(file);
        out = new DataOutputStream(fileStream);
        out.writeDouble(x);
        out.writeDouble(y);
        out.writeDouble(z);
        out.writeUTF(worldName);
    }
    catch (Throwable t)
    {
        return false;
    }
    finally
    {
        try
        {
            if (out != null) out.close();
            if (fileStream != null) fileStream.close();
        }
        catch (Throwable t)
        {
            return false;
        }
    }

    return true;
}

public static Object[] readLocation(File file)
{
    FileInputStream fileStream = null;
    DataInputStream in = null;

    double x;
    double y;
    double z;
    String worldName;

    try
    {
        fileStream = new FileInputStream(file);
        in = new DataInputStream(fileStream);
        x = in.readDouble();
        y = in.readDouble();
        z = in.readDouble();
        worldName = in.readUTF();
    }
    catch (Throwable t)
    {
        x = Double.NaN;
        y = Double.NaN;
        z = Double.NaN;
        worldName = "UNKNOUN";
    }
    finally
    {
        try
        {
            if (in != null) in.close();
            if (fileStream != null) fileStream.close();
        }
        catch (Throwable t)
        {
            x = Double.NaN;
            y = Double.NaN;
            z = Double.NaN;
            worldName = "UNKNOUN";
        }
    }

    return new Object[] { x, y, z, worldName };
}
 
Java:
public static boolean writeLocation(double x, double y, double z, String worldName, File file)
{
    FileOutputStream fileStream = null;
    DataOutputStream out = null;

    try
    {
        fileStream = new FileOutputStream(file);
        out = new DataOutputStream(fileStream);
        out.writeDouble(x);
        out.writeDouble(y);
        out.writeDouble(z);
        out.writeUTF(worldName);
    }
    catch (Throwable t)
    {
        return false;
    }
    finally
    {
        try
        {
            if (out != null) out.close();
            if (fileStream != null) fileStream.close();
        }
        catch (Throwable t)
        {
            return false;
        }
    }

    return true;
}

public static Object[] readLocation(File file)
{
    FileInputStream fileStream = null;
    DataInputStream in = null;

    double x;
    double y;
    double z;
    String worldName;

    try
    {
        fileStream = new FileInputStream(file);
        in = new DataInputStream(fileStream);
        x = in.readDouble();
        y = in.readDouble();
        z = in.readDouble();
        worldName = in.readUTF();
    }
    catch (Throwable t)
    {
        x = Double.NaN;
        y = Double.NaN;
        z = Double.NaN;
        worldName = "UNKNOUN";
    }
    finally
    {
        try
        {
            if (in != null) in.close();
            if (fileStream != null) fileStream.close();
        }
        catch (Throwable t)
        {
            x = Double.NaN;
            y = Double.NaN;
            z = Double.NaN;
            worldName = "UNKNOUN";
        }
    }

    return new Object[] { x, y, z, worldName };
}
Вот спасибо.
 
Java:
public static boolean writeLocation(double x, double y, double z, String worldName, File file)
{
    FileOutputStream fileStream = null;
    DataOutputStream out = null;

    try
    {
        fileStream = new FileOutputStream(file);
        out = new DataOutputStream(fileStream);
        out.writeDouble(x);
        out.writeDouble(y);
        out.writeDouble(z);
        out.writeUTF(worldName);
    }
    catch (Throwable t)
    {
        return false;
    }
    finally
    {
        try
        {
            if (out != null) out.close();
            if (fileStream != null) fileStream.close();
        }
        catch (Throwable t)
        {
            return false;
        }
    }

    return true;
}

public static Object[] readLocation(File file)
{
    FileInputStream fileStream = null;
    DataInputStream in = null;

    double x;
    double y;
    double z;
    String worldName;

    try
    {
        fileStream = new FileInputStream(file);
        in = new DataInputStream(fileStream);
        x = in.readDouble();
        y = in.readDouble();
        z = in.readDouble();
        worldName = in.readUTF();
    }
    catch (Throwable t)
    {
        x = Double.NaN;
        y = Double.NaN;
        z = Double.NaN;
        worldName = "UNKNOUN";
    }
    finally
    {
        try
        {
            if (in != null) in.close();
            if (fileStream != null) fileStream.close();
        }
        catch (Throwable t)
        {
            x = Double.NaN;
            y = Double.NaN;
            z = Double.NaN;
            worldName = "UNKNOUN";
        }
    }

    return new Object[] { x, y, z, worldName };
}
Как все громоздко.
 
Java:
public static boolean writeLocation(double x, double y, double z, String worldName, File file)
{
    FileOutputStream fileStream = null;
    DataOutputStream out = null;

    try
    {
        fileStream = new FileOutputStream(file);
        out = new DataOutputStream(fileStream);
        out.writeDouble(x);
        out.writeDouble(y);
        out.writeDouble(z);
        out.writeUTF(worldName);
    }
    catch (Throwable t)
    {
        return false;
    }
    finally
    {
        try
        {
            if (out != null) out.close();
            if (fileStream != null) fileStream.close();
        }
        catch (Throwable t)
        {
            return false;
        }
    }

    return true;
}

public static Object[] readLocation(File file)
{
    FileInputStream fileStream = null;
    DataInputStream in = null;

    double x;
    double y;
    double z;
    String worldName;

    try
    {
        fileStream = new FileInputStream(file);
        in = new DataInputStream(fileStream);
        x = in.readDouble();
        y = in.readDouble();
        z = in.readDouble();
        worldName = in.readUTF();
    }
    catch (Throwable t)
    {
        x = Double.NaN;
        y = Double.NaN;
        z = Double.NaN;
        worldName = "UNKNOUN";
    }
    finally
    {
        try
        {
            if (in != null) in.close();
            if (fileStream != null) fileStream.close();
        }
        catch (Throwable t)
        {
            x = Double.NaN;
            y = Double.NaN;
            z = Double.NaN;
            worldName = "UNKNOUN";
        }
    }

    return new Object[] { x, y, z, worldName };
}
try with resources для кого придумали?
 

Поделиться этой страницей

Назад
Верх