Download System.out.println(“Hello”)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
‫פיתוח מונחה עצמים –‬
‫שפת ‪JAVA‬‬
‫קבצים‬
‫‪References‬‬
‫‪ ‬קורס "שיטות בהנדסת תוכנה"‪ ,‬הפקולטה למדעי‬
‫המחשב‪ ,‬הטכניון‪.‬‬
‫‪ ‬קורס "מערכות מידע מבוזרות"‪ ,‬הפקולטה‬
‫להנדסת תעשייה וניהול‪ ,‬הטכניון‪.‬‬
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.
Introduction - 2
 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:
Reading:
Writing:
Stream
Source
Stream
reads
Program
Dest.
writes
Program
open a stream
while more information
read information
open a stream
while more information
write information
close the stream
close the stream
Standard Input / Output Streams in Java
Writing to the screen:
Reading from the screen:
System.out.print(…);
System.in.read(byte[]);
System.out.println(…);
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.
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();
}
}
}
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.
Character Streams Hierarchy
BufferedReader
LineNumberReader
CharArrayReader
Reader
InputStreamReader
FilterReader
FileReader
PushbackReader
PipedReader
StringReader
BufferedWriter
CharArrayWriter
Writer
OutputStreamWriter
FilterWriter
PipedWriter
StringWriter
FileWriter
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();
}
}
Advanced Usage
//extracts from a class – not a whole class
//args <name of file><name of delimiter>
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] +")");
}
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();
}
}
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/