1 / 9

استفاده از فایلها در جاوا

استفاده از فایلها در جاوا. مظفر بگ محمدی دانشگاه ایلام. ذخیره ی اطلاعات در فایلها. فایلها بصورت دودویی در کامپیوتر ذخیره می شوند. انواع فایلها: فایل متنی فایل باینری. 110000. 110001. ‘0’. ‘1’. 110001110010 2. 3186 10. فایل متنی و باینری. فایل متنی هر 8 بیت یک کاراکتر است

ayame
Télécharger la présentation

استفاده از فایلها در جاوا

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. استفاده از فایلها در جاوا مظفر بگ محمدی دانشگاه ایلام

  2. ذخیره ی اطلاعات در فایلها • فایلها بصورت دودویی در کامپیوتر ذخیره می شوند. • انواع فایلها: • فایل متنی • فایل باینری

  3. 110000 110001 ‘0’ ‘1’ 1100011100102 318610 فایل متنی و باینری • فایل متنی • هر 8 بیت یک کاراکتر است • مثل: ‘0’ = 48, ‘1’ = 49 • فایل باینری • انواع دیگر فایلها • مثلاً ممکن است که داده بصورت اعداد طبیعی 16 بیتی ذخیره شده باشد.

  4. BufferedReader ‘A’ 00100000 ‘N’ 01001110 char stream 01000001 ‘ ‘ byte stream FileReader “AN “ string خواندن متن از یک فایل File 01000001 01001110 01000001 :

  5. 01000001 FileWriter PrintWriter 01001110 ‘ ‘ ‘N’ ‘A’ 00100000 byte stream char stream نوشتن متن در یک فایل Primitives, Strings, Objects “AN “ File 01000001 01001110 01000001 :

  6. کلاس IntegerWrapper class IntegerWrapper { private int num; public IntegerWrapper () { num = (int) (Math.random() * 100); } public void setNum (int no) { num = no; } public intgetNum () { return num; } }

  7. کلاس SimpleIO import java.io.*; class SimpleIO{ public static void main (String [] argv){ IntegerWrapper iw1 = new IntegerWrapper (); IntegerWrapper iw2 = new IntegerWrapper (); String filename = "data"; PrintWriter pw; FileWriter fw; BufferedReader br; FileReader fr;

  8. کلاس SimpleIO (2) try { fw = new FileWriter (filename); pw = new PrintWriter (fw); System.out.println("Written to file: " + iw1.getNum()); pw.println(iw1.getNum()); System.out.println("Written to file: " + iw2.getNum()); pw.println(iw2.getNum()); pw.close();

  9. کلاس SimpleIO (3) fr = new FileReader(filename); br = new BufferedReader(fr); System.out.println("Read from file: " + br.readLine()); System.out.println("Read from file: " + br.readLine()); } catch (IOException e) { System.out.println(“File IO error: Exception thrown"); e.printStackTrace(); } } }

More Related