Facebook
From TWOJA STARA, 6 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 195
  1. import java.io.*;
  2.  
  3. public class FileManager{
  4.     //Tworzenie folderu
  5.     public static void UtworzFolder(String path)
  6.     {
  7.         boolean sukces = (new File(path)).mkdirs();
  8.         if (!sukces) {
  9.             System.out.append("Folder już istnieje!\n");
  10.         }
  11.         else {
  12.             System.out.append("Folder został utworzony!");
  13.         }
  14.     }
  15.  
  16.     //Tworzenie pliku
  17.     public static void UtworzPlik(String path)
  18.     {
  19.         try {
  20.             File file = new File(path);
  21.             if (file.createNewFile()){
  22.                 System.out.println("Plik został utworzony!");
  23.             }
  24.             else {
  25.                 System.out.println("Plik już istnieje!");
  26.             }
  27.         } catch (IOException e){
  28.             e.printStackTrace();
  29.         }
  30.     }
  31.  
  32.     //Sprawdzanie czy plik istnieje
  33.     public static boolean SprawdzCzyIstnieje(String path)
  34.     {
  35.         File f = new File(path);
  36.         if (f.exists()){
  37.             System.out.println("Plik istnieje.");
  38.             return true;
  39.         }
  40.         else {
  41.             System.out.println("Plik nie istnieje.");
  42.             return false;
  43.         }
  44.     }
  45.  
  46.     //Skasowanie pliku
  47.     public static void SkasujPlik(String path)
  48.     {
  49.         try{
  50.         File f = new File(path);
  51.         if (f.delete()){
  52.             System.out.println("Plik " + f.getName() + " został skasowany!");
  53.         }
  54.         else {
  55.             System.out.println("Operacja kasowania nie powiodła się. Spróbuj ponownie.");
  56.         }
  57.         } catch (Exception e){
  58.             e.printStackTrace();
  59.         }
  60.     }
  61.  
  62.     //Zmiana nazwy pliku
  63.     public static void  ZmienNazwe(String oldPath, String newPath)
  64.     {
  65.         File oldfile = new File(oldPath);
  66.         File newfile = new File(newPath);
  67.  
  68.         if (oldfile.renameTo(newfile)){
  69.             System.out.println("Udało się zmienić nazwę pliku.");
  70.         }
  71.         else{
  72.             System.out.println("Nie udało zmienić się nazwy pliku!");
  73.         }
  74.     }
  75.  
  76.     //Kopiowanie pliku
  77.     public static void KopiujPlik(String path1, String path2)
  78.     {
  79.         InputStream inStream = null;
  80.         OutputStream outStream = null;
  81.         try {
  82.             File afile = new File(path1);
  83.             File bfile = new File(path2);
  84.             inStream = new FileInputStream(afile);
  85.             outStream = new FileOutputStream(bfile);
  86.             byte[] buffer = new byte[1024];
  87.             int len;
  88.             while ((len = inStream.read(buffer)) > 0) {
  89.                 outStream.write(buffer, 0, len);
  90.             }
  91.             inStream.close();
  92.             outStream.close();
  93.             System.out.println("Plik został skopiowany.");
  94.         } catch (IOException e){
  95.             e.printStackTrace();
  96.         }
  97.     }
  98.  
  99.     //
  100.     public static void main(String args[]) throws IOException
  101.     {
  102.         while(true)
  103.         {
  104.             try{
  105.                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  106.                 String s = br.readLine();
  107.  
  108.                 switch (s)
  109.                 {
  110.                     case "1":
  111.                         System.out.append("1: Utworzenie folderu\n");
  112.                         UtworzFolder("c:\\JavaIOTest\\");
  113.                         break;
  114.                 }
  115.             } catch (IOException e){
  116.                 //TODO Auto-generated catch block e.printStackTrace();
  117.             }
  118.         }
  119.     }
  120. }