/ Java java.io, , / . java.io.InputStream i java.io.OutputStream, , . , - . Object. java.io :
- Object
- InputStream
- FileInputStream
- StringBufferedInputStream
- ByteArrayInputStream
- SequenceInputStream
- PipedInputStream
- FIlterInputStream
- DataInputStream
- BufferedInputStream
- LineNumberInputStream
- PushbackInputStream
- OutputStream
- FileOutputStream
- ByteArrayOutputStream
- PipedOutputStream
- FIlterOutputStream
- DataOutputStream
- BufferedOutputStream
- PrintStream
/
- FileInputStream
- FileOutputStream
:
InputStream myFile1 = new FileInputStream("\\dir\\file1.txt"); FileDescriptor myF = myFile1.getFD(); InputStream myFile2 = new FileInputStream(myF); |
' , , . JVM ' , . java.io.File. ³ , , . :
import java.io.*; public class ReadWriteFile { public static void main(String args[]) { int rbyte = 0; FileInputStream readFile; FileOutputStream writeFile; try { readFile = new FileInputStream("File1.txt"); writeFile = new FileOutputStream("File2.txt"); try { while ((rbyte = readFile.read())!= -1) { System.out.print(Integer.toHexString(rbyte) + " "); writeFile.write(rbyte); } } catch (IOException ioe) { System.out.println("Unable to write: " + ioe.toString()); System.exit(0); } } catch (FileNotFoundException fnfe1) { System.out.println("Unable to Find Input File: " + fnfe1.toString()); System.exit(0); } } } |
/
, StringReader. :
import java.io.*; public class TestStringIo { public static void main(String args[]) { String s = new String("String from Stream"); int bt = 0; StringReader mStr = new StringReader(s); try { while ((bt = mStr.read())!= -1) { System.out.print((char)bt); } System.out.println(); } catch (IOException e) { System.out.println("Unable to read: " + e.toString()); } } } |
StringWriter.
:
, filename1 , filename2, ﳿ . , : filename1, filename.
|
|
:
, ( new) , , , .
public class MultiCopy { TextBuffer buffer; MultiCopy() { System.out.println("MultiCopy:MultiCopy()"); buffer = new TextBuffer(); WriteFile wf = new WriteFile("File3.txt", buffer); PrintFile pf = new PrintFile(buffer); ReadFile rf = new ReadFile("File1.txt", buffer, wf); rf.start(); wf.start(); pf.start(); } public static void main(String args[]) { System.out.println("main"); MultiCopy mc = new MultiCopy(); } } |
() , .
class TextBuffer { int bufferByte; TextBuffer() { System.out.println("TextBuffer:TextBuffer()"); bufferByte = 0; } public synchronized void setBufferByte(int b) { System.out.println("TextBuffer:setBufferByte() \r"); bufferByte = b; } public synchronized int getBufferByte() { System.out.println("TextBuffer:getBufferByte() \r"); return(bufferByte); } } |
, , . , , (synchronized).
wait() notify(). notify() , , , .
class ReadFile implements Runnable { FileInputStream readFile; Thread ReadThread; TextBuffer tb = null; WriteFile writeFile; ReadFile(String fileName, TextBuffer b, WriteFile wf) { tb = b; writeFile = wf; try { readFile = new FileInputStream(fileName); } catch (FileNotFoundException fnfe1) { System.out.println("Unable to Find Input File: " + fnfe1.toString()); System.exit(0); } } public void start() { System.out.println("ReadFile:start()"); ReadThread = new Thread(this); ReadThread.start(); } public void stop() { System.out.println("ReadFile:stop()"); if (ReadThread!= null) { ReadThread = null; } } public void run() { int rbyte; System.out.println("ReadFile:run()"); try { while ((rbyte = readFile.read())!= -1) { tb.setBufferByte(rbyte); synchronized(writeFile) { writeFile.notify(); } try { Thread.sleep(50); } catch (InterruptedException ee) { System.out.println("Interrupted Exception: " + ee.toString()); } } tb.setBufferByte(-1); synchronized(writeFile) { writeFile.notify(); } } catch (IOException ioe) { System.out.println("Unable to read: " + ioe.toString()); } } } |
wait().
class WriteFile implements Runnable { FileOutputStream writeFile; Thread WriteThread; TextBuffer tb = null; WriteFile(String fileName, TextBuffer b) { System.out.println("WriteFile:WriteFile()"); tb = b; try { writeFile = new FileOutputStream(fileName); } catch (FileNotFoundException fnfe1) { System.out.println("Unable to Find Output File: " + fnfe1.toString()); System.exit(0); } } public void start() { System.out.println("WriteFile:start()"); WriteThread = new Thread(this); WriteThread.start(); } public void stop() { System.out.println("WriteFile:stop()"); if (WriteThread!= null) { WriteThread = null; } } public synchronized void run() { int rbyte; System.out.println("WriteFile:run()"); try { do { try { wait(); } catch (InterruptedException ee) { System.out.println("Interrupted Exception: " + ee.toString()); } rbyte = tb.getBufferByte(); writeFile.write(rbyte); } while (rbyte!= -1); } catch (IOException ioe) { System.out.println("Unable to write: " + ioe.toString()); } } } |
|
|
8.