Dosya İşlemleri

13
Dosya İşlemleri

description

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. - PowerPoint PPT Presentation

Transcript of Dosya İşlemleri

Page 1: Dosya İşlemleri

Dosya İşlemleri

Page 2: Dosya İşlemleri

Exceptions• 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

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.

Page 3: Dosya İşlemleri

Try-Catch

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

ex.printStackTrace(); }

Page 4: Dosya İşlemleri

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" ); }

Page 5: Dosya İşlemleri

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()

Page 6: Dosya İşlemleri

Dosya Oluşturmak

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

Page 7: Dosya İşlemleri

Dosya Silmek

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

Page 8: Dosya İşlemleri

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

Page 9: Dosya İşlemleri

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

Page 10: Dosya İşlemleri

Dosya Yazma 1

try { FileOutputStream fos = new FileOutputStream(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(); }

Page 11: Dosya İşlemleri

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(); }

Page 12: Dosya İşlemleri

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(); }

Page 13: Dosya İşlemleri

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(); }