Title: I/O ?????
1I/O ?????
2stream ????
- stream
- ???? ??(source) ?? ??(destination)? ?? ?? ?? ???
???? ?? - ????? ????? ?? ?????? ???? ??? ?(sequence of
bytes)??. - ????? stream? ???? ??? ??.
- Stream programming? ??? ???? ???? ?(type)? ?? ??
???? ??? ?? ????? ????. - input stream? output stream
- input stream ??? ???? ??? ?? ?? ??? ?? data?
- ?? ? ??
- output stream ??? ?? ??? ? ?? ??(??, ?? ???)?
- data? ? ? ??
3FileInputStream FileOutputStream (1)
- FileInputStream
- ??? ??? ?? ??? FileInputStream ??? ??
- FileInputStream ???? ??? ???? ???
FileNotFoundException? ?? ???. - FileInputStream myStream new
FileInputStream(C\data\aaa.txt) - FileOutputStream
- ??? ??? ?? ??? FileOutputStream ??? ??
- ??? ?? ??? ???? ??? SecurityException? ?????.
- FileOutputStream myStream new
FileOutputStream(bbb.txt)
4FileInputStream FileOutputStream (2)
- import java.io. // command line
argument? ???? ?? ?? ?? -
// ??? ??? copy - public class FileCopy
- public static void main(String args) throws
Exception - if (args.length lt 2)
- System.out.println("Usage java CopyFile
file1 file2") - return
-
- FileInputStream fis new
FileInputStream(args0) //???? ? ?? ?? ?? - FileOutputStream fos new
FileOutputStream(args1) //???? ? ?? ??? ?? - int readByte 0
- while ((readByte fis.read()) ! -1)
// EOF?? 1byte? ??? - fos.write(readByte) // 1 byte?
?? -
- fis.close()
- fos.close()
- System.out.println(args0 " copied to
" args1) -
5DataInputStream DataOutputStream (1)
- FileInputStream, FileOutputStream - ?? byte???
I/O? ?? - DataInputStream, DataOutputStream - ?? primitive
I/O? ??? -
??? ?? - Java? ??? ???? ?? ? 16bit-UniCode??? ??
- ??? byte ??? I/O?? ?? ?? ??? ?? ??? ??.(?? gt
2byte) - I/O? ? ??? 16bit-Unicode? ???? I/O class?? ????.
- Reader, Writer ????? ?? ?? ???? 2byte ????
- ???? stream
6DataInputStream DataOutputStream (2)
- DataOutputStream Class Method summary
Method summary Method summary
writeByte(int value) Int ??? ?? ???? ???? ??.
writeBoolean(boolean value) Boolean?? true ?? 1?, false?? 0? ???? ? ??? ??? ??.
writeChar(int value) ??? ?? ? ???? ???? ??.
writeShort(int value) ??? ?? ? ???? ???? ??.
writeInt(int value) int ??? ? ??? ??? ???? ??.
writeLong(long value) Long ??? ?? ??? ??? ???? ??.
writeFloat(float value) Float ??? ? ???? ???? ??.
writeDouble(double value) Double ??? ?? ???? ???? ??.
7DataInputStream DataOutputStream (3)
- import java.io. //???
??? ???? ??? ??? ?? ??? ?? - public class ExDataStream
- public static void main(String args)
throws Exception - if (args.length lt 1 )
- System.out.println("???? java
ExDataStream.java aa.txt") - return
-
- FileOutputStream fos new
FileOutputStream(args0) - DataOutputStream dos new
DataOutputStream(fos) //privitive type size?
i/o? ?? - dos.writeUTF("???") // UTF-8 encoding
?? ??? ?? - dos.writeUTF("41456") // UTF-8?
16bit ????? 8bit ??? ?? - dos.writeInt(30)
- dos.close()
- FileInputStream fis new
FileInputStream(args0) - DataInputStream dis new
DataInputStream(fis) - System.out.println("?? "
dis.readUTF()) //??? ??? ??? ?? - System.out.println("?? "
dis.readUTF()) - System.out.println("?? "
dis.readInt()) - dis.close()
8reader writer
- import java.io. //?????? ???? ?? ?? ???
?? - public class StringInput
- public static void main(String args)
throws Exception - String inputString
- InputStreamReader isr new
InputStreamReader(System.in) - BufferedReader br new BufferedReader(isr)
- FileOutputStream fos new
FileOutputStream(args0) - OutputStreamWriter osr new
OutputStreamWriter(fos) - BufferedWriter bw new BufferedWriter(osr)
- inputString br.readLine()
//readLine()? ??? ?? ??? ??? ??? ?? ? ?? ??. -
- bw.write(inputString "\n") //??? ??? ??
- bw.close() //??? ?? stream object?
close? ?? ?? ?? stream object? close?? ??. - br.close()
-
9File Class
- File Class
- File Object? ???? ??? ?? ??? ?? ??? ???? ??????
???? ????. - File Object? ?? ?? ??? ????? ??? ? ??.
- ???? ?????? ???? ???? ??? ?? ? ?? ???, ?? ??? ???
? ? ?? ?? ?? ???? ????.
Method summary Method summary
exists() File ??? ???? ???? ????? ???? true, ??? false ??
isDirectory() File ??? ????? ???? true, ??? false ??
isFile() File ??? ??? ???? true, ??? false ??
canRead() File ??? ???? ??? ?? ? ??? true, ??? false ????. ??? ?? ?? SecurityException? ???? ? ??.
canWrite() File ??? ???? ??? ? ? ??? true, ??? false ?? ??. ??? ? ? ??? SecurityException? ???? ? ??.
10URL Class
- URL Class
- Java.net ???? ?? ?? ??.
- URL??? ?? URL? ?? ??? ??? ???, ???? ??? ??? ???
????? ?? ? ??. - URL? ???? ??? ???? ??? ??? ???? ? ? ??.
- ??? ?? URL? local pc? ???? ?? ? ??.
- import java.io. // ???
?? ??? ???? ???? ?? - import java.net. //URL
Class?????? ??? import??. - public class GetIndexHtml
- public static void main(String args)
throws Exception - byte inputString new byte1024
- InputStream is (new URL(args0)).openS
tream() - FileOutputStream fos new
FileOutputStream(args1) - while(is.read(inputString, 0,
inputString.length) ! -1) - fos.write(inputString)
-
- fos.close()
-
11RandomAccessFile(1)
- RandomAccessFile
- ??? ??? ???? ? ??
- ???, ?? ????? ??? ??? ??? ? ?? ??(RandomAccess)?
?? - RandomAccessFile raf new
RandomAccessFile("c\myfile.txt", "r/w")
Method summary Method summary
seek(long pos) ??? ?? ??? pos ??? ??? ??? ???
getFilePointer() ??? ?? ??(??? ??????? ??)? long ???? ????.
length() ??? ??? ??? ??? long ????? ????.
12RandomAccessFile(2)
- import java.io.
- import java.util.
- public class RandomWrite
- public static void main(String args)
throws Exception - RandomAccessFile raf new
RandomAccessFile("Mylog.log", "rw") - raf.seek(raf.length()) //??? ?? ?????
?? ??? ???. - raf.writeUTF(new Date().toString())
//?? ??? ??. - raf.close()
-
-
13Serialization(1)
- Serialization
- ??? ?? ??? ???? ?? ?? ??
- ???? ???? ?? ??? type ???? ?? ???? ???? ?? ?? ?
?? ??? ?? - serializable? ? ? ?? object? class?
Serializable??? ?? interface? ??? implements??
??. - Serializable interface
- serializable interface implements? method? ??.
- Serializable? implements??? ?? ?
class?serializable? ????? ????. - Object? serialization? ? data??(member??)? ????
constructor? method? code? ???? ???.
14Serialization(2)
- import java.io. //primitive
type? ?? object? ??? ?? ?? ?? - public class TestSerialization implements
Serializable - String name
- int age 7
- TestSerialization (String name, int age)
- this.name name
- this.age age
-
-
- public class SerializeMyObject
//TestSerialization class? object? ??? write?? - TestSerialization ts
- public static void main(String
args) throws Exception - SerializeMyObject smo
new SerializeMyObject() - smo.writeMyObject()
// object? file? write?. - smo.ts null
// object? memory?? ??. - smo.readMyObject()
// ???? object? ?? ???. - System.out.println(smo.t
s.name) -
15Serialization(3)
- public void writeMyObject() throws
Exception //object? ??? write - ts new TestSerialization("???",
32) - ObjectOutputStream oos
- new ObjectOutputStream(new
FileOutputStream("myObject.txt")) - oos.writeObject(ts) // object?
??? write?. - oos.close()
-
-
- public void readMyObject() throws
Exception // ??? ??? object? ?? -
//???? ??? ??? - ObjectInputStream ois
- new ObjectInputStream(new
FileInputStream("myObject.txt")) - ts (TestSerialization)ois.readO
bject() // object? ???? ??? memory? ??. - ois.close()
-
-