Auf Thema antworten

Das finde ich sogar noch flexibler, wenn z. B. ein Parameter in der Mitte hinzukommt. Da hätte man mit dem Array grobe Probleme.


[code=Java]

package javaforum;


import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

import java.util.Map.Entry;

import java.util.regex.Pattern;


public class SaveNLoad

{

    private static final String SAVE_ARGUMENT = "username=%s\npassword=%s\nage=%s";

    private static final int NEEDING_ARGUMENTS;

   

    static

    {

        int j = 0;

        for(int i = 0, size = SAVE_ARGUMENT.length() - 1; i < size; i++)

        {

            if(SAVE_ARGUMENT.charAt(i) == '%' && SAVE_ARGUMENT.charAt(i + 1) == 's')

                j++;

        }

        NEEDING_ARGUMENTS = j;

    }

    private static void write(String path, String... args)

    {

        write(new File(path), args);

    }

    private static void write(File file, String... args)

    {

        if(args.length != NEEDING_ARGUMENTS)

            throw new IllegalArgumentException("Unexceped count of arguments: " + NEEDING_ARGUMENTS + " excepted.");

       

        if(!file.exists())

        {

            try

            {

                file.createNewFile();

            }

            catch (IOException e)

            {

                e.printStackTrace();

            }

        }

       

        BufferedWriter bos = null;

        try

        {

            bos = new BufferedWriter(new FileWriter(file));

            bos.write(String.format(SAVE_ARGUMENT, (Object[])args));

        }

        catch (Exception e)

        {

            e.printStackTrace();

        }

        finally

        {

            try

            {

                if(bos != null)

                {

                    bos.flush();

                    bos.close();

                }

            }

            catch (IOException e)

            {

                e.printStackTrace();

            }

        }

    }

    private static Map<String, String> read(String path)

    {

        Map<String, String> map = new HashMap<String, String>();

       

        BufferedReader br = null;

        Pattern pattern = Pattern.compile("=");

        try

        {

            br = new BufferedReader(new FileReader(path));

            for(String line = null; (line = br.readLine()) != null;)

            {

                String[] split = pattern.split(line, 0);

                if(split.length != 2)

                    throw new IOException("Illegal Line: " + line);

               

                map.put(split[0], split[1]);

            }

        }

        catch (IOException e)

        {

            e.printStackTrace();

        }

        finally

        {

            try

            {

                if(br != null)

                    br.close();

            }

            catch (IOException e)

            {

                e.printStackTrace();

            }

        }

        return(map);

    }

    public static void main(String[] args)

    {

        write("volvagia", "volvagia", "1A1DC91C907325C69271DDF0C944BC72", "21");

        Map<String, String> map = read("volvagia");

       

        for(Entry<String, String> entry:map.entrySet())

            System.out.println(entry.getKey() + " = " + entry.getValue());

    }

}

[/code]



Oben