Jump to content

Generic Bot settings snippet [EASY]


Aces_

Recommended Posts

Presenting you easy generic SAVE/LOAD implementation, feel free to use :

 

 (some of the Load and Save class implementations are copypasted from help section)

 

Save Class:

import java.io.*;
import java.util.Base64;

public class Save {
    
    private PrintWriter pw;

    String localDir = System.getProperty("user.home") + File.separator + "OSBot" + File.separator + "data" + File.separator;

    public Save(String localName) {
        try {
            pw = new PrintWriter(localDir + localName + ".ini");
            System.out.println(pw.toString() + "\n" + localDir);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public boolean writeString(String s) {
        pw.println(s);
        pw.close();
        return true;
    }

    public boolean writeObject(Serializable o) throws IOException {
        writeString(toString(o));
        System.out.println(toString(o));
        return true;
    }

    private static String toString(Serializable o) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(o);
        oos.close();
        return Base64.getEncoder().encodeToString(baos.toByteArray());
    }
}

Load class:

import java.io.*;
import java.nio.charset.Charset;
import java.util.Base64;

public class Load {

    private BufferedReader br;
    private InputStream fis;
    private InputStreamReader isr;
    private boolean finished = false;

    String localDir = System.getProperty("user.home") + File.separator + "OSBot" + File.separator + "data" + File.separator;

    public Load(String localName) {
        try {
            fis = new FileInputStream(localDir + localName + ".ini");
            isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
            br = new BufferedReader(isr);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public String readLine() throws IOException {
        String line = null;
        if (!finished) {
            line = br.readLine();
            if (line == null) finished = true;
        }
        else {
            if (br != null) {
                br.close();
                fis.close();
                isr.close();
                br = null;
            }
        }
        return line;
    }


    public Object readObject() throws IOException, ClassNotFoundException {
        return fromString(readLine());
    }

    private static Object fromString( String s ) throws IOException ,
            ClassNotFoundException {
        byte [] data = Base64.getDecoder().decode( s );
        ObjectInputStream ois = new ObjectInputStream(
                new ByteArrayInputStream(  data ) );
        Object o  = ois.readObject();
        ois.close();
        return o;
    }
}

How it works?

 

 

FOR SAVING

All you have to do is create a Save class that you are going to use:

Save save = new Save("Filename");

And saving class that contains information about your bot:

//make sure that your Properties class implements "Serializable"
Properties propertiesofBot = new Properties("My properties class");

save.writeObject(propertiesofBot);

FOR LOADING:

//Make sure you create Load class with same name as the Save class
Load load = new Load("Filename");  

And all you need to do is load and save the class to the variable

Properties properties = (Properties) load.readObject();

Saving or loading classes that differ is not supported.

 

Hope this helps! smile.png

Edited by Aces_
  • Like 3
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...