Guest User

Untitled

a guest
Mar 1st, 2017
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.07 KB | None | 0 0
  1.  
  2.  
  3. import java.io.*;
  4. import java.util.ArrayList;
  5.  
  6. public class writeToFile {
  7.  
  8.     static String userName, passWord;
  9.  
  10.     public static void getCredentials() throws IOException {
  11.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  12.         System.out.println("Username :");
  13.         userName = br.readLine();
  14.  
  15.         System.out.println("Password :");
  16.         passWord = br.readLine();
  17.  
  18.         if (userName.equals("") || passWord.equals("")) {
  19.             System.out.println("Enter username and password!");
  20.             getCredentials();
  21.         }
  22.  
  23.     }
  24.  
  25.     public static void saveCredentials() throws IOException {
  26.  
  27.         File someFile = new File("D://AppCr//credentials.txt");
  28.         FileWriter writerMou = new FileWriter(someFile);
  29.         writerMou.write(userName);
  30.         writerMou.write(passWord);
  31.         writerMou.close();
  32.  
  33.     }
  34.  
  35.     public static void readFile(String path) throws IOException {
  36.         File myFile = new File(path);
  37.         FileReader fr = new FileReader(myFile);
  38.         BufferedReader br = new BufferedReader(fr);
  39.         String s = br.readLine();
  40.        
  41.         System.out.println(s);
  42.         fr.close();
  43.     }
  44.  
  45.     public static void getFiles() throws IOException {
  46.         String name = "";
  47.         ArrayList<String> fileNames = new ArrayList<String>();
  48.         File[] files = new File("C://Users//i2sbsBang003//Desktop//New folder").listFiles();
  49.         int len = files.length;
  50.  
  51.         for (int i = 0; i < len; i++) {
  52.             name = files[i].getName();
  53.             fileNames.add(name);
  54.  
  55.             // System.out.println(files[i] + " " + i);
  56.         }
  57.         // System.out.println(fileNames);
  58.  
  59.         System.out.println("File Name :");
  60.         BufferedReader buRe = new BufferedReader(new InputStreamReader(System.in));
  61.         String userInp = buRe.readLine();
  62.         boolean boo = fileNames.contains(userInp);
  63.         if (boo == true) {
  64.             int idx = fileNames.indexOf(userInp);
  65.             // System.out.println(idx);
  66.             String path = files[idx].getPath();
  67.             // System.out.println(path);
  68.             readFile(path);
  69.             float fileSize = files[idx].length();
  70.             System.out.println("File Size :" + fileSize);
  71.         }
  72.     }
  73.  
  74.     public static void main(String[] args) throws IOException {
  75.  
  76.         getCredentials();
  77.         saveCredentials();
  78.         getFiles();
  79.     }
  80. }
Add Comment
Please, Sign In to add comment