Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter9 The Java I/O System.

Similar presentations


Presentation on theme: "Chapter9 The Java I/O System."— Presentation transcript:

1 Chapter9 The Java I/O System

2 ● Often a program needs to bring in information from an external source or to send out information to an external destination. ● The information can be anywhere: in a file, on disk, somewhere on the network, in memory, or in another program. Also, the information can be of any type: objects, characters, images, or sounds.

3 I/O: Command-Line Arguments Console I/O Files and File I/O

4 Text-Based Applications

5

6 public class TestArgs {
public static void main(String[] args) { for ( int i = 0; i < args.length; i++ ) { System.out.println("args[" + i + "] is *" + args[i] + "*"); } java TestArgs arg1 arg2 "another arg“ Here is an except of output: args[0] is *arg0* args[1] is *arg1* args[2] is *12*

7

8

9 import java.util.Enumeration; public class TestProperties {
import java.util.Properties; import java.util.Enumeration; public class TestProperties { public static void main(String[] args) { Properties props = System.getProperties(); Enumeration prop_names = props.propertyNames(); while ( prop_names.hasMoreElements() ) { String prop_name = (String) prop_names.nextElement(); String property = props.getProperty(prop_name); System.out.println("property ’" + prop_name + "’ is ’" + property + "’"); } java -DmyProp=theValue TestProperties

10 DOS:

11 EditPlus: ---------- java ----------
property ’ java.runtime.name’ is ’ Java(TM) 2 Runtime Environment, Standard Edition’ property ’sun.boot.library.path’ is ’C:\j2sdk1.4.2_06\jre\bin’ property ’java.vm.version’ is ’1.4.2_06-b03’ property ’java.vm.vendor’ is ’Sun Microsystems Inc.’ property ’java.vendor.url’ is ’ property ’path.separator’ is ’;’ property ’java.vm.name’ is ’Java HotSpot(TM) Client VM’ property ’file.encoding.pkg’ is ’sun.io’ property ’user.country’ is ’CN’ …… property ’sun.io.unicode.encoding’ is ’UnicodeLittle’ property ’sun.cpu.isalist’ is ’pentium i486 i386’

12

13

14 Reading Java API : BufferedReader – readLine( )
import java.io.*; public class KeyboardInput { public static void main (String args[]) { String s; // Create a buffered reader to read // each line from the keyboard. InputStreamReader ir = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(ir); System.out.println("Unix: Type ctrl-d or ctrl-c to exit." + "\nWindows: Type ctrl-z to exit");

15 try { // Read each input line and echo it to the screen. //Throws: IOException - If an I/O error occurs s = in.readLine(); while ( s != null ) { System.out.println("Read: " + s); } // Close the buffered reader. in.close(); } catch (IOException e) { // Catch any IO exceptions. e.printStackTrace();

16

17

18

19

20

21 //Read by Buffered Reader
import java.io.*; public class ReadFile { public static void main (String [] args) { // Create file File file = new File(args[0]); try { // Create a buffered reader to read each line from a file. FileReader(file)-if the specified file is not found throws FileNotFoundException BufferedReader in = new BufferedReader(new FileReader(file)); String s; // Read each line from the file and echo it to the screen. s = in.readLine(); while (s != null) { System.out.println(s); }

22 // Close the buffered reader, which also closes the file reader.
in.close(); } catch (FileNotFoundException e1) { // If this file does not exist System.err.println("File not found: " + file); } catch (IOException e2) { // Catch any other IO exceptions. e2.printStackTrace(); } //java ReadFile E:\zhangjm\java\JavaLecture\教案\chapter9\code9\DaiWS.txt

23 Result: java 戴望舒 给什么智慧给我, 小小的白蝴蝶。 翻开了空白之页, 合上了空白之页? 翻开的书页: 寂寞; 合上的书页: 寂寞. Normal Termination Output completed (0 sec consumed).

24 // Read file by FileReader
import java.io.*; public class ReadFile02 { public static void main (String [] args) { // Create file File file = new File(args[0]); char [] ch = new char[8]; try { FileReader in = new FileReader(file); int s; s = in.read(ch); while (s != -1) { System.out.println(ch); }

25 // Close the buffered reader, which also closes the file reader.
in.close(); } catch (FileNotFoundException e1) { // If this file does not exist System.err.println("File not found: " + file); } catch (IOException e2) { // Catch any other IO exceptions. e2.printStackTrace(); } //java ReadFile E:\zhangjm\java\JavaLecture\教案\chapter9\code9\DaiWS.txt

26 import java.io.*; public class WriteFile { public static void main (String args[]) { // Create file File file = new File(args[0]); try { // Create a buffered reader to read each line from standard in. BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // Create a print writer on this file. PrintWriter out = new PrintWriter(new FileWriter(file));

27 String s; System.out.print("Enter file text. "); System.out.println("[Type cntl-d (or ctrl-z) to stop.]"); // Read each input line and echo it to the screen. while ((s = in.readLine()) != null) { out.println(s); } // Close the buffered reader and the file print writer. in.close(); out.close(); } catch (IOException e) { // Catch any IO exceptions. e.printStackTrace();

28 Advanced I/O Streams

29

30

31

32

33

34

35

36

37 import java.io.*; public class TestNodeStreams { public static void main(String[] args) { try { FileReader input = new FileReader(args[0]); FileWriter output = new FileWriter(args[1]); char[] buffer = new char[128]; int charsRead; // read the first buffer // 除尾次外,每次读buffer长个字符 charsRead = input.read(buffer);

38 // write the buffer out to the output file
while ( charsRead != -1 ) { // write the buffer out to the output file output.write(buffer, 0, charsRead); // read the next buffer charsRead = input.read(buffer); } input.close(); output.close(); } catch (IOException e) { e.printStackTrace();

39 public class TestBufferedStreams {
import java.io.*; public class TestBufferedStreams { public static void main(String[] args) { try { FileReader input = new FileReader(args[0]); BufferedReader bufInput = new BufferedReader(input); FileWriter output = new FileWriter(args[1]); BufferedWriter bufOutput = new BufferedWriter(output); String line; // read the first line line = bufInput.readLine(); }

40 while ( line != null ) { // write the line out to the output file bufOutput.write(line, 0, line.length()); bufOutput.newLine(); // read the next line line = bufInput.readLine(); } bufInput.close(); bufOutput.close(); } catch (IOException e) { e.printStackTrace();

41

42 An InputStreamReader is a bridge
peek:从存储器读一个字节的命令。取数。 An InputStreamReader is a bridge from byte streams to character streams

43 public class InputStreamReader extends Reader:
An InputStreamReader is a bridge from byte streams to character streams. public class OutputStreamWriter extends Writer: An OutputStreamWriter is a bridge from character streams to byte streams

44

45

46

47


Download ppt "Chapter9 The Java I/O System."

Similar presentations


Ads by Google