28
1 yeni bir proje oluşturduk 2 dosya seçme oluşturma kopyalama taşıma silme işlemlerine bakacağız 3 klasör seçme oluşturma kopyalama taşıma silme işlemlerine bakacağız 4 dosya seç metodu oluşturduk 5 string değer döndürecek 6 static metod olacak ve public olacak 7 public olduğundan her taraftan kullanılabilir 8 static olduğundan nesne olmadan kullanılabilir 9 10 package javadosyaislemleri; 11 12 public class Test { 13 14 public static void main(String[] args){ 15 16 } 17 public static String dosyaSec(){ 18 19 } 20 } 21 22 23 jfilechooser sınıfı ile dosya seçim işlemlerini yaparız 24 jfilechooser sınıfından nesne oluşturduk 25 26 package javadosyaislemleri; 27 28 import javax.swing.JFileChooser; 29 30 public class Test { 31 32 public static void main(String[] args){ 33 34 } 35 36 public static String dosyaSec() { 37 38 JFileChooser jFileChooser = new JFileChooser(); 39 40 41 } 42 } 43 44 45 setcurrentdicrectory ile nereden dosya seçme penceresinin açılacağını ayarlarız 46 file tipinde parametre alır 47 new file parametresini verdik 48 . ile bulunduğumuz klaörde aç dedik 49 50 package javadosyaislemleri; 51 52 import java.io.File; 53 import javax.swing.JFileChooser; 54 55 public class Test { 56 57 public static void main(String[] args){ 58 59 } 60 61 public static String dosyaSec() { 62 63 JFileChooser jFileChooser = new JFileChooser(); 64 jFileChooser.setCurrentDirectory(new File(".")); 65 66 } 67 } 68 69 70 setdialog ile açılacak seçim penceresinin başlığını belirleriz 71 72 package javadosyaislemleri; 73

WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

  • Upload
    others

  • View
    11

  • Download
    0

Embed Size (px)

Citation preview

Page 1: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

1 yeni bir proje oluşturduk2 dosya seçme oluşturma kopyalama taşıma silme işlemlerine bakacağız3 klasör seçme oluşturma kopyalama taşıma silme işlemlerine bakacağız4 dosya seç metodu oluşturduk5 string değer döndürecek6 static metod olacak ve public olacak7 public olduğundan her taraftan kullanılabilir8 static olduğundan nesne olmadan kullanılabilir9

10 package javadosyaislemleri;11 12 public class Test {13 14 public static void main(String[] args) {15 16 }17 public static String dosyaSec(){18 19 }20 }21 22 23 jfilechooser sınıfı ile dosya seçim işlemlerini yaparız24 jfilechooser sınıfından nesne oluşturduk25 26 package javadosyaislemleri;27 28 import javax.swing.JFileChooser;29 30 public class Test {31 32 public static void main(String[] args) {33 34 }35 36 public static String dosyaSec() {37 38 JFileChooser jFileChooser = new JFileChooser();39 40 41 }42 }43 44 45 setcurrentdicrectory ile nereden dosya seçme penceresinin açılacağını ayarlarız46 file tipinde parametre alır47 new file parametresini verdik48 . ile bulunduğumuz klaörde aç dedik49 50 package javadosyaislemleri;51 52 import java.io.File;53 import javax.swing.JFileChooser;54 55 public class Test {56 57 public static void main(String[] args) {58 59 }60 61 public static String dosyaSec() {62 63 JFileChooser jFileChooser = new JFileChooser();64 jFileChooser.setCurrentDirectory(new File("."));65 66 }67 }68 69 70 setdialog ile açılacak seçim penceresinin başlığını belirleriz71 72 package javadosyaislemleri;73

Page 2: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

74 import java.io.File;75 import javax.swing.JFileChooser;76 77 public class Test {78 79 public static void main(String[] args) {80 81 }82 83 public static String dosyaSec() {84 85 JFileChooser jFileChooser = new JFileChooser();86 jFileChooser.setCurrentDirectory(new File("."));87 jFileChooser.setDialogTitle("lusfen dosya seciniz");88 89 }90 }91 92 93 setfileselectionmode ile ne seçileceğini belirleyebiliriz94 parametre olarak jfilechooser files only ile dosya seç dedik95 96 package javadosyaislemleri;97 98 import java.io.File;99 import javax.swing.JFileChooser;

100 101 public class Test {102 103 public static void main(String[] args) {104 105 }106 107 public static String dosyaSec() {108 109 JFileChooser jFileChooser = new JFileChooser();110 jFileChooser.setCurrentDirectory(new File("."));111 jFileChooser.setDialogTitle("lusfen dosya seciniz");112 jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);113 }114 }115 116 117 jfilechooser nesnemizin showopendialog değeri ile penceremizi açarız118 int değer döndürür119 jfilechooser approve option ile seçim yapıldı ise birden büyük değer döner120 121 122 123 if else ile bu değere göre kontrol ettik124 125 package javadosyaislemleri;126 127 import java.io.File;128 import javax.swing.JFileChooser;129 130 public class Test {131 132 public static void main(String[] args) {133 134 }135 136 public static String dosyaSec() {137 138 JFileChooser jFileChooser = new JFileChooser();139 jFileChooser.setCurrentDirectory(new File("."));140 jFileChooser.setDialogTitle("lusfen dosya seciniz");141 jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);142 143 int donendeger = jFileChooser.showOpenDialog(null);144 145 if (donendeger == JFileChooser.APPROVE_OPTION) {146

Page 3: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

147 } else {148 }149 150 }151 }152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

getabsolutepath ile adres yolunu döndürürüz155 156 package javadosyaislemleri;157 158 import java.io.File;159 import javax.swing.JFileChooser;160 161 public class Test {162 163 public static void main(String[] args) {164 165 }166 167 public static String dosyaSec() {168 169 JFileChooser jFileChooser = new JFileChooser();170 jFileChooser.setCurrentDirectory(new File("."));171 jFileChooser.setDialogTitle("lusfen dosya seciniz");172 jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);173 174 int donendeger = jFileChooser.showOpenDialog(null);175 176 if (donendeger == JFileChooser.APPROVE_OPTION) {177 return jFileChooser.getSelectedFile().getAbsolutePath();178 } else {179 }180 181 }182 }183 eğer dosya seçilmemişse else alanından return null döndürülür184 package javadosyaislemleri;185 186 import java.io.File;187 import javax.swing.JFileChooser;188 189 public class Test {190 191 public static void main(String[] args) {192 193 }194 195 public static String dosyaSec() {196 197 JFileChooser jFileChooser = new JFileChooser();198 jFileChooser.setCurrentDirectory(new File("."));199 jFileChooser.setDialogTitle("lusfen dosya seciniz");200 jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);201 202 int donendeger = jFileChooser.showOpenDialog(null);203 204 if (donendeger == JFileChooser.APPROVE_OPTION) {205 return jFileChooser.getSelectedFile().getAbsolutePath();206 } else {207 return null;208 }209 210 }211 }212 213 214 dosya seçebilirir ancak klasör seçemeyiz215 metodu kullanalım216 eğer dönen değer null değilse seçilen dosyanın path adresini yazdıralım217 218 package javadosyaislemleri;

Page 4: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

219 220 import java.io.File;221 import javax.swing.JFileChooser;222 223 public class Test {224 225 public static void main(String[] args) {226 String secilendosya = Test.dosyaSec();227 if (secilendosya != null) {228 System.out.println(secilendosya);229 } else {230 System.out.println("dosya secmediniz");231 }232 }233 234 public static String dosyaSec() {235 236 JFileChooser jFileChooser = new JFileChooser();237 jFileChooser.setCurrentDirectory(new File("."));238 jFileChooser.setDialogTitle("lusfen dosya seciniz");239 jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);240 241 int donendeger = jFileChooser.showOpenDialog(null);242 243 if (donendeger == JFileChooser.APPROVE_OPTION) {244 return jFileChooser.getSelectedFile().getAbsolutePath();245 } else {246 return null;247 }248 249 }250 }251 252 253 eğer nereden başlayacağını belirtmeseydik belgelerimden seçmeye devam ederdi254 255 package javadosyaislemleri;256 257 import java.io.File;258 import javax.swing.JFileChooser;259 260 public class Test {261 262 public static void main(String[] args) {263 String secilendosya = Test.dosyaSec();264 if (secilendosya != null) {265 System.out.println(secilendosya);266 } else {267 System.out.println("dosya secmediniz");268 }269 }270 271 public static String dosyaSec() {272 273 JFileChooser jFileChooser = new JFileChooser();274 // jFileChooser.setCurrentDirectory(new File("."));275 jFileChooser.setDialogTitle("lusfen dosya seciniz");276 jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);277 278 int donendeger = jFileChooser.showOpenDialog(null);279 280 if (donendeger == JFileChooser.APPROVE_OPTION) {281 return jFileChooser.getSelectedFile().getAbsolutePath();282 } else {283 return null;284 }285 286 }287 }288 eğer belirli bir klasörden aramaya başlamak istiyorsak belirtmemiz lazım289 örneğim masaüstünden aramaya başlamak için file ile göstermemiz lazım290 291 package javadosyaislemleri;

Page 5: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

292 293 import java.io.File;294 import javax.swing.JFileChooser;295 296 public class Test {297 298 public static void main(String[] args) {299 String secilendosya = Test.dosyaSec();300 if (secilendosya != null) {301 System.out.println(secilendosya);302 } else {303 System.out.println("dosya secmediniz");304 }305 }306 307 public static String dosyaSec() {308 309 JFileChooser jFileChooser = new JFileChooser();310 jFileChooser.setCurrentDirectory(new File("C:\\Users\\(DESSAS)\\Desktop\\"));311 jFileChooser.setDialogTitle("lusfen dosya seciniz");312 jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);313 314 int donendeger = jFileChooser.showOpenDialog(null);315 316 if (donendeger == JFileChooser.APPROVE_OPTION) {317 return jFileChooser.getSelectedFile().getAbsolutePath();318 } else {319 return null;320 }321 322 }323 }324 325 326 şimdide klasör seçelim327 bunun İçin bir metod oluşturduk328 string değer döndürdü329 330 package javadosyaislemleri;331 332 import java.io.File;333 import javax.swing.JFileChooser;334 335 public class Test {336 337 public static void main(String[] args) {338 String secilendosya = Test.dosyaSec();339 if (secilendosya != null) {340 System.out.println(secilendosya);341 } else {342 System.out.println("dosya secmediniz");343 }344 }345 346 public static String dosyaSec() {347 348 JFileChooser jFileChooser = new JFileChooser();349 jFileChooser.setCurrentDirectory(new File("C:\\Users\\(DESSAS)\\Desktop\\"));350 jFileChooser.setDialogTitle("lusfen dosya seciniz");351 jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);352 353 int donendeger = jFileChooser.showOpenDialog(null);354 355 if (donendeger == JFileChooser.APPROVE_OPTION) {356 return jFileChooser.getSelectedFile().getAbsolutePath();357 } else {358 return null;359 }360 361 }362 363 public static String klasorSec(){364

Page 6: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

365 }366 367 }368 369 370 jFileChooser nesnesi oluşturduk371 setCurrentDirectory ile başlayacağı konumu seçtik372 new file . ile bulunduğu klasörden aç dedik373 proje dosyasının olduğu klasörden seçim penceresi açılacak374 package javadosyaislemleri;375 376 import java.io.File;377 import javax.swing.JFileChooser;378 379 public class Test {380 381 public static void main(String[] args) {382 String secilendosya = Test.dosyaSec();383 if (secilendosya != null) {384 System.out.println(secilendosya);385 } else {386 System.out.println("dosya secmediniz");387 }388 }389 390 public static String dosyaSec() {391 392 JFileChooser jFileChooser = new JFileChooser();393 jFileChooser.setCurrentDirectory(new File("C:\\Users\\(DESSAS)\\Desktop\\"));394 jFileChooser.setDialogTitle("lusfen dosya seciniz");395 jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);396 397 int donendeger = jFileChooser.showOpenDialog(null);398 399 if (donendeger == JFileChooser.APPROVE_OPTION) {400 return jFileChooser.getSelectedFile().getAbsolutePath();401 } else {402 return null;403 }404 405 }406 407 public static String klasorSec(){408 JFileChooser jFileChooser=new JFileChooser(new File("."));409 }410 411 }412 413 setDialogTitle ile başlık alanını verdik414 setFileSelectionMode metoduna jFileChooser directoriesonly dedik415 directoriesonly ile klasör seçmeyi ayarlamış olduk416 jFileChooser nesnesini showOpenDialog ile açtık417 418 package javadosyaislemleri;419 420 import java.io.File;421 import javax.swing.JFileChooser;422 423 public class Test {424 425 public static void main(String[] args) {426 String secilendosya = Test.dosyaSec();427 if (secilendosya != null) {428 System.out.println(secilendosya);429 } else {430 System.out.println("dosya secmediniz");431 }432 }433 434 public static String dosyaSec() {435 436 JFileChooser jFileChooser = new JFileChooser();437 jFileChooser.setCurrentDirectory(new File("C:\\Users\\(DESSAS)\\Desktop\\"));

Page 7: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

438 jFileChooser.setDialogTitle("lusfen dosya seciniz");439 jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);440 441 int donendeger = jFileChooser.showOpenDialog(null);442 443 if (donendeger == JFileChooser.APPROVE_OPTION) {444 return jFileChooser.getSelectedFile().getAbsolutePath();445 } else {446 return null;447 }448 449 }450 451 public static String klasorSec(){452 JFileChooser jFileChooser=new JFileChooser(new File("."));453 jFileChooser.setDialogTitle("klasör seçinir");454 jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);455 456 }457 458 }459 460 showOpenDialog dan dönen değeri if ile kontrol ettik461 eğer jFileChooser .APPROVE_OPTION değerine eşitse462 return ile jFileChooser nesnesinin getSelectedFile getAbsolutePath ifadenini döndürdük463 değilse null değer döndürdük464 465 package javadosyaislemleri;466 467 import java.io.File;468 import javax.swing.JFileChooser;469 470 public class Test {471 472 public static void main(String[] args) {473 String secilendosya = Test.dosyaSec();474 if (secilendosya != null) {475 System.out.println(secilendosya);476 } else {477 System.out.println("dosya secmediniz");478 }479 }480 481 public static String dosyaSec() {482 483 JFileChooser jFileChooser = new JFileChooser();484 jFileChooser.setCurrentDirectory(new File("C:\\Users\\(DESSAS)\\Desktop\\"));485 jFileChooser.setDialogTitle("lusfen dosya seciniz");486 jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);487 488 int donendeger = jFileChooser.showOpenDialog(null);489 490 if (donendeger == JFileChooser.APPROVE_OPTION) {491 return jFileChooser.getSelectedFile().getAbsolutePath();492 } else {493 return null;494 }495 496 }497 498 public static String klasorSec() {499 JFileChooser jFileChooser = new JFileChooser(new File("."));500 jFileChooser.setDialogTitle("klasör seçinir");501 jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);502 503 int notlar = jFileChooser.showOpenDialog(null);504 505 if (notlar == JFileChooser.APPROVE_OPTION) {506 return jFileChooser.getSelectedFile().getAbsolutePath();507 } else {508 return null;509 }510

Page 8: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

511 }512 513 }514 515 main metodunda bunu kullanalım516 517 package javadosyaislemleri;518 519 import java.io.File;520 import javax.swing.JFileChooser;521 522 public class Test {523 524 public static void main(String[] args) {525 526 String dosyaYolu = Test.klasorSec();527 if (dosyaYolu != null) {528 System.out.println(dosyaYolu);529 } else {530 System.out.println("klasör secilmedi");531 }532 533 }534 535 public static String dosyaSec() {536 537 JFileChooser jFileChooser = new JFileChooser();538 jFileChooser.setCurrentDirectory(new File("C:\\Users\\(DESSAS)\\Desktop\\"));539 jFileChooser.setDialogTitle("lusfen dosya seciniz");540 jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);541 542 int donendeger = jFileChooser.showOpenDialog(null);543 544 if (donendeger == JFileChooser.APPROVE_OPTION) {545 return jFileChooser.getSelectedFile().getAbsolutePath();546 } else {547 return null;548 }549 550 }551 552 public static String klasorSec() {553 JFileChooser jFileChooser = new JFileChooser(new File("."));554 jFileChooser.setDialogTitle("klasör seçinir");555 jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);556 557 int notlar = jFileChooser.showOpenDialog(null);558 559 if (notlar == JFileChooser.APPROVE_OPTION) {560 return jFileChooser.getSelectedFile().getAbsolutePath();561 } else {562 return null;563 }564 565 }566 567 }568 569 şimdi çalıştırırsak sadece klasör seçebiliriz570 571 run:572 C:\Users\(DESSAS)\Desktop\(PROGRAM) YAZILIMLAR573 BUILD SUCCESSFUL (total time: 25 seconds)574 575 576 şimdi dosya oluşturmaya bakalım577 bir metod oluşturalım dosyaoluşturmaişlemleri578 file nesnesi alsın579 if ile file.exists deriz580 bu dosyanın var olup olmadığını kontrol eder581 eğer varsa return ile dosya var döndürelim582 eğer yoksa file.createnewfile ile oluşturelım583 oluşturunca return ile oluşturuldu yazsın

Page 9: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

584 585 package javadosyaislemleri;586 587 import java.io.File;588 import java.io.IOException;589 import java.util.logging.Level;590 import java.util.logging.Logger;591 import javax.swing.JFileChooser;592 593 public class Test {594 595 public static void main(String[] args) {596 597 String dosyaYolu = Test.klasorSec();598 if (dosyaYolu != null) {599 System.out.println(dosyaYolu);600 } else {601 System.out.println("klasör secilmedi");602 }603 604 }605 606 public static String dosyaSec() {607 608 JFileChooser jFileChooser = new JFileChooser();609 jFileChooser.setCurrentDirectory(new File("C:\\Users\\(DESSAS)\\Desktop\\"));610 jFileChooser.setDialogTitle("lusfen dosya seciniz");611 jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);612 613 int donendeger = jFileChooser.showOpenDialog(null);614 615 if (donendeger == JFileChooser.APPROVE_OPTION) {616 return jFileChooser.getSelectedFile().getAbsolutePath();617 } else {618 return null;619 }620 621 }622 623 public static String klasorSec() {624 JFileChooser jFileChooser = new JFileChooser(new File("."));625 jFileChooser.setDialogTitle("klasör seçinir");626 jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);627 628 int notlar = jFileChooser.showOpenDialog(null);629 630 if (notlar == JFileChooser.APPROVE_OPTION) {631 return jFileChooser.getSelectedFile().getAbsolutePath();632 } else {633 return null;634 }635 636 }637 638 public static String dosyaOlusturma(File file) {639 if (!file.exists()) {640 System.out.println("dosya yok ");641 try {642 if (file.createNewFile()) {643 System.out.println("dosya olusturuldu");644 return "dosya olusutuldu";645 }else{646 return "dosya olusturulamadi";647 }648 } catch (IOException ex) {649 System.out.println("dosya olusturulurken hata olustu : "+ex.toString

());650 return "dosya olusturulamadi";651 }652 } else {653 System.out.println("dosya var");654 return "dosya var";655 }

Page 10: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

656 }657 658 }659 660 661 şimdi bu metodu kullanalım662 önce dosya adını girelim663 dosya adı ile uzantısıda yazalım664 klasörü seçelim sonradan dosyamızı oluşturalım665 package javadosyaislemleri;666 667 import java.io.File;668 import java.io.IOException;669 import java.util.Scanner;670 import javax.swing.JFileChooser;671 672 public class Test {673 674 public static void main(String[] args) {675 676 Scanner scanner = new Scanner(System.in);677 System.out.println("olusturacaginiz dosyanin adini giriniz");678 String dosyaAdi = scanner.nextLine();679 680 String dosyaYolu = Test.klasorSec();681 File file = new File(dosyaYolu + "/" + dosyaAdi);682 683 Test.dosyaOlusturma(file);684 685 }686 687 public static String dosyaSec() {688 689 JFileChooser jFileChooser = new JFileChooser();690 jFileChooser.setCurrentDirectory(new File("C:\\Users\\(DESSAS)\\Desktop\\"));691 jFileChooser.setDialogTitle("lusfen dosya seciniz");692 jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);693 694 int donendeger = jFileChooser.showOpenDialog(null);695 696 if (donendeger == JFileChooser.APPROVE_OPTION) {697 return jFileChooser.getSelectedFile().getAbsolutePath();698 } else {699 return null;700 }701 702 }703 704 public static String klasorSec() {705 JFileChooser jFileChooser = new JFileChooser(new File("."));706 jFileChooser.setDialogTitle("klasör seçinir");707 jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);708 709 int notlar = jFileChooser.showOpenDialog(null);710 711 if (notlar == JFileChooser.APPROVE_OPTION) {712 return jFileChooser.getSelectedFile().getAbsolutePath();713 } else {714 return null;715 }716 717 }718 719 public static String dosyaOlusturma(File file) {720 if (!file.exists()) {721 System.out.println("dosya yok ");722 try {723 if (file.createNewFile()) {724 System.out.println("dosya olusturuldu");725 return "dosya olusutuldu";726 } else {727 return "dosya olusturulamadi";728 }

Page 11: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

729 } catch (IOException ex) {730 System.out.println("dosya olusturulurken hata olustu : " + ex.

toString());731 return "dosya olusturulamadi";732 }733 } else {734 System.out.println("dosya var");735 return "dosya var";736 }737 }738 739 }740 run:741 olusturacaginiz dosyanin adini giriniz742 erkan.sdasd743 dosya yok744 dosya olusturuldu745 BUILD SUCCESSFUL (total time: 12 seconds)746 747 çalıştırınca önce bize dosya adını sordu748 daha sonra klasör seçme metodu çağrıldı749 klasör seçme penceresi açıldı750 dosyamızı hangi klasörde oluşturmak istiyorsak onu seçtik751 girdiğimiz ad ve uzantıda belirlediğimiz klasörde dosyamız oluşturuldu752 753 754 755 şimdi klasör oluşturmaya bakalım756 klasör oluşturma metodu oluşturduk757 758 public static String klasorOlusturma(){759 760 }761 762 file sınıfından parametre aldı763 public static String klasorOlusturma(File file){764 765 }766 767 file exists ile kontrol ettik768 eğer dosya varsa ekrana dosya var dedik769 eğer yoksa oluşturalım770 mkdir ile oluşturulur771 772 773 public static String klasorOlusturma(File file) {774 if (file.exists()) {775 return "klasor var";776 } else {777 file.mkdir();778 return "klasor olusturuldu";779 }780 }781 782 783 main metodunda klasörün oluşturulacağı yeri seçtik784 ve klasörün adını girdik785 şimdi çalıştırırsak önce dosya adını girilir daha sonra nerede oluşturulacağı seçilir786 ve oluşturulur787 788 789 public static void main(String[] args) {790 791 Scanner scanner = new Scanner(System.in);792 System.out.println("olusturacaginiz klasorun adini giriniz");793 String klasorAdi = scanner.nextLine();794 795 String dosyaYolu = Test.klasorSec();796 File file = new File(dosyaYolu + "\\" + klasorAdi);797 798 Test.klasorOlusturma(file);799 800 }

Page 12: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

801 802 klasör oluşturulurken dosya uzantısı olmamalıdır803 programın son hali804 package javadosyaislemleri;805 806 import java.io.File;807 import java.io.IOException;808 import java.util.Scanner;809 import javax.swing.JFileChooser;810 811 public class Test {812 813 public static void main(String[] args) {814 815 Scanner scanner = new Scanner(System.in);816 System.out.println("olusturacaginiz klasorun adini giriniz");817 String klasorAdi = scanner.nextLine();818 819 String dosyaYolu = Test.klasorSec();820 File file = new File(dosyaYolu + "\\" + klasorAdi);821 822 Test.klasorOlusturma(file);823 824 }825 826 public static String dosyaSec() {827 828 JFileChooser jFileChooser = new JFileChooser();829 jFileChooser.setCurrentDirectory(new File("C:\\Users\\(DESSAS)\\Desktop\\"));830 jFileChooser.setDialogTitle("lusfen dosya seciniz");831 jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);832 833 int donendeger = jFileChooser.showOpenDialog(null);834 835 if (donendeger == JFileChooser.APPROVE_OPTION) {836 return jFileChooser.getSelectedFile().getAbsolutePath();837 } else {838 return null;839 }840 841 }842 843 public static String klasorSec() {844 JFileChooser jFileChooser = new JFileChooser(new File("."));845 jFileChooser.setDialogTitle("klasör seçinir");846 jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);847 848 int notlar = jFileChooser.showOpenDialog(null);849 850 if (notlar == JFileChooser.APPROVE_OPTION) {851 return jFileChooser.getSelectedFile().getAbsolutePath();852 } else {853 return null;854 }855 856 }857 858 public static String dosyaOlusturma(File file) {859 if (!file.exists()) {860 System.out.println("dosya yok ");861 try {862 if (file.createNewFile()) {863 System.out.println("dosya olusturuldu");864 return "dosya olusutuldu";865 } else {866 return "dosya olusturulamadi";867 }868 } catch (IOException ex) {869 System.out.println("dosya olusturulurken hata olustu : " + ex.

toString());870 return "dosya olusturulamadi";871 }872 } else {

Page 13: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

873 System.out.println("dosya var");874 return "dosya var";875 }876 }877 878 public static String klasorOlusturma(File file) {879 if (file.exists()) {880 System.out.println("klasor var");881 return "klasor var";882 } else {883 file.mkdir();884 System.out.println("klasor olusturuldu");885 return "klasor olusturuldu";886 }887 }888 889 }890 891 892 893 şimdi kopyalama işlemlerine bakalım894 bir metod oluşturduk895 iki yöntem vardır896 biri parça parça gönderir897 diğersi tamamını gönderir898 metodumuz file sınıfından iki parametre alır899 biri kaynağımız diğersi ise kopyalanacak adrestir900 901 public static void kopyalama(File kaynakFile,File hefdefFile){902 903 }904 905 if isdirectory ile kaynağı kontrol ettik906 varmı diye907 eğer kaynak varsa if ile hedefte istenen dosya varmı diye bakılır exists ile908 eğer yoksa mkdir ile oluşturulur909 910 public static void kopyalama(File kaynakFile, File hefdefFile) {911 if (kaynakFile.isDirectory()) {912 System.out.println("kaynak bulundu");913 if (!hefdefFile.exists()) {914 hefdefFile.mkdir();915 }916 }917 }918 919 string dizisi ile alt klasörleride alırız920 bunun için kaynak dosyasının list metodunu kullanırız921 922 923 public static void kopyalama(File kaynakFile, File hefdefFile) {924 if (kaynakFile.isDirectory()) {925 System.out.println("kaynak bulundu");926 if (!hefdefFile.exists()) {927 hefdefFile.mkdir();928 }929 }930 String dizi[]=kaynakFile.list();931 932 }933 934 for ile kopyalama metodunu çağırırız935 bunun ile klasörleri oluşturmuş olduk936 937 public static void kopyalama(File kaynakFile, File hefdefFile) {938 if (kaynakFile.isDirectory()) {939 System.out.println("kaynak bulundu");940 if (!hefdefFile.exists()) {941 hefdefFile.mkdir();942 }943 }944 String dizi[] = kaynakFile.list();945 for (String string : dizi) {

Page 14: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

946 kopyalama(new File(kaynakFile, string), new File(hefdefFile, string));947 }948 }949 950 şimdi kopyalama metodlarına bakalım951 inputstream ilekoyalamaya bakacağız952 iki parametre alır file sınıfından kaynak ve hedef953 954 public static void inputStreamIleKopyalama(File kaynakFile, File hefdefFile) {955 956 }957 958 if ile dosya okunabilirmi yazılabilirmi diye bakacağız isdirectory ile959 şimdide varmı diye bakalım exists ile bakarız960 eğer yoksa createNewFile ile oluştururuz961 962 963 public static void inputStreamIleKopyalama(File kaynakFile, File hefdefFile) {964 if (hefdefFile.isDirectory()) {965 hefdefFile = new File(hefdefFile, kaynakFile.getName());966 }967 if (!hefdefFile.exists()) {968 try {969 hefdefFile.createNewFile();970 } catch (IOException ex) {971 System.out.println("hedef dosya olusturulamadi");972 }973 }974 975 }976 977 978 inputstream nesnesi oluşturduk979 new ile fileinputstream bağladık980 parametre olarak kaynağı verdik981 982 public static void inputStreamIleKopyalama(File kaynakFile, File hefdefFile) {983 if (hefdefFile.isDirectory()) {984 hefdefFile = new File(hefdefFile, kaynakFile.getName());985 }986 if (!hefdefFile.exists()) {987 try {988 hefdefFile.createNewFile();989 } catch (IOException ex) {990 System.out.println("hedef dosya olusturulamadi");991 }992 }993 try {994 InputStream inputStream=new FileInputStream(kaynakFile);995 } catch (FileNotFoundException ex) {996 997 }998 }999

1000 bu input birde output var1001 outputstream nesnesi oluşturduk1002 new ile fileoutputnesnesine bağladık1003 1004 public static void inputStreamIleKopyalama(File kaynakFile, File hefdefFile) {1005 if (hefdefFile.isDirectory()) {1006 hefdefFile = new File(hefdefFile, kaynakFile.getName());1007 }1008 if (!hefdefFile.exists()) {1009 try {1010 hefdefFile.createNewFile();1011 } catch (IOException ex) {1012 System.out.println("hedef dosya olusturulamadi");1013 }1014 }1015 try {1016 InputStream inputStream = new FileInputStream(kaynakFile);1017 OutputStream outputStream = new FileOutputStream(hefdefFile);1018 } catch (FileNotFoundException ex) {

Page 15: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

1019 1020 }1021 }1022 1023 bilgileri byte byte göndereceğimizden dolayı byte dizisi oluşturudk1024 int uzunluk değişkeni tanımladık1025 while ile uzunluk değeri inputstream nesnesinin read metodunundan dönen değere

eşitledik1026 eğer 0 dan bütükse while çalışmaya devam etsin1027 1028 public static void inputStreamIleKopyalama(File kaynakFile, File hefdefFile) {1029 if (hefdefFile.isDirectory()) {1030 hefdefFile = new File(hefdefFile, kaynakFile.getName());1031 }1032 if (!hefdefFile.exists()) {1033 try {1034 hefdefFile.createNewFile();1035 } catch (IOException ex) {1036 System.out.println("hedef dosya olusturulamadi");1037 }1038 }1039 try {1040 InputStream inputStream = new FileInputStream(kaynakFile);1041 OutputStream outputStream = new FileOutputStream(hefdefFile);1042 1043 byte dizi[] = new byte[1024];1044 int uzunluk;1045 while ((uzunluk=inputStream.read(dizi))>0) {1046 1047 }1048 1049 } catch (FileNotFoundException ex) {1050 1051 } catch (IOException ex) {1052 Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);1053 }1054 }1055 şimdi yazdıralım1056 parametre olarak byte dizisini verdik1057 uzunluk sıfırdan bütük olduğu müddetçe while çalışsın1058 outputstream nesnesinin write metodu ile yazdırma işlemini yaptık1059 3 parametre aldı1060 birincisi byte dizisi ikincisi 0 üçüncüsü uzunluk değeridir1061 1062 public static void inputStreamIleKopyalama(File kaynakFile, File hefdefFile) {1063 if (hefdefFile.isDirectory()) {1064 hefdefFile = new File(hefdefFile, kaynakFile.getName());1065 }1066 if (!hefdefFile.exists()) {1067 try {1068 hefdefFile.createNewFile();1069 } catch (IOException ex) {1070 System.out.println("hedef dosya olusturulamadi");1071 }1072 }1073 try {1074 InputStream inputStream = new FileInputStream(kaynakFile);1075 OutputStream outputStream = new FileOutputStream(hefdefFile);1076 1077 byte dizi[] = new byte[1024];1078 int uzunluk;1079 while ((uzunluk = inputStream.read(dizi)) > 0) {1080 outputStream.write(dizi, 0, uzunluk);1081 }1082 1083 } catch (FileNotFoundException ex) {1084 1085 } catch (IOException ex) {1086 Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);1087 }1088 }1089 1090 şimdi main metodundan bunu kullanalım

Page 16: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

1091 1092 1093 public static void main(String[] args) {1094 1095 String path = Test.klasorSec();1096 1097 String dosyaYolu = Test.klasorSec();1098 Test.kopyalama(new File(path), new File(dosyaYolu));1099 1100 }1101 programı çalıştırınca seçtiğimiz klasörü diğer klasöre kopyalar1102 klasörün altındakileride kopyalar1103 1104 programın son hali1105 package javadosyaislemleri;1106 1107 import java.io.File;1108 import java.io.FileInputStream;1109 import java.io.FileNotFoundException;1110 import java.io.FileOutputStream;1111 import java.io.IOException;1112 import java.io.InputStream;1113 import java.io.OutputStream;1114 import java.util.logging.Level;1115 import java.util.logging.Logger;1116 import javax.swing.JFileChooser;1117 1118 public class Test {1119 1120 public static void main(String[] args) {1121 1122 String path = Test.klasorSec();1123 1124 String dosyaYolu = Test.klasorSec();1125 Test.kopyalama(new File(path), new File(dosyaYolu));1126 1127 }1128 1129 public static String dosyaSec() {1130 1131 JFileChooser jFileChooser = new JFileChooser();1132 jFileChooser.setCurrentDirectory(new File("C:\\Users\\(DESSAS)\\Desktop\\"));1133 jFileChooser.setDialogTitle("lusfen dosya seciniz");1134 jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);1135 1136 int donendeger = jFileChooser.showOpenDialog(null);1137 1138 if (donendeger == JFileChooser.APPROVE_OPTION) {1139 return jFileChooser.getSelectedFile().getAbsolutePath();1140 } else {1141 return null;1142 }1143 1144 }1145 1146 public static String klasorSec() {1147 JFileChooser jFileChooser = new JFileChooser(new File("."));1148 jFileChooser.setDialogTitle("klasör seçinir");1149 jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);1150 1151 int notlar = jFileChooser.showOpenDialog(null);1152 1153 if (notlar == JFileChooser.APPROVE_OPTION) {1154 return jFileChooser.getSelectedFile().getAbsolutePath();1155 } else {1156 return null;1157 }1158 1159 }1160 1161 public static String dosyaOlusturma(File file) {1162 if (!file.exists()) {1163 System.out.println("dosya yok ");

Page 17: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

1164 try {1165 if (file.createNewFile()) {1166 System.out.println("dosya olusturuldu");1167 return "dosya olusutuldu";1168 } else {1169 return "dosya olusturulamadi";1170 }1171 } catch (IOException ex) {1172 System.out.println("dosya olusturulurken hata olustu : " + ex.

toString());1173 return "dosya olusturulamadi";1174 }1175 } else {1176 System.out.println("dosya var");1177 return "dosya var";1178 }1179 }1180 1181 public static String klasorOlusturma(File file) {1182 if (file.exists()) {1183 System.out.println("klasor var");1184 return "klasor var";1185 } else {1186 file.mkdir();1187 System.out.println("klasor olusturuldu");1188 return "klasor olusturuldu";1189 }1190 }1191 1192 public static void kopyalama(File kaynakFile, File hefdefFile) {1193 if (kaynakFile.isDirectory()) {1194 System.out.println("kaynak bulundu");1195 if (!hefdefFile.exists()) {1196 hefdefFile.mkdir();1197 }1198 1199 String dizi[] = kaynakFile.list();1200 for (String string : dizi) {1201 kopyalama(new File(kaynakFile, string), new File(hefdefFile, string));1202 }1203 } else {1204 inputStreamIleKopyalama(kaynakFile, hefdefFile);1205 }1206 1207 }1208 1209 public static void inputStreamIleKopyalama(File kaynakFile, File hefdefFile) {1210 if (hefdefFile.isDirectory()) {1211 hefdefFile = new File(hefdefFile, kaynakFile.getName());1212 }1213 if (!hefdefFile.exists()) {1214 try {1215 hefdefFile.createNewFile();1216 } catch (IOException ex) {1217 System.out.println("hedef dosya olusturulamadi");1218 }1219 }1220 try {1221 InputStream inputStream = new FileInputStream(kaynakFile);1222 OutputStream outputStream = new FileOutputStream(hefdefFile);1223 1224 byte dizi[] = new byte[1024];1225 int uzunluk;1226 while ((uzunluk = inputStream.read(dizi)) > 0) {1227 outputStream.write(dizi, 0, uzunluk);1228 }1229 1230 } catch (FileNotFoundException ex) {1231 1232 } catch (IOException ex) {1233 Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);1234 }1235 }

Page 18: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

1236 1237 }1238 1239 bu şekilde olunca klasörü belirlediğimiz klasöre kopyalar1240 şimdide seçilen dosyayı seçilen klasöre kopyalayalım1241 main metodu aşağıdaki gibi olmalıdır1242 public static void main(String[] args) {1243 1244 String path = Test.dosyaSec();1245 1246 String dosyaYolu = Test.klasorSec();1247 Test.kopyalama(new File(path), new File(dosyaYolu));1248 1249 }1250 1251 şimdi input streamlarla değilde filechannel ile koplayalım1252 fileChannelIleKopyalama metodunu oluşturduk1253 hedef dosya varmı diye kontrol ettik eğer yoksa oluşturduk1254 if (hefdefFile.isDirectory()) {// kaynak dosya varmi yoksa olustur1255 hefdefFile = new File(hefdefFile, kaynakFile.getName());1256 }1257 if (!hefdefFile.exists()) {1258 try {1259 hefdefFile.createNewFile();1260 } catch (IOException ex) {1261 System.out.println("dosya olusturulamadi");1262 }1263 }1264 file channel sınınfından nesne oluşturduk1265 FileChannel kaynakChannel = null, hedefChannel = null;1266 transferfrom metodu ile kopyalarız1267 üç parametre alır kaynak channel başlanğıç adresi ve uzunluğu1268 hedefChannel.transferFrom(kaynakChannel, 0, kaynakChannel.size());1269 1270 public static void fileChannelIleKopyalama(File kaynakFile, File hefdefFile) {1271 1272 if (hefdefFile.isDirectory()) {// kaynak dosya varmi yoksa olustur1273 hefdefFile = new File(hefdefFile, kaynakFile.getName());1274 }1275 if (!hefdefFile.exists()) {1276 try {1277 hefdefFile.createNewFile();1278 } catch (IOException ex) {1279 System.out.println("dosya olusturulamadi");1280 }1281 }1282 1283 FileChannel kaynakChannel = null, hedefChannel = null;1284 1285 try {1286 kaynakChannel = new FileInputStream(kaynakFile).getChannel();1287 hedefChannel = new FileOutputStream(hefdefFile).getChannel();1288 hedefChannel.transferFrom(kaynakChannel, 0, kaynakChannel.size());1289 } catch (FileNotFoundException ex) {1290 Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);1291 } catch (IOException ex) {1292 Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);1293 }1294 1295 }1296 1297 kopyapa metodundan fileChannelIleKopyalama metodunu çağırdık1298 1299 public static void kopyalama(File kaynakFile, File hefdefFile) {1300 if (kaynakFile.isDirectory()) {1301 System.out.println("kaynak bulundu");1302 if (!hefdefFile.exists()) {1303 hefdefFile.mkdir();1304 }1305 1306 String dizi[] = kaynakFile.list();1307 for (String string : dizi) {1308 kopyalama(new File(kaynakFile, string), new File(hefdefFile, string));

Page 19: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

1309 }1310 } else {1311 // inputStreamIleKopyalama(kaynakFile, hefdefFile);1312 fileChannelIleKopyalama(kaynakFile, hefdefFile);1313 }1314 1315 }1316 main metodunu değiştirmedik1317 1318 String path = Test.dosyaSec();1319 1320 String dosyaYolu = Test.klasorSec();1321 Test.kopyalama(new File(path), new File(dosyaYolu));1322 1323 çalıştırısak çalışır1324 1325 programın son hali1326 package javadosyaislemleri;1327 1328 import java.io.File;1329 import java.io.FileInputStream;1330 import java.io.FileNotFoundException;1331 import java.io.FileOutputStream;1332 import java.io.IOException;1333 import java.io.InputStream;1334 import java.io.OutputStream;1335 import java.nio.channels.FileChannel;1336 import java.util.logging.Level;1337 import java.util.logging.Logger;1338 import javax.swing.JFileChooser;1339 1340 public class Test {1341 1342 public static void main(String[] args) {1343 1344 String path = Test.dosyaSec();1345 1346 String dosyaYolu = Test.klasorSec();1347 Test.kopyalama(new File(path), new File(dosyaYolu));1348 1349 }1350 1351 public static String dosyaSec() {1352 1353 JFileChooser jFileChooser = new JFileChooser();1354 jFileChooser.setCurrentDirectory(new File("C:\\Users\\(DESSAS)\\Desktop\\"));1355 jFileChooser.setDialogTitle("lusfen dosya seciniz");1356 jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);1357 1358 int donendeger = jFileChooser.showOpenDialog(null);1359 1360 if (donendeger == JFileChooser.APPROVE_OPTION) {1361 System.out.println("secilen dosyanin adresi : " + jFileChooser.

getSelectedFile().getAbsolutePath());1362 return jFileChooser.getSelectedFile().getAbsolutePath();1363 } else {1364 return null;1365 }1366 1367 }1368 1369 public static String klasorSec() {1370 JFileChooser jFileChooser = new JFileChooser(new File("."));1371 jFileChooser.setDialogTitle("klasör seçinir");1372 jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);1373 1374 int notlar = jFileChooser.showOpenDialog(null);1375 1376 if (notlar == JFileChooser.APPROVE_OPTION) {1377 return jFileChooser.getSelectedFile().getAbsolutePath();1378 } else {1379 return null;1380 }

Page 20: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

1381 1382 }1383 1384 public static String dosyaOlusturma(File file) {1385 if (!file.exists()) {1386 System.out.println("dosya yok ");1387 try {1388 if (file.createNewFile()) {1389 System.out.println("dosya olusturuldu");1390 return "dosya olusutuldu";1391 } else {1392 return "dosya olusturulamadi";1393 }1394 } catch (IOException ex) {1395 System.out.println("dosya olusturulurken hata olustu : " + ex.

toString());1396 return "dosya olusturulamadi";1397 }1398 } else {1399 System.out.println("dosya var");1400 return "dosya var";1401 }1402 }1403 1404 public static String klasorOlusturma(File file) {1405 if (file.exists()) {1406 System.out.println("klasor var");1407 return "klasor var";1408 } else {1409 file.mkdir();1410 System.out.println("klasor olusturuldu");1411 return "klasor olusturuldu";1412 }1413 }1414 1415 public static void kopyalama(File kaynakFile, File hefdefFile) {1416 if (kaynakFile.isDirectory()) {1417 System.out.println("kaynak bulundu");1418 if (!hefdefFile.exists()) {1419 hefdefFile.mkdir();1420 }1421 1422 String dizi[] = kaynakFile.list();1423 for (String string : dizi) {1424 kopyalama(new File(kaynakFile, string), new File(hefdefFile, string));1425 }1426 } else {1427 // inputStreamIleKopyalama(kaynakFile, hefdefFile);1428 fileChannelIleKopyalama(kaynakFile, hefdefFile);1429 }1430 1431 }1432 1433 public static void inputStreamIleKopyalama(File kaynakFile, File hefdefFile) {1434 if (hefdefFile.isDirectory()) {1435 hefdefFile = new File(hefdefFile, kaynakFile.getName());1436 }1437 if (!hefdefFile.exists()) {1438 try {1439 hefdefFile.createNewFile();1440 } catch (IOException ex) {1441 System.out.println("hedef dosya olusturulamadi");1442 }1443 }1444 try {1445 InputStream inputStream = new FileInputStream(kaynakFile);1446 OutputStream outputStream = new FileOutputStream(hefdefFile);1447 1448 byte dizi[] = new byte[1024];1449 int uzunluk;1450 while ((uzunluk = inputStream.read(dizi)) > 0) {1451 outputStream.write(dizi, 0, uzunluk);1452 }

Page 21: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

1453 1454 } catch (FileNotFoundException ex) {1455 1456 } catch (IOException ex) {1457 Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);1458 }1459 }1460 1461 public static void fileChannelIleKopyalama(File kaynakFile, File hefdefFile) {1462 1463 if (hefdefFile.isDirectory()) {// kaynak dosya varmi yoksa olustur1464 hefdefFile = new File(hefdefFile, kaynakFile.getName());1465 }1466 if (!hefdefFile.exists()) {1467 try {1468 hefdefFile.createNewFile();1469 } catch (IOException ex) {1470 System.out.println("dosya olusturulamadi");1471 }1472 }1473 1474 FileChannel kaynakChannel = null, hedefChannel = null;1475 1476 try {1477 kaynakChannel = new FileInputStream(kaynakFile).getChannel();1478 hedefChannel = new FileOutputStream(hefdefFile).getChannel();1479 hedefChannel.transferFrom(kaynakChannel, 0, kaynakChannel.size());1480 } catch (FileNotFoundException ex) {1481 Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);1482 } catch (IOException ex) {1483 Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);1484 }1485 1486 }1487 1488 }1489 1490 şimdi klasör kopyalamaya bakalım1491 video 101492 1493 dosya kopyalama İle aynı kodlar yazılır1494 fark eden birşey yoktur1495 önce dosya seçeriz1496 daha sonra kopyalanacak klasörü seçeriz1497 dosya kopyalama metodundaki aynı kodlarla kopyalama işlemini yaparız1498 1499 1500 1501 1502 şimdi taşıma işlemine bakalım1503 yeni bir metod oluşturduk1504 taşıma metodu1505 iki parametre alır kaynak ve hedef1506 1507 public static void tasima(File kaynak,File hedef) {1508 }1509 1510 önce if exists ile hedef dizin varmı kontrol edilir1511 eğer yoksa mkdirs ile oluşturulur1512 renameto metodu ile kopyalarız1513 kaynak.renameto ile kopyalarız1514 new file parametresi alır1515 birincisi hedef dosya1516 diğersi kaynak.getname dosyasının adıdır1517 1518 public static void tasima(File kaynak, File hedef) {1519 if (!hedef.exists()) {1520 hedef.mkdirs();1521 }1522 kaynak.renameTo(new File(hedef, kaynak.getName()));1523 }1524 1525 main metodunda önce taşınacağı dosya seçilir

Page 22: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

1526 daha sonra taşınacağıyer null mu diye kontrol edilir1527 null ise seçim yapılmadı yazsın1528 1529 public class Test {1530 1531 public static void main(String[] args) {1532 1533 System.out.println("tasinacak dosyayi seciniz");1534 String klasor=Test.dosyaSec();1535 System.out.println("hedef dosyayi seciniz");1536 String hedef=Test.klasorSec();1537 if (klasor==null) {1538 System.out.println("dosya secilmedi");1539 } else {1540 Test.tasima(new File(klasor), new File(hedef));1541 }1542 1543 }1544 şimdi klasör taşıma işlemine bakalım1545 bunun için tasima metodu aynen kullanılır1546 main metodunda dosya yerine klasor seçilir1547 ancak c diskinden d diskine aktarım yapamadım1548 public class Test {1549 1550 public static void main(String[] args) {1551 1552 System.out.println("tasinacak dosyayi seciniz");1553 String klasor=Test.klasorSec();1554 System.out.println("hedef dosyayi seciniz");1555 String hedef=Test.klasorSec();1556 if (klasor==null) {1557 System.out.println("dosya secilmedi");1558 } else {1559 Test.tasima(new File(klasor), new File(hedef));1560 }1561 1562 }1563 1564 1565 şimdi silme işlemine bakalım1566 silme metodu oluşturduk1567 parametre olarak file sınıfından nesne aldı1568 önce exists metodu ile varmı diye kontrol ettik1569 public static void silmeIslemi(File file) {1570 if (!file.exists()) {1571 1572 1573 1574 }1575 }1576 1577 1578 eğer varsa isfile metodu ile dosyamı diye baktık1579 eğer dosya ise delete ile sileriz1580 1581 public static void silmeIslemi(File file) {1582 if (!file.exists()) {1583 1584 if (file.isFile()) {1585 file.delete();1586 }1587 1588 }1589 }1590 1591 1592 isdirectory ile klasörmü diye bakarız1593 eğer dizin ise string dizisi oluşturduk1594 ve alt klaösrleri list metodu ile alırız1595 for ile alt klasörleri gezeriz1596 1597 public static void silmeIslemi(File file) {1598 if (!file.exists()) {

Page 23: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

1599 1600 if (file.isFile()) {1601 file.delete();1602 } else if (file.isDirectory()) {1603 String altKlasorler[] = file.list();1604 1605 for (int i = 0; i < altKlasorler.length; i++) {1606 1607 }1608 1609 }1610 1611 }1612 }1613 1614 kendikendisini çağırırarak sileriz1615 en son sileriz1616 1617 dizin silme işlemi için eğitim setindeki kodları aynen aldım1618 public static String silmeMetodu(File secilenDosyaveDizin) {1619 1620 if (secilenDosyaveDizin.exists()) {1621 if (secilenDosyaveDizin.isFile()) {1622 if (secilenDosyaveDizin.delete() == true) {1623 return "ONAYLANDI";1624 } else {1625 return "Hata oluştu.";1626 }1627 } else if (secilenDosyaveDizin.isDirectory()) {1628 if (ustKlasorVeIcindekiAltKlasorleriSilmeMetodu(secilenDosyaveDizin)

== true) {1629 return "ONAYLANDI";1630 } else {1631 return "Hata oluştu";1632 }1633 1634 } else {1635 return "Veilen yol bir dosyaya veya klasöre ait değil.";1636 }1637 1638 } else {1639 return "Silinecek dosya veya klasör bulunamadı!";1640 }1641 1642 }1643 1644 public static boolean ustKlasorVeIcindekiAltKlasorleriSilmeMetodu(File dizin) {1645 1646 if (dizin.isDirectory()) {1647 String[] altKlasorler = dizin.list();1648 1649 for (int i = 0; i < altKlasorler.length; i++) {1650 boolean sonuc = ustKlasorVeIcindekiAltKlasorleriSilmeMetodu(new File(

dizin, altKlasorler[i]));1651 if (false == sonuc) {1652 return false;1653 }1654 }1655 }1656 return dizin.delete();1657 }1658 1659 main metodunda klasorü seçeriz ve sileriz1660 1661 public class Test {1662 1663 public static void main(String[] args) {1664 1665 System.out.println("silinecek dosyayi seciniz");1666 String klasor = Test.klasorSec();1667 1668 if (klasor == null) {1669 System.out.println("dosya secilmedi");

Page 24: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

1670 } else {1671 Test.silmeMetodu(new File(klasor));1672 }1673 1674 }1675 1676 programın son hali1677 1678 package javadosyaislemleri;1679 1680 import java.io.File;1681 import java.io.FileInputStream;1682 import java.io.FileNotFoundException;1683 import java.io.FileOutputStream;1684 import java.io.IOException;1685 import java.io.InputStream;1686 import java.io.OutputStream;1687 import java.nio.channels.FileChannel;1688 import java.util.logging.Level;1689 import java.util.logging.Logger;1690 import javax.swing.JFileChooser;1691 1692 public class Test {1693 1694 public static void main(String[] args) {1695 1696 System.out.println("silinecek dosyayi seciniz");1697 String klasor = Test.klasorSec();1698 1699 if (klasor == null) {1700 System.out.println("dosya secilmedi");1701 } else {1702 Test.silmeMetodu(new File(klasor));1703 }1704 1705 }1706 1707 public static String dosyaSec() {1708 1709 JFileChooser jFileChooser = new JFileChooser();1710 jFileChooser.setCurrentDirectory(new File("C:\\Users\\(DESSAS)\\Desktop\\"));1711 jFileChooser.setDialogTitle("lusfen dosya seciniz");1712 jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);1713 1714 int donendeger = jFileChooser.showOpenDialog(null);1715 1716 if (donendeger == JFileChooser.APPROVE_OPTION) {1717 System.out.println("secilen dosyanin adresi : " + jFileChooser.

getSelectedFile().getAbsolutePath());1718 return jFileChooser.getSelectedFile().getAbsolutePath();1719 } else {1720 return null;1721 }1722 1723 }1724 1725 public static String klasorSec() {1726 JFileChooser jFileChooser = new JFileChooser(new File("."));1727 jFileChooser.setDialogTitle("klasör seçinir");1728 jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);1729 1730 int notlar = jFileChooser.showOpenDialog(null);1731 1732 if (notlar == JFileChooser.APPROVE_OPTION) {1733 return jFileChooser.getSelectedFile().getAbsolutePath();1734 } else {1735 return null;1736 }1737 1738 }1739 1740 public static String dosyaOlusturma(File file) {1741 if (!file.exists()) {

Page 25: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

1742 System.out.println("dosya yok ");1743 try {1744 if (file.createNewFile()) {1745 System.out.println("dosya olusturuldu");1746 return "dosya olusutuldu";1747 } else {1748 return "dosya olusturulamadi";1749 }1750 } catch (IOException ex) {1751 System.out.println("dosya olusturulurken hata olustu : " + ex.

toString());1752 return "dosya olusturulamadi";1753 }1754 } else {1755 System.out.println("dosya var");1756 return "dosya var";1757 }1758 }1759 1760 public static String klasorOlusturma(File file) {1761 if (file.exists()) {1762 System.out.println("klasor var");1763 return "klasor var";1764 } else {1765 file.mkdir();1766 System.out.println("klasor olusturuldu");1767 return "klasor olusturuldu";1768 }1769 }1770 1771 public static void kopyalama(File kaynakFile, File hefdefFile) {1772 if (kaynakFile.isDirectory()) {1773 System.out.println("kaynak bulundu");1774 if (!hefdefFile.exists()) {1775 hefdefFile.mkdir();1776 }1777 1778 String dizi[] = kaynakFile.list();1779 for (String string : dizi) {1780 kopyalama(new File(kaynakFile, string), new File(hefdefFile, string));1781 }1782 } else {1783 // inputStreamIleKopyalama(kaynakFile, hefdefFile);1784 fileChannelIleKopyalama(kaynakFile, hefdefFile);1785 }1786 1787 }1788 1789 public static void inputStreamIleKopyalama(File kaynakFile, File hefdefFile) {1790 if (hefdefFile.isDirectory()) {1791 hefdefFile = new File(hefdefFile, kaynakFile.getName());1792 }1793 if (!hefdefFile.exists()) {1794 try {1795 hefdefFile.createNewFile();1796 } catch (IOException ex) {1797 System.out.println("hedef dosya olusturulamadi");1798 }1799 }1800 try {1801 InputStream inputStream = new FileInputStream(kaynakFile);1802 OutputStream outputStream = new FileOutputStream(hefdefFile);1803 1804 byte dizi[] = new byte[1024];1805 int uzunluk;1806 while ((uzunluk = inputStream.read(dizi)) > 0) {1807 outputStream.write(dizi, 0, uzunluk);1808 }1809 1810 } catch (FileNotFoundException ex) {1811 1812 } catch (IOException ex) {1813 Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);

Page 26: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

1814 }1815 }1816 1817 public static void fileChannelIleKopyalama(File kaynakFile, File hefdefFile) {1818 1819 if (hefdefFile.isDirectory()) {// kaynak dosya varmi yoksa olustur1820 hefdefFile = new File(hefdefFile, kaynakFile.getName());1821 }1822 if (!hefdefFile.exists()) {1823 try {1824 hefdefFile.createNewFile();1825 } catch (IOException ex) {1826 System.out.println("dosya olusturulamadi");1827 }1828 }1829 1830 FileChannel kaynakChannel = null, hedefChannel = null;1831 1832 try {1833 kaynakChannel = new FileInputStream(kaynakFile).getChannel();1834 hedefChannel = new FileOutputStream(hefdefFile).getChannel();1835 hedefChannel.transferFrom(kaynakChannel, 0, kaynakChannel.size());1836 } catch (FileNotFoundException ex) {1837 Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);1838 } catch (IOException ex) {1839 Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);1840 }1841 1842 }1843 1844 public static void tasima(File kaynak, File hedef) {1845 if (!hedef.exists()) {1846 hedef.mkdirs();1847 }1848 kaynak.renameTo(new File(hedef, kaynak.getName()));1849 }1850 1851 public static String silmeMetodu(File secilenDosyaveDizin) {1852 1853 if (secilenDosyaveDizin.exists()) {1854 if (secilenDosyaveDizin.isFile()) {1855 if (secilenDosyaveDizin.delete() == true) {1856 return "ONAYLANDI";1857 } else {1858 return "Hata oluştu.";1859 }1860 } else if (secilenDosyaveDizin.isDirectory()) {1861 if (ustKlasorVeIcindekiAltKlasorleriSilmeMetodu(secilenDosyaveDizin)

== true) {1862 return "ONAYLANDI";1863 } else {1864 return "Hata oluştu";1865 }1866 1867 } else {1868 return "Veilen yol bir dosyaya veya klasöre ait değil.";1869 }1870 1871 } else {1872 return "Silinecek dosya veya klasör bulunamadı!";1873 }1874 1875 }1876 1877 public static boolean ustKlasorVeIcindekiAltKlasorleriSilmeMetodu(File dizin) {1878 1879 if (dizin.isDirectory()) {1880 String[] altKlasorler = dizin.list();1881 1882 for (int i = 0; i < altKlasorler.length; i++) {1883 boolean sonuc = ustKlasorVeIcindekiAltKlasorleriSilmeMetodu(new File(

dizin, altKlasorler[i]));1884 if (false == sonuc) {

Page 27: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

1885 return false;1886 }1887 }1888 }1889 return dizin.delete();1890 }1891 1892 }1893 1894 şimdi dosya okuma metodunu yapalım1895 metodu oluşturduk file parametresi aldı1896 scanner nesnesi oluşturduk1897 parametre olarak file parametresini verdik1898 list nesnesi oluşturduk1899 while ile scanner nesnesini hasnestline metodu ile gezdik1900 gelen değerleri add ile list yapısına ekledik1901 1902 public static List<String> dosyaOkuma(File file) throws FileNotFoundException {1903 Scanner scanner = new Scanner(file);1904 List<String> list = new ArrayList<>();1905 while (scanner.hasNextLine()) {1906 list.add(scanner.nextLine());1907 }1908 return list;1909 }1910 1911 1912 okunan değerleri list yapısından tek tek aldık1913 1914 1915 public class Test {1916 1917 public static void main(String[] args) {1918 1919 System.out.println("silinecek dosyayi seciniz");1920 String klasor = Test.dosyaSec();1921 1922 if (klasor == null) {1923 System.out.println("dosya secilmedi");1924 } else {1925 try {1926 List<String> list = Test.dosyaOkuma(new File(klasor));1927 for (int i = 0; i < list.size(); i++) {1928 System.out.println(list.get(i));1929 }1930 } catch (FileNotFoundException ex) {1931 System.out.println("dosya okunurken hata olustu");1932 }1933 }1934 1935 }1936 1937 1938 1939 şimdide yazma işlemine bakalım1940 dosyayazma metodu oluşturduk1941 list ve file tiplerinde parametre aldı1942 list tipinde eklenecek satırlar1943 bufferedwriter sınıfından nesne oluşturduk1944 filewriter sınıfından parametre aldı1945 new filewriter dosya dedik1946 list yapısındaki her satırı yazdırmak için döngü açtık1947 appends ile ekledik1948 newline ile alt satıra geçip ekleneni buraya ekledik1949 1950 public static void dosyayaYazma(List<String> yazilacaklar, File file) throws

IOException {1951 BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, true

));1952 1953 for (String string : yazilacaklar) {1954 bufferedWriter.append(string);1955 bufferedWriter.newLine();

Page 28: WordPress.com...147 } else {148 } 149 150 } 151 } 152 153 154 eğer dosya seçilmişse getselectedfile ile seçilen dosyayı döndürürüz bununda

1956 }1957 bufferedWriter.close();1958 1959 }1960 1961 şimdi main metodunda bunu kullanalım1962 public class Test {1963 1964 public static void main(String[] args) {1965 1966 System.out.println("silinecek dosyayi seciniz");1967 String klasor = Test.dosyaSec();1968 1969 if (klasor == null) {1970 System.out.println("dosya secilmedi");1971 } else {1972 1973 String string[] = {"erkan", "oner", "kajmer"};1974 List<String> list = new ArrayList<>();1975 for (String string1 : string) {1976 list.add(string1);1977 }1978 try {1979 1980 Test.dosyayaYazma(list, new File(klasor));1981 } catch (IOException ex) {1982 System.out.println("yazilirken hata aldi");1983 }1984 1985 }1986 1987 }1988 1989