Advertisement
Guest User

Untitled

a guest
Dec 25th, 2013
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.19 KB | None | 0 0
  1. package backend;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileReader;
  8. import java.io.FileWriter;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. import java.io.RandomAccessFile;
  12. import java.nio.channels.FileChannel;
  13. import java.nio.channels.FileLock;
  14. import java.util.ArrayList;
  15.  
  16. import ui.Error_Screen;
  17.  
  18. public class Methods {
  19.     static String path = System.getProperty("user.home") + File.separator
  20.             + "OSBot" + File.separator + "data" + File.separator + "btm.txt";
  21.  
  22.     public static String[] getAccountNames() throws Throwable {
  23.         if (statsAreValid()) {
  24.             return readBotStats((path), 1).toArray(new String[0]);
  25.         }
  26.         return null;
  27.  
  28.     }
  29.  
  30.     public static String[] getScriptNames() throws Throwable {
  31.         if (statsAreValid()) {
  32.             return readBotStats((path), 0).toArray(new String[0]);
  33.         }
  34.         return null;
  35.  
  36.     }
  37.  
  38.     public static String[] getXpph() throws Throwable {
  39.         if (statsAreValid()) {
  40.             return readBotStats((path), 3).toArray(new String[0]);
  41.         }
  42.         return null;
  43.     }
  44.  
  45.     public static String[] getXp() throws Throwable {
  46.         if (statsAreValid()) {
  47.             return readBotStats((path), 2).toArray(new String[0]);
  48.         }
  49.         return null;
  50.     }
  51.  
  52.     /*
  53.      * The path can be found in the writing method but also you need to add ","
  54.      * after String so name+","+time+","+xpgained;
  55.      */
  56.  
  57.     public static ArrayList<String> readBotStats(String Path, int index)
  58.             throws Throwable {
  59.         if (new File(Path).exists()) {
  60.             BufferedReader br = new BufferedReader(new FileReader(
  61.                     new File(Path)));
  62.             ArrayList<String> settings = new ArrayList<String>();
  63.             String line;
  64.             while ((line = br.readLine()) != null) {
  65.                 String tokens[] = (line.split(",", 4));
  66.                 settings.add(tokens[index].trim());
  67.             }
  68.             br.close();
  69.             return settings;
  70.         }
  71.         Error_Screen.showScreen();
  72.         return null;
  73.     }
  74.  
  75.     /*
  76.      * Method needs to be used like so Account name,Timerunning,Xp-Gained
  77.      */
  78.  
  79.     public static void addAccount(String... settings)
  80.             throws IOException {
  81.         File file = new File(path);
  82.         FileWriter f = new FileWriter(file, true);
  83.  
  84.         RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
  85.         FileChannel channel = randomAccessFile.getChannel();
  86.         FileLock lock = channel.lock();
  87.         for (String setting : settings) {
  88.             f.write(setting + System.getProperty("line.separator"));
  89.  
  90.         }
  91.         lock.release();
  92.         channel.close();
  93.         randomAccessFile.close();
  94.         f.close();
  95.     }
  96.  
  97.     public static boolean statsAreValid() {
  98.         return (new File((path)).exists());
  99.     }
  100.  
  101.     public static File getFile() {
  102.         if (statsAreValid())
  103.             return (new File((path)));
  104.         return null;
  105.     }
  106.  
  107.     public static void removeAccount(String acc) throws IOException {
  108.         if (statsAreValid()) {
  109.             FileInputStream fstream = new FileInputStream(path);
  110.             BufferedReader br = new BufferedReader(new InputStreamReader(
  111.                     fstream));
  112.             String strLine;
  113.             StringBuilder fileContent = new StringBuilder();
  114.             while ((strLine = br.readLine()) != null) {
  115.                 String lines[] = strLine.split(",");
  116.                 if (lines[1].equalsIgnoreCase(acc)) {
  117.                     fileContent.append("");
  118.                 } else {
  119.                     fileContent.append(strLine + System.lineSeparator());
  120.                 }
  121.             }
  122.  
  123.             FileWriter fstreamWrite = new FileWriter((path));
  124.             BufferedWriter out = new BufferedWriter(fstreamWrite);
  125.             out.write(fileContent.toString());
  126.             out.close();
  127.             br.close();
  128.  
  129.         }
  130.     }
  131.  
  132.     public static void updateAccount(String account_name, String settings)
  133.             throws IOException {
  134.         FileInputStream fstream = new FileInputStream(path);
  135.         BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
  136.         String strLine;
  137.         StringBuilder fileContent = new StringBuilder();
  138.         while ((strLine = br.readLine()) != null) {
  139.             String lines[] = strLine.split(",");
  140.             if (lines[1].equalsIgnoreCase(account_name)) {
  141.                 String newLine = settings;
  142.                 fileContent.append(newLine + System.lineSeparator());
  143.             } else {
  144.                 fileContent.append(strLine + System.lineSeparator());
  145.             }
  146.         }
  147.  
  148.         FileWriter fstreamWrite = new FileWriter((path));
  149.         BufferedWriter out = new BufferedWriter(fstreamWrite);
  150.         out.write(fileContent.toString());
  151.         out.close();
  152.         br.close();
  153.     }
  154.  
  155.  
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement