JAVADA STSNALAR VE DOSYA LEMLER Dr Galip Aydn

  • Slides: 24
Download presentation
JAVA’DA İSTİSNALAR VE DOSYA İŞLEMLERİ Dr. Galip Aydın

JAVA’DA İSTİSNALAR VE DOSYA İŞLEMLERİ Dr. Galip Aydın

Exceptions - İstisnalar • An exception is an error that occurs at runtime. java.

Exceptions - İstisnalar • An exception is an error that occurs at runtime. java. lang. Object | It is either generated by the Java Virtual +--java. lang. Throwable Machine (VM) in response to an unexpected | +--java. lang. Exception condition or it is generated by your code as a | | | +--java. lang. Class. Not. Found. Exceptionresult of executing a throw statement. | | | +--java. io. IOException | | | +--java. io. File. Not. Found. Exception | | | +--java. lang. Runtime. Exception | | +--java. lang. Null. Pointer. Exception | | +--java. lang. Index. Out. Of. Bounds. Exception | | +--java. lang. Array. Index. Out. Of. Bounds. Exception | +--java. lang. Error | +--java. lang. Virtual. Machine. Error | +--java. lang. Out. Of. Memory. Error

Exception Hierarchy Throwable Error IOException Class. Not Found. Exception Clone. Not Supported Exception Runtime.

Exception Hierarchy Throwable Error IOException Class. Not Found. Exception Clone. Not Supported Exception Runtime. Exception Arithmetic. Exception EOFException Class. Cast. Exception File. Not. Found. Exception Illegal. State. Exception Malformed. URLException Number. Format. Exception Index. Out. Of. Bounds. Exception Unknown. Host. Exception Array. Index. Out. Of. Bounds. Exception No. Such. Element. Exception Null. Pointer. Exception

Try-Catch public File. Reader(String file. Name) throws File. Not. Found. Exception try { //komutlar

Try-Catch public File. Reader(String file. Name) throws File. Not. Found. Exception try { //komutlar } catch (Exception ex) { System. out. println(“Hata Bulundu"); ex. print. Stack. Trace(); }

Birden Fazla İstisnanın Yakalanması try { //. . . } catch ( File. Not.

Birden Fazla İstisnanın Yakalanması try { //. . . } catch ( File. Not. Found. Exception e ) { System. out. println( e. get. Message()); } 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(dosya. Adi);

Dosya İşlemleri • import java. io. File; • File dosya = new File(dosya. Adi); – dosya. get. Absolute. Path() dosya. get. Name() dosya. get. Parent() dosya. exists() dosya. can. Read() dosya. can. Write() dosya. is. Directory() dosya. is. File() dosya. last. Modified() dosya. length()

Yeni Dosya Oluşturma File f = new File(dosya. Adi); // Dosya nesnesi if(!f. exists()){

Yeni Dosya Oluşturma File f = new File(dosya. Adi); // Dosya nesnesi if(!f. exists()){ //Dosya zaten var mı f. create. New. File(); //Dosyayı oluştur } File dosya = new File(“ornek. dat”); File dosya = new File (“C: /Ornek. Program/test. dat”); Bulunulan klasordeki ornek. dat dosyasını açar C: Ornek. Program klasöründeki test. dat dosyasını açar. Dosyanın adresi / ayracı ile verilir.

Dosya İşlemleri dosya değişkeni gerçekten var olan bir dosyayı mı gösteriyor. if ( dosya.

Dosya İşlemleri dosya değişkeni gerçekten var olan bir dosyayı mı gösteriyor. if ( dosya. exists( ) ) { if ( dosya. is. File() ) { File klasor = new File("C: /Programlarım/java"); dosya bir doysa mı yoksa bir klasör mü. String dosyalar[] = klasor. list(); for (int i = 0; i < dosyalar. length; i++) { System. out. println(dosyalar[i]); } C: Programlarımjava verilen klasördeki bütün dosyaları listeler.

Dosya Silme File f = new File(dosya. Adi); //Dosya Nesnesi if(f. exists()){ //Dosya var

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

Stream A stream serves as a connection between your program and an external source

Stream A stream serves as a connection between your program and an external source or destination for bytes and bits could be standard input or output, files, network connections, or other programs

Dosya Okuma 1 try { File. Input. Stream fis = new File. Input. Stream(dosya.

Dosya Okuma 1 try { File. Input. Stream fis = new File. Input. Stream(dosya. Adi); int ch = 0; while (ch != -1) { ch = fis. read(); char karakter = (char)ch; System. out. print(karakter); } fis. close(); } catch (Exception e) { e. print. Stack. Trace(); }

//dosya ve stream olustur File dosya = new File(“ornek. data"); File. Input. Stream giris.

//dosya ve stream olustur File dosya = new File(“ornek. data"); File. Input. Stream giris. Stream = new File. Input. Stream(dosya); //verileri okumak icin bir dizi olustur int dosya. Boyutu = (int)dosya. length(); byte[] byte. Dizisi = new byte[dosya. Boyutu]; //veriyi oku ve goster giris. Stream. read(byte. Dizisi); for (int i = 0; i < dosya. Boyutu; i++) { System. out. println(byte. Dizisi[i]); } //okuma bitti stream’I kapat giris. Stream. close();

Dosya Okuma 2 try { File. Reader fr = new File. Reader(dosya. Adi); Buffered.

Dosya Okuma 2 try { File. Reader fr = new File. Reader(dosya. Adi); Buffered. Reader br = new Buffered. Reader(fr); while(br. ready()){ String satir = br. read. Line(); System. out. println(satir); } fr. close(); br. close(); } catch (Exception e) { e. print. Stack. Trace(); }

Dosya Yazma 1 try { File. Output. Stream fos = new File. Output. Stream(dosya.

Dosya Yazma 1 try { File. Output. Stream fos = new File. Output. Stream(dosya. Adi); String yazi = "Bu satir dosyaya yazilacaknaltina da bu satir yazilacak. "; fos. write(yazi. get. Bytes()); fos. flush(); fos. close(); } catch (Exception e) { e. print. Stack. Trace(); }

//Yazilacak dosyayi olustur File cikis. Dosyasi = new File("sample 1. data"); File. Output. Stream

//Yazilacak dosyayi olustur File cikis. Dosyasi = new File("sample 1. data"); File. Output. Stream cikis. Stream = new File. Output. Stream( cikis. Dosyasi ); //kaydedilecek veri byte[] byte. Dizisi = {10, 20, 30, 40, 50, 60, 70, 80}; //verileri stream’e yaz cikis. Stream. write( byte. Dizisi ); //stream kapat cikis. Stream. close();

Data. Output. Stream 16 Typical sequence: File cikis. Dosyasi = new File( “ornek. data"

Data. Output. Stream 16 Typical sequence: File cikis. Dosyasi = new File( “ornek. data" ); File. Output. Stream cikis. Dosyasi. Stream = new File. Output. Stream(cikis. Dosyasi); Data. Output. Stream out. Data. Stream = new Data. Output. Steam(cikis. Dosyasi. Stream);

Data. Input. Stream 17 Typical sequence: File okunacak. Dosya = new File( "sample 2.

Data. Input. Stream 17 Typical sequence: File okunacak. Dosya = new File( "sample 2. data" ); File. Input. Stream oku. Dosya. Stream = new File. Input. Stream(in. File); Data. Input. Stream in. Data. Stream = new Data. Input. Steam(oku. Dosya. Stream); CS-1020 MSOE Portions adapted with permission from the textbook author.

Dosya Yazma 2 try { File. Writer fw = new File. Writer(dosya. Adi); Buffered.

Dosya Yazma 2 try { File. Writer fw = new File. Writer(dosya. Adi); Buffered. Writer bw = new Buffered. Writer(fw); bw. write("Bu satiri yaznyeni satira gec. "); bw. flush(); bw. close(); } catch (Exception e) { e. print. Stack. Trace(); }

Scanner ile Dosya Okuma 1 try { Scanner s = new Scanner( new File(dosya.

Scanner ile Dosya Okuma 1 try { Scanner s = new Scanner( new File(dosya. Adi)); String dosya. Icerigi = s. use. Delimiter("\A"). next(); System. out. println(dosya. Icerigi); s. close(); } catch (Exception e) { e. print. Stack. Trace(); }

import java. io. *; class Test. Scanner { public static void main (String[] args)

import java. io. *; class Test. Scanner { public static void main (String[] args) throws IOException { //Scanner nesnesi olustur Scanner scanner = new Scanner(new File(“ornek. data")); //integer oku int i = scanner. next. Int(); //diger veri turleri de benzer sekilde okunur scanner. close(); } }

Scanner ile Dosya Okuma 2 try { Scanner s = new Scanner( new File(“test.

Scanner ile Dosya Okuma 2 try { Scanner s = new Scanner( new File(“test. txt")); while(s. has. Next()){ String satir = s. next. Line(); System. out. println(satir); } scanner. close(); } catch (Exception e) { e. print. Stack. Trace(); }

import java. util. Scanner; import java. io. File; import java. io. IOException; public class

import java. util. Scanner; import java. io. File; import java. io. IOException; public class Rakamlari. Oku { public static void main(String[] args) { try { Scanner s = new Scanner( new File(“rakamlar. dat") ); while( s. has. Next. Int() ) { System. out. println( s. next. Int() ); } s. close(); } catch(IOException e) { System. out. println( e ); } } }

//Bir dosyaya 100 tane rastgele int yazan program import java. io. Print. Stream; import

//Bir dosyaya 100 tane rastgele int yazan program import java. io. Print. Stream; import java. io. IOException; import java. io. File; import java. util. Random; public class Dosyaya. Yaz { public static void main(String[] args) { try { Print. Stream writer = new Print. Stream( new File(“sayilar. txt")); Random r = new Random(); final int LIMIT = 100; for(int i = 0; i < LIMIT; i++) { writer. println( r. next. Int() ); } writer. close(); } } catch(IOException e) { System. out. println(“Bir hata olustu”); }

Scanner kullanarak sadece kelimeleri okumek icin: Scanner s = new Scanner( new File(“test. txt”)).

Scanner kullanarak sadece kelimeleri okumek icin: Scanner s = new Scanner( new File(“test. txt”)). use. Delimiter("\W");