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

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Lecture 15: I/O and Parsing
Files from Ch4. File Input and Output  Reentering data all the time could get tedious for the user.  The data can be saved to a file. Files can be input.
Lab1: File I/O and Streams Lecturer: Mauro Conti T.A.: Eyüp S. Canlar.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism.
James Tam Simple file handling in Java Simple File Input And Output Types of Java files Simple file output in Java Simple file input in Java.
Network Read/Write. Review of Streams and Files java.io package InputStream and OutputStream classes for binary bytes Reader and Writer classes for characters.
 We can use a combination of the File and FileOutputStream to write a series of bytes to a file.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
James Tam Simple File Input And Output You will learn how to write to and read from text and serialized files in Java.
1 Text File I/O  I/O streams  Opening a text file for reading  Closing a stream  Reading a text file  Writing and appending to a text file.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
Java I/O – what does it include? Command line user interface –Initial arguments to main program –System.in and System.out GUI Hardware –Disk drives ->
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java Writing and reading objects to and from.
فایل پردازی در C File based Programming in C. انواع فایل متنی –سرعت بالا –حجم کمتر –امکان دسترسی تصادفی –حفظ امنیت داده ها دودویی (باینری) –امکان باز.
Reading/Writing Files, Webpages CS2110, Recitation 10 1.
Performance measurements for inter-process communication.
Using java’s Scanner class To read from input and from a file. (horstmann ch04 and ch 17)
פיתוח מונחה עצמים – שפת JAVA קבצים. References קורס "שיטות בהנדסת תוכנה", הפקולטה למדעי המחשב, הטכניון. קורס "מערכות מידע מבוזרות", הפקולטה להנדסת תעשייה.
Java Exception Handling Handling errors using Java’s exception handling mechanism.
Two Ways to Store Data in a File Text format Binary format.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
File IO Basics By Dan Fleck Coming up: Data Streams.
Introduction to Programming G50PRO University of Nottingham Unit 11 : Files Input/Ouput Paul Tennent
By Rachel Thompson and Michael Deck.  Java.io- a package for input and output  File I/O  Reads data into and out of the console  Writes and reads.
Chapter 15 Text Processing and File Input/Output Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
1 بنام خدا زبان برنامه نویسی C (21814( Lecture 9 Selected Topics.
CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.
Lecture 5 I/O and Parsing
Reading/Writing Files, Webpages CS2110, Recitation 8 1.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
مظفر بگ محمدی دانشگاه ایلام Generics. کدهای عمومی 2 یکی از اهداف OOP ایجاد قابلیت نوشتن برنامه های عمومی با قابلیت استفاده ی مجدد است. کدهای چندریخت تا.
Simple File Input And Output You will learn how to write to and read from text and serialized files in Java.
Java Input/Output. Java Input/output Input is any information that is needed by your program to complete its execution. Output is any information that.
CSI 3125, Preliminaries, page 1 Files. CSI 3125, Preliminaries, page 2 Reading and Writing Files Java provides a number of classes and methods that allow.
1 Text File Input and Output. Objectives You will be able to Write text files from your Java programs. Read text files in your Java programs. 2.
James Tam Java Exception Handling Handling errors using Java’s exception handling mechanism.
Lecture 8: I/O Streams types of I/O streams Chaining Streams
Java Exception Handling
עקרונות תכנות מונחה עצמים תרגול 7: כתיבה לקבצים
Lesson 8: More File I/O February 5, 2008
Reading from a file A file is typically stored on your computers hard drive. In the simplest case, lets just assume it is text. For a program to use.
CSE 341, S. Tanimoto Java networking-
Simple File Input And Output
Strings and File I/O.
Lecture Note Set 1 Thursday 12-May-05
04/14/14 Exceptions.
הרצאה 12: קבצים וחריגות (Exceptions)
CHAPTER 5 (PART 2) JAVA FILE INPUT/OUTPUT
Reading and Writing Text Files
September 9, 2008 Lecture 5 – More IO.
Text File Read and Write Method
Simple File Input And Output
مباني كامپيوتر و برنامه سازي Basics of Computer and Programming
مباني كامپيوتر و برنامه سازي Basics of Computer and Programming
September 9, 2008 Lecture 5 – More IO.
Simple File Input And Output
Web Design & Development Lecture 8
Exceptions one last time…
ECE 122 April 14, 2005.
EEC 484/584 Computer Networks
Exceptions and Exception Handling
David Davenport Spring 2005
Presentation transcript:

استفاده از فایلها در جاوا مظفر بگ محمدی دانشگاه ایلام Simple file handling in Java

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

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

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

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

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

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

کلاس 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(); Simple file handling in Java

کلاس 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();