Advertisement
Guest User

Methods to my monitor

a guest
Dec 25th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.io.RandomAccessFile;
  10. import java.nio.channels.FileChannel;
  11. import java.nio.channels.FileLock;
  12.  
  13. public class Methods {
  14.     static String path = System.getProperty("user.home") + File.separator
  15.             + "OSBot" + File.separator + "data" + File.separator + "btm.txt";
  16.  
  17.     /*
  18.      * Method needs to be used like so ScriptName,Accname,XP Gained,Xp-per-hour
  19.      */
  20.  
  21.     public static void addAccount(String setting)
  22.             throws IOException {
  23.         File file = new File(path);
  24.         FileWriter f = new FileWriter(file, true);
  25.  
  26.         RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
  27.         FileChannel channel = randomAccessFile.getChannel();
  28.         FileLock lock = channel.lock();
  29.         f.write(setting + System.getProperty("line.separator"));
  30.         lock.release();
  31.         channel.close();
  32.         randomAccessFile.close();
  33.         f.close();
  34.     }
  35.  
  36.     public static boolean statsAreValid() {
  37.         return (new File((path)).exists());
  38.     }
  39.  
  40.     public static void removeAccount(String acc) throws IOException {
  41.         if (statsAreValid()) {
  42.             FileInputStream fstream = new FileInputStream(path);
  43.             BufferedReader br = new BufferedReader(new InputStreamReader(
  44.                     fstream));
  45.             String strLine;
  46.             StringBuilder fileContent = new StringBuilder();
  47.             while ((strLine = br.readLine()) != null) {
  48.                 String lines[] = strLine.split(",");
  49.                 if (lines[1].equalsIgnoreCase(acc)) {
  50.                     fileContent.append("");
  51.                 } else {
  52.                     fileContent.append(strLine + System.lineSeparator());
  53.                 }
  54.             }
  55.  
  56.             FileWriter fstreamWrite = new FileWriter((path));
  57.             BufferedWriter out = new BufferedWriter(fstreamWrite);
  58.             out.write(fileContent.toString());
  59.             out.close();
  60.             br.close();
  61.  
  62.         }
  63.     }
  64.  
  65.     public static void updateAccount(String account_name, String settings)
  66.             throws IOException {
  67.         FileInputStream fstream = new FileInputStream(path);
  68.         BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
  69.         String strLine;
  70.         StringBuilder fileContent = new StringBuilder();
  71.         while ((strLine = br.readLine()) != null) {
  72.             String lines[] = strLine.split(",");
  73.             if (lines[1].equalsIgnoreCase(account_name)) {
  74.                 String newLine = settings;
  75.                 fileContent.append(newLine + System.lineSeparator());
  76.             } else {
  77.                 fileContent.append(strLine + System.lineSeparator());
  78.             }
  79.         }
  80.  
  81.         FileWriter fstreamWrite = new FileWriter((path));
  82.         BufferedWriter out = new BufferedWriter(fstreamWrite);
  83.         out.write(fileContent.toString());
  84.         out.close();
  85.         br.close();
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement