1 / 13

Dosya İşlemleri

Dosya İşlemleri. Exceptions. An exception is an error that occurs at runtime. It is either generated by the Java Virtual Machine (VM) in response to an unexpected condition or it is generated by your code as a result of executing a throw statement.

beau-dixon
Télécharger la présentation

Dosya İşlemleri

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dosya İşlemleri

  2. Exceptions An exception is an error that occurs at runtime. It is either generated by the Java Virtual Machine (VM) in response to an unexpected condition or it is generated by your code as a result of executing a throw statement. • java.lang.Object   |   +--java.lang.Throwable         |         +--java.lang.Exception         |     |         |     +--java.lang.ClassNotFoundException         |     |         |     +--java.io.IOException         |     |     |         |     |     +--java.io.FileNotFoundException         |     |         |     +--java.lang.RuntimeException         |           |         |           +--java.lang.NullPointerException         |           |         |           +--java.lang.IndexOutOfBoundsException         |                 |         |                 +--java.lang.ArrayIndexOutOfBoundsException         |         +--java.lang.Error               |               +--java.lang.VirtualMachineError                     |                     +--java.lang.OutOfMemoryError

  3. Try-Catch • try {//komutlar    }    catch (Exception ex)    {      System.out.println(“Hata Bulundu"); ex.printStackTrace();    }

  4. Catching Multiple Exceptions try { //... } catch ( FileNotFoundException e ) {System.out.println( e.getMessage());} catch ( IOException e ) {System.out.println( e + " IO EXCEPTION" ); } catch ( Exception e ) {System.out.println( e + " EXCEPTION" ); }

  5. Dosya İşlemleri • import java.io.File; • File dosya = new File(dosyaAdi); • dosya.getAbsolutePath() dosya.getPath() dosya.getName() dosya.getParent() dosya.exists() dosya.canRead() dosya.canWrite() dosya.isDirectory() dosya.isFile() dosya.lastModified() dosya.length()

  6. Dosya Oluşturmak File f = new File(dosyaAdi); // Dosya nesnesi if(!f.exists()){ //Dosya zaten var mı f.createNewFile(); //Dosyayı oluştur }

  7. Dosya Silmek File f = new File(dosyaAdi); //Dosya Nesnesi if(f.exists()){ //Dosya var mı f.delete(); //Dosyayı sil }

  8. Dosya Okuma 1 try { FileInputStreamfis = new FileInputStream(dosyaAdi); intch = 0; while (ch != -1) { ch = fis.read(); char karakter = (char)ch; System.out.print(karakter); } fis.close(); } catch (Exception e) { e.printStackTrace(); }

  9. Dosya Okuma 2 try { FileReaderfr = new FileReader(dosyaAdi); BufferedReaderbr = new BufferedReader(fr); while(br.ready()){ String satir = br.readLine(); System.out.println(satir); } fr.close(); br.close(); } catch (Exception e) { e.printStackTrace(); }

  10. Dosya Yazma 1 try { FileOutputStream fos = newFileOutputStream(dosyaAdi); String yazi = "Bu satir dosyaya yazilacak\naltina da bu satir yazilacak."; fos.write(yazi.getBytes()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); }

  11. Dosya Yazma 2 try { FileWriter fw = new FileWriter(dosyaAdi); BufferedWriter bw = new BufferedWriter(fw); bw.write("Bu satiri yaz\nyeni satira gec."); bw.flush(); bw.close(); } catch (Exception e) { e.printStackTrace(); }

  12. Scanner ile Dosya Okuma 1 try { Scanner s = new Scanner( new File(dosyaAdi)); String dosyaIcerigi = s.useDelimiter("\\A").next(); System.out.println(dosyaIcerigi); s.close(); } catch (Exception e) { e.printStackTrace(); }

  13. Scanner ile Dosya Okuma 2 try { Scanner s = new Scanner( new File("siir.txt")); while(s.hasNext()){ String satir = s.nextLine(); System.out.println(satir); } } catch (Exception e) { e.printStackTrace(); }

More Related