1. การจำแนกประเภทของ IO Streams
สตรีมตัวละคร
ผู้อ่าน
InputStreamReader (โหนดสตรีม)
BufferedReader (การประมวลผลสตรีม)
นักเขียน
OutputStreamWriter (โหนดสตรีม)
BufferedWriter (สตรีมการประมวลผล)
นักพิมพ์
สตรีมไบต์
อินพุตสตรีม
FileInputStream (โหนดสตรีม)
BufferedInputStream (สตรีมการประมวลผล)
ObjectInputStream (สตรีมการประมวลผล)
printstream
outputstream
FileOutputStream (โหนดสตรีม)
BufferedOutputStream (สตรีมการประมวลผล)
ObjectOutputStream (สตรีมการประมวลผล)
สตรีมการประมวลผลเบรกพอยต์
แบบสุ่ม
2. การใช้งาน IO Streams
1. การใช้สตรีมการแปลง
FileInputStream ใน = ใหม่ FileInputStream (newFile ("")); readerReader = ใหม่ inputStreamReader (in); // byte ถึง character fileOutputStreamout = new fileOutputStream (newFile ("")); Writer Writer = new OutputStreamWriter (out); // ตัวอักษรถึง BYTE2. การทำให้เป็นอนุกรมวัตถุวัตถุจำเป็นต้องใช้อินเทอร์เฟซแบบ serializable
fileOutputStreamFileOutputStream = ใหม่ fileOutputStream ("c: //users//lx//desktop//record.txt"); ObjectOutputStreamObjectOutputStream = new ObjectOutputStream (fileOutputStream); ObjectOutputStream.writeObject (Object); // การเขียนวัตถุไปยังไฟล์ที่ระบุ ObjectOutputStream.close (); FileInputStreamFileInputStream = new FileInputStream ("C: //users//lx//desktop//record.txt"); ObjectInputStreamObjectInputStream = ใหม่ ObjectInputStream (FileInputStream); Object = ObjectInputStream.readObject (); // อ่านวัตถุวัตถุวัตถุ fileInputStream.lose ();3. แอปพลิเคชันของจุดพัก
การคัดลอกคลาสสาธารณะขยายเธรด {// คุณสามารถใช้ multithread เพื่อรับรู้การคัดลอก longstart; longend; filesorce; fileTargetDir; publicCopy () {} publicCopy (longstart, ปลายยาว, ส่วนประกอบไฟล์, ไฟล์เป้าหมาย this.start = เริ่ม; this.end = สิ้นสุด; this.sorce = sort; this.targetDir = TargetDir;}@Override PublicVoid Run () {ลอง {randomAccessFilesOuceraf = new RandomActEncessFile (Sorce, "R"); RandomaccessFileTargetraf = ใหม่ RandomaccessFile (newfile (targetdir, sorce.getName ()), "rw"); souceraf.seek (เริ่มต้น); targetraf.seek (เริ่มต้น); intlen = 0; byte [] bs = byte ใหม่ [1024]; longseek; souceraf.read (bs))! =-1) {targetraf.write (bs, 0, len); seet = souceraf.getFilepointer (); // รับตำแหน่งเบรกพอยต์ถ้า (ค้นหา == end) {break;}} targetraf.close (); {E.PrintStackTrace ();}}}4. การใช้งานของไบต์ลำธาร
Public Class Test_InputStream {// ใช้ BYTE Streams เพื่อรับเนื้อหาไฟล์ข้อความ แต่ปัญหามีแนวโน้มที่จะเกิดขึ้น/* // ความยาว int อาจอยู่นอกขอบเขตโมฆะสาธารณะคงที่หลัก (สตริง [] args) โยน ioexception {inputstream = new FileInputStream (ไฟล์ใหม่ ไบต์ [] b = ไบต์ใหม่ [inputstream.available ()]; inputStream.read (b); string str = สตริงใหม่ (b); System.out.println (str); }*/// รหัสที่อ่านไม่ออกอาจปรากฏขึ้นเป็นโมฆะคงที่สาธารณะหลัก (สตริง [] args) พ่น ioexception {ไฟล์ไฟล์ = ไฟล์ใหม่ ("c: //users//lx//desktop//test//33.txt ไบต์ [] b = ไบต์ใหม่ [2]; StringBuffer sbuffer = new StringBuffer (); ในขณะที่ ((len = inputstream.read (b))! =-1) {sbuffer.append (สตริงใหม่ (b, 0, len));} system.out.println (sbuffer.toString ());}} // คัดลอกไฟล์ที่ใช้ null; fileOutputStreamFileOutputStream = null; fileInputStream = ใหม่ fileInputStream (SourceFile); fileTargetFile = ไฟล์ใหม่ (targetDir, sourceFile.getName ()); fileOutputStream = ใหม่ fileOutputStream ! = -1) {fileOutputStream.write (b, 0, len);}}5. การใช้สตรีมตัวละครแคช
PublicStatic Void Main (String [] args) พ่น ioexception {// การใช้สตรีมอักขระแคชเพื่อเขียนไฟล์ inputstreamin = system.in; readerReader = ใหม่ inputStreamReader (IN); bufferedReaderbr = new BufferedReader (reader); bufferedWriterBw = ใหม่ ไฟล์ ("src/1.txt"))); strings = ""; ในขณะที่ ((s = br.readline ())! = null) {bw.write (s); bw.newline (); bw.flush (); ฟลัช !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!สรุป
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้เกี่ยวกับการวิเคราะห์รหัสความรู้ที่เกี่ยวข้องกับสตรีม Java IO ฉันหวังว่ามันจะเป็นประโยชน์กับทุกคน เพื่อนที่สนใจสามารถอ้างถึงหัวข้ออื่น ๆ ที่เกี่ยวข้องในเว็บไซต์นี้ต่อไป หากมีข้อบกพร่องใด ๆ โปรดฝากข้อความไว้เพื่อชี้ให้เห็น ขอบคุณเพื่อนที่ให้การสนับสนุนเว็บไซต์นี้!