Presentation is loading. Please wait.

Presentation is loading. Please wait.

פיתוח מונחה עצמים – שפת JAVA קבצים. References קורס "שיטות בהנדסת תוכנה", הפקולטה למדעי המחשב, הטכניון. קורס "מערכות מידע מבוזרות", הפקולטה להנדסת תעשייה.

Similar presentations


Presentation on theme: "פיתוח מונחה עצמים – שפת JAVA קבצים. References קורס "שיטות בהנדסת תוכנה", הפקולטה למדעי המחשב, הטכניון. קורס "מערכות מידע מבוזרות", הפקולטה להנדסת תעשייה."— Presentation transcript:

1 פיתוח מונחה עצמים – שפת JAVA קבצים

2 References קורס "שיטות בהנדסת תוכנה", הפקולטה למדעי המחשב, הטכניון. קורס "מערכות מידע מבוזרות", הפקולטה להנדסת תעשייה וניהול, הטכניון.

3 I/O Streams in Java Introduction - 1 Definition Stream is a flow of Data. characters read from a file bytes written to the network …  The information can be read/written sequentially from/to the stream.  The assumption made on the stream: the information passes in FIFO order.

4 Source Program Stream reads Dest. Program Stream writes  No matter where the data is coming from or going to and no matter what its type, the algorithms for sequentially reading and writing data are basically the same: Introduction - 2 Reading: open a stream while more information read information close the stream Writing: open a stream while more information write information close the stream

5 Standard Input / Output Streams in Java Writing to the screen: System.out.print(…); System.out.println(…); Reading from the screen: System.in.read(byte[]); System.in.read(byte[],int,int); Standard streams are automatically opened, so no need to open them explicitly. Writing System.out.print(“Hello\n”); is the same as writing System.out.println(“Hello”); You have to create byte[] array before calling read function. read function has to be surrounded by try{ }catch(..){ } block.

6 Example - Echo package test; import java.io.*; public class IOTest { public IOTest() { } public static void main(String[] args) { byte[] ch = new byte[80]; int num = 0; try { num = System.in.read(ch); System.out.println(new String(ch,0,num)); // String(byte[] bt, int offset,int length) } catch (IOException ex) { System.out.println("Can't read"); ex.printStackTrace(); }

7 java.io Classes Hierarchy The Streams can be divided into Character Streams and Byte Streams Character Streams: Reader and Writer are the abstract superclasses for character streams in java.io. Reader provides the API and partial implementation for readers-- streams that read 16-bit characters--and Writer provides the API and partial implementation for writers--streams that write 16-bit characters. Byte Streams: To read and write 8-bit bytes, programs should use the byte streams, descendants of InputStream and OutputStream. InputStream and OutputStream provide the API and partial implementation for input streams (streams that read 8-bit bytes) and output streams (streams that write 8-bit bytes). These streams are typically used to read and write binary data such as images and sounds.

8 Character Streams Hierarchy Writer BufferedWriter CharArrayWriter StringWriter FilterWriter OutputStreamWriter PipedWriter FileWriter Reader BufferedReader CharArrayReader StringReader FilterReader InputStreamReader PipedReader PushbackReader FileReader LineNumberReader

9 Example import java.io.*; //must be imported public class Copy { public static void main(String[] args) throws IOException { File inputFile = new File("farrago.txt"); File outputFile = new File("outagain.txt"); //FileOutputStream and FileInputStream for bytes //throws FileNotFoundException (descendent of IOException) FileReader in = new FileReader(inputFile ); FileWriter out = new FileWriter(outputFile); int c; while ((c = in.read()) != -1) out.write(c); // housekeeping in.close(); out.close(); }

10 Advanced Usage //extracts from a class – not a whole class //args import java.io.*; import java.util.StringTokenizer;... try { int lineNum, wordNum; String line; BufferedReader inStream = new BufferedReader(new FileReader(args[0])); lineNum = wordNum = 0; do { line = inStream.readLine(); if(line != null) { lineNum++; StringTokenizer st = new StringTokenizer(line,args[1]); wordNum += st.countTokens(); } }while(line != null); System.out.println("There are " + lineNum + " lines."); System.out.println("There are " + wordNum + " words."); }catch(FileNotFoundException fnfe) { System.out.println("File ("+ args[0] +") not found."); }catch(IOException ioe) { System.out.println("I/O error while reading file ("+ args[0] +")"); }

11 private void writeToFile(String from, String to){ try{ FileReader fr = new FileReader(from); BufferedReader br = new BufferedReader(fr); FileWriter fw = new FileWriter(to); String line = null, srt[]; String wline; int lineNum = wordNum = 0; while ( (line = br.readLine()) != null ) { str = line.split("\\s+"); wordNum += str.length; lineNum ++;} fw.write("There are " + lineNum + " lines."); fw.write("There are " + wordNum + " words."); fw.close(); fr.close(); } catch (IOException e){ System.out.println("Uh oh, got an IOException error!"); e.printStackTrace(); } }

12 IO - Advanced topics Redirecting standard IO –System contains setIn, setOut and setErr. System.setIn(new FileInputStream(“x.dat”)); System.setOut(new PrintStream(new FileOutputStream( "out.txt"))); –Standard streams could be from/to anything you want! Compression –Read about ZipInputStream and ZipOutputStream For more information: –jdk documentation –On-line tutorials : http://java.sun.com/docs/books/tutorial/


Download ppt "פיתוח מונחה עצמים – שפת JAVA קבצים. References קורס "שיטות בהנדסת תוכנה", הפקולטה למדעי המחשב, הטכניון. קורס "מערכות מידע מבוזרות", הפקולטה להנדסת תעשייה."

Similar presentations


Ads by Google