Advertisement
romancha

FifthFile

Mar 15th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.RandomAccessFile;
  3.  
  4. public class AccountingUser {
  5.     RandomAccessFile file;
  6.  
  7.     public AccountingUser(String file) throws IOException{
  8.         this.file = new RandomAccessFile(file, "rw");
  9.     }
  10.  
  11.     public AccountingUser() throws IOException{
  12.         this("users.txt");
  13.     }
  14.  
  15.     public void testUsers(String name) throws IOException{
  16.         String line;
  17.         while ((line = file.readLine()) != null) {
  18.             if(line.contains(name)) {
  19.                 String[] args = line.split(":");
  20.                 int count = Integer.parseInt(args[1]);
  21.                 file.seek(file.getFilePointer() - line.length() - 1);
  22.                 file.writeBytes(args[0] + ":" + ++count + "\n");
  23.             }
  24.         }
  25.     }
  26.  
  27.     public void printFile() throws IOException{
  28.         String line;
  29.         while ((line = file.readLine()) != null) {
  30.             System.out.println(line);
  31.         }
  32.     }
  33.  
  34.     @Override
  35.     protected void finalize() throws Throwable {
  36.         file.close();
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement