Dosya İşlemleri
-
Upload
beau-dixon -
Category
Documents
-
view
35 -
download
0
description
Transcript of Dosya İşlemleri
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.
Try-Catch
• try { //komutlar } catch (Exception ex) { System.out.println(“Hata Bulundu");
ex.printStackTrace(); }
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" ); }
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()
Dosya Oluşturmak
File f = new File(dosyaAdi); // Dosya nesnesi if(!f.exists()){ //Dosya zaten var mı f.createNewFile(); //Dosyayı oluştur }
Dosya Silmek
File f = new File(dosyaAdi); //Dosya Nesnesi if(f.exists()){ //Dosya var mı f.delete(); //Dosyayı sil }
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(); }
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(); }
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(); }
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(); }
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(); }
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(); }