You are on page 1of 44

Ch08

Java /

Reader/Writer
InputStream/OutputStream

11/10/15

Java /
Java I/O Java
Input/Output /
Java
Class Library
Stream

11/10/15


Stream Unix

Java

11/10/15

java.io -
Java java.io
Java
Character Stream
Byte Stream

11/10/15

java.io -

Character Stream
Java 1.1
Humanreadable Reader/Writer
16

11/10/15

java.io -

Byte Stream

Machine-formatted
8

/
InputStream/OutputStream

11/10/15

java.io File

File
Java

11/10/15

- File

Java File

File f = new File(String);

File File()

11/10/15

boolean isDirectory()
String[] list()

11/10/15

File true
false
String

10


File

boolean exists()

true
false
boolean isFile()
File true false

String getAbsolutePath()

String getAbsoluteFile() getAbsolutePath()

String getName()

String.getParent()
File()
null
String getPath()
File()

long length()
0
boolean canRead()
true false
boolean
canWrite()
11/10/15
true false

11


File

boolean renameTo(File)

11/10/15

File
true false

12


File

boolean delete()

11/10/15

true false

13


File

boolean mkdir()
boolean mkdirs()

11/10/15

true false

14

-
Java File

File Swing

Java Swing
JList AWT List
File list()

List lstItems = new List(elementCount);

List lstItems
elementCount
11/10/15

15

int getSelectedIndex()
String getItem(int)
void removeAll()
void remove(int)
void add(String)
void add(String, int)

11/10/15

int 0

int
String List int
int

16

11/10/15

17

-
Java System
System.out System.in
System java.io
java.lang
Java
System.out println() print()

System.out.println(".");
System.out.print(".");

11/10/15

18

-
Reader/Writer

int read()
int read(char[])
int read(char[], int, int)

1
-1
2 int 3
int
void write(int)

void write(char[])
1
void write(char[], int, int) 2 int 3
int
void flush()

void close()

11/10/15

19

-
Java
System.out
OutputStreamWriter
BufferedWriter

BufferedWriter output = new


BufferedWriter(new
OutputStreamWriter(System.out));

11/10/15

20

-
BufferedWriter Writer
write()

void write(String)
void write(String, int, int)

11/10/15

1
2 int 3
int

21

1:
1. import java.io.*;
2. //
3. public class Ch10_05
4. { //
5.
public static void main(String[] args)
6.
throws Exception
7.
{ // BufferedWriter
8.
BufferedWriter output = new BufferedWriter(
9.
new OutputStreamWriter(System.out));
10.
String str = "Java 2 2e";
11.
output.write(str);
//
12.
output.close();
//
13.
}
14.}

11/10/15

22

-
Java
System.in InputStreamReader

BufferedReader

BufferedReader input = new


BufferedReader(new
InputStreamReader(System.in));

11/10/15

23

-
BufferedReader Reader
readLine()

String readLine()

11/10/15

\r\n
\r\n

24

2:
1. import java.io.*;
2. //
3. public class Ch10_06
4. { //
5.
public static void main(String[] args)
6.
throws Exception
7.
{ // BufferedReader
8.
BufferedReader input = new BufferedReader(
9.
new InputStreamReader(System.in));
10.
String str;
11.
System.out.print(" : ");
12.
System.out.flush();
//
13.
str = input.readLine(); //
14.
input.close();
//
15.
System.out.println(" : " + str);
16.
}
17.}

11/10/15

25


Java
FileWriter
BufferedWriter

BufferedWriter output = new


BufferedWriter(new FileWriter(file));
file
write()

11/10/15

26


Java FileReader

BufferedReader

BufferedReader input = new BufferedReader(new


FileReader(name));
name File
while
while ( (str = input.readLine()) != null )
{ }
11/10/15

27


Java 14-4-1 14-42
FileReader FileWriter

while
while ( (ch = input.read()) != -1 )
output.write(ch);

read()
write()
11/10/15

28

3: = +

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.

import java.io.*;
//
public class Ch10_07
{ //
public static void main(String[] args)
throws Exception
{ String sour_path = "Test10_07_01.txt";
String dest_path = "Test10_07_02.txt";
File sour = new File(sour_path); // File
File dest = new File(dest_path);
if ( sour.exists() )
{ // BufferedReader
BufferedReader input = new BufferedReader(
new FileReader(sour));
// BufferedWriter
BufferedWriter output = new BufferedWriter(
new FileWriter(dest));
int ch;
System.out.println(" ..." + dest);
//
while ( (ch = input.read()) != -1 )
output.write(ch);
input.close();
//
output.close();
System.out.println(" ..." + dest);
}
else
System.out.println(" ["+sour+" !");
}
}

11/10/15

29

InputStream/OutputStream -
Java
InputStream/OutputStream

InputStream/OutputStream
Reader/Writer

11/10/15

30

InputStream/OutputStream -

int read()
int read(byte[])
int read(byte[], int, int)
void write(int)
void write(byte[])
void write(byte[], int, int)

11/10/15

1
-1
2 int
3 int

1
2 int
3 int

31

InputStream/OutputStream
I/O Java
FileOutputStream FileInputStream

FileOutputStream output = new


FileOutputStream(file);

FileInputStream input = new


FileInputStream(name);

2
file

name File
11/10/15

32

-
Java Filter Stream

11/10/15

33

-
DateInputStream/DataOutputStream
FilterInputStream/FilterOutputStream

DataOutputStream output = new


DataOutputStream(
new
FileOutputStream(file));

DataInputStream input = new DataInputStream(
new
FileInputStream(name));

11/10/15

34

Sequential Access Streams

Random Access
File

File Pointer

11/10/15

35

-
RandomAccessFile
DataInput DataOutput

RandomAccessFile input = new


RandomAccessFile(file,"rw");
RandomAccessFile input = new
RandomAccessFile(file,"r");

11/10/15

36

-
RandomAccessFile

int skipBytes(int)
void seek(long)
long getFilePointer()

11/10/15

int
long
0
long

37

1:
,
_ , _ , _

11/10/15

38

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.

/* : Ch10_01.java */
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
// JFrame , ActionListener
public class Ch10_01 extends JFrame
implements ActionListener
{
private JMenuItem newItem, openItem, saveItem,
exitItem;
private Container c;
private JTextArea area;
private JScrollPane scroll;
private JToolBar toolBar;
// JFileChooser
final JFileChooser jfc = new JFileChooser();

11/10/15

18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.

//
public Ch10_01()
{ super (" ");
c = getContentPane();
c.setBackground(Color.white);
JMenuBar jmb = new JMenuBar();
setJMenuBar(jmb);
//
//
JMenu file = new JMenu(" (F)");
file.setMnemonic(KeyEvent.VK_F);

newItem = new JMenuItem(" (N)",


KeyEvent.VK_N);
30.
openItem = new JMenuItem("
(O)", KeyEvent.VK_O);
31.
saveItem = new JMenuItem("
(S)",KeyEvent.VK_S);
32.
exitItem = new JMenuItem("
(X)",KeyEvent.VK_X);
33.
34.
newItem.addActionListener(this);
35.
openItem.addActionListener(this);
36.
saveItem.addActionListener(this);
37.
exitItem.addActionListener(this);
29.

39

38.
39.
40.
41.
42.

file.add(newItem);
file.add(openItem);
file.add(saveItem);
file.addSeparator(); //
file.add(exitItem);
jmb.add(file); // file
//
JMenu help = new JMenu(" (H)");
help.setMnemonic(KeyEvent.VK_H);
JCheckBoxMenuItem check;
check = new JCheckBoxMenuItem(" ");
check.addActionListener(this);
help.add(check);

43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.

jmb.add(help);

//
area = new JTextArea(15,30);
scroll = new JScrollPane(area);
c.add(scroll, BorderLayout.CENTER);
}

11/10/15

59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.

//
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() == newItem)
{
area.append(" \n");
}
if (evt.getSource() == openItem)
{
int n = jfc.showOpenDialog(Ch10_01.this);
if ( n == JFileChooser.APPROVE_OPTION )
{ File file = jfc.getSelectedFile();
area.append(" : ");
area.append(file.getName() + "\n");
}
}
if (evt.getSource() == saveItem)
{
int m = jfc.showSaveDialog(Ch10_01.this);
if ( m == JFileChooser.APPROVE_OPTION )
{ File file = jfc.getSelectedFile();
area.append(" : ");
area.append(file.getName() + "\n");
}
}
if (evt.getSource() == exitItem)
System.exit(0);
}

40

87. //
88.
public static void
main(String[] args)
89.
{ // Swing
90.
Ch10_01 app = new
Ch10_01();
91.
// ,

92.
app.addWindowListener(new
WindowAdapter()
93.
{ public void
windowClosing(WindowEvent evt)
94.
{ System.exit(0); }
95.
});
96.
app.setSize(300,200); //

97.
app.setVisible(true); //

98.
}
99. }

11/10/15

41

import java.io.*;
class RFile
{
public static void main(String [] args) throws Exception
{
FileReader fr = new FileReader("E:/wang/JAVA/Java(3)/RWFile/RWTest.txt");
BufferedReader br = new BufferedReader(fr);
String str;
while((str = br.readLine()) != null)
{ System.out.println(str);
}
}
}

11/10/15

42

import java.io.*;
class RWFile
{
public static void main(String [] args) throws Exception
{
//File ff = new File("E:/wang/JAVA/Java(3)/RWFile/WTest.txt");
FileReader fr = new FileReader("E:/wang/JAVA/Java(3)/RWFile/RWTest.txt");
FileWriter fw = new FileWriter("E:/wang/JAVA/Java(3)/RWFile/WTest.txt");
BufferedReader br = new BufferedReader(fr);
BufferedWriter bw = new BufferedWriter(fw);
String str;
while((str = br.readLine()) != null)
{ bw.write(str + "\n");
}
bw.close();
br.close();
}
}

11/10/15

43

import java.io.*;
import java.net.*;
class RWWeb
{ public static void main(String [] args) throws Exception
{ String web1 = "http://www.nkmu.edu.tw/history.asp" ;//UTF-8
URL Rweb = new URL(web1);
FileOutputStream fw = new FileOutputStream("E:/wang/JAVA/Java(3)/RWFile/WWebTest.txt");
BufferedOutputStream bos = new BufferedOutputStream(fw);
BufferedInputStream bis = new BufferedInputStream(Rweb.openStream());
byte b[] = new byte[4096] ;
int count;
while((count = bis.read(b, 0, 4096)) != -1)
{ bos.write(b, 0, count);
}
bis.close();
bos.close();
}
}

11/10/15

44

You might also like