CodeGym /Kursus Jawa /1. modul /Aliran output

Aliran output

1. modul
tingkat , wulangan
kasedhiya

1. OutputStreamkelas

Kita bubar njelajah aliran input. Iku wektu kanggo pirembagan bab output stream.

Kelas kasebut OutputStreamminangka kelas induk kanggo kabeh kelas sing ndhukung output byte. Iki minangka kelas abstrak sing ora nindakake apa-apa, nanging nduweni kelas turunan kanggo saben kesempatan.

Muni rumit banget. Kanggo sijine iku luwih gampang, kelas iki makaryakke ing bita, lan ora, contone, karakter utawa jinis data liyane. Lan kasunyatan sing abstrak tegese kita biasane ora nggunakake, nanging rodo salah siji saka kelas turunane. Contone, FileOutputStreamlan liya-liyane.

Nanging bali menyang OutputStreamkelas. Kelas iki nduweni metode sing kudu ditindakake kabeh kelas turunane. Ing ngisor iki sing utama:

Metode Katrangan
void write(int b)
Nulis siji bait (ora an int) menyang stream.
void write(byte[] buffer)
Nulis array saka bita menyang stream
void write(byte[] buffer, off, len)
Nulis bagean saka Uploaded byte menyang stream
void flush()
Nulis kabeh data sing disimpen ing buffer menyang stream
void close()
Nutup stream

Nalika nggawe obyek saka kelas sing oleh warisan InputStream, sampeyan biasane nemtokake obyek sumber sing InputStreamdata diwaca saka. Nalika sampeyan nggawe obyek saka kelas sing oleh warisan OutputStream, sampeyan uga biasane nemtokake obyek target utawa stream sing data bakal ditulis kanggo.

Ayo goleki kabeh metode kelas kanthi ringkes OutputStream:

write(int b)cara

Cara iki nulis siji byte (ora an int) menyang stream output. Nilai sing dilewati dibuwang menyang bait, lan telung bait pertama int dibuwang.

write(byte[] buffer)cara

Nulis Uploaded diwenehi saka bita menyang stream output. Mekaten.

write(byte[] buffer, int offset, int length)cara

Nulis bagean saka Uploaded liwat bita menyang stream output. Variabel offset nuduhake indeks saka unsur pisanan saka array, lan lengthdawa subset bakal ditulis.

flush()cara

Cara iki flush()digunakake kanggo meksa data apa wae sing duweni potensi buffer ing stream saiki supaya ditulis menyang stream target. Iki cocog nalika nggunakake buffering lan / utawa sawetara obyek stream disusun ing chain.

close()cara

Nulis data sing ora ditulis menyang obyek target. Cara kasebut close()ora perlu diarani yen sampeyan nggunakake try-with-resourcesblok.

Tuladha nyalin file

Kode Cathetan
String src = "c:\\projects\\log.txt";
String dest = "c:\\projects\\copy.txt";

try(FileInputStream input = new FileInputStream(src);
FileOutputStream output = new FileOutputStream(dest))
{
   byte[] buffer = new byte[65536]; // 64Kb
   while (input.available() > 0)
   {
      int real = input.read(buffer);
      output.write(buffer, 0, real);
   }
}



InputStreamkanggo maca saka file
OutputStreamkanggo nulis menyang file

Buffer ing ngendi kita bakal maca data
Anggere ana data ing stream

Waca data menyang buffer
Tulis data saka buffer menyang stream kapindho

2. Writerkelas

Kelas Writeriku persis padha karo OutputStreamkelas, nanging mung siji prabédan sepisan maneh: dianggo karo karakter ( char) tinimbang bita.

Iki kelas abstrak: sampeyan ora bisa nggawe obyek saka Writerkelas. Tujuan utamane yaiku dadi kelas induk sing umum kanggo atusan kelas turunan lan menehi cara umum kanggo nggarap aliran karakter.

Metode kelas Writer(lan kabeh kelas turunane):

Metode Katrangan
void write(int b)
Nulis siji karakter (ora an int) menyang stream.
void write(char[] buffer)
Nulis array karakter menyang stream
void write(char[] buffer, off, len)
Nulis bagean saka macem-macem karakter menyang stream
void write(String str)
Nulis string menyang stream
void write(String str, off, len)
Nulis bagéan saka senar menyang stream
void flush()
Nulis kabeh data sing disimpen ing buffer menyang stream
void close()
Nutup stream

Cara kasebut meh padha karo metode kelas OutputStream, nanging bisa digunakake kanthi karakter tinimbang bita.

Deskripsi metode:

write(int b)cara

Cara iki nulis karakter siji ( char- ora an int) menyang stream output. Nilai liwati dibuwang menyang a char, lan rong bita pisanan dibuwak.

write(char[] buffer)cara

Nulis array diwenehi karakter menyang stream output.

write(char[] buffer, int offset, int length)cara

Nulis bagean saka Uploaded liwat karakter kanggo stream output. Variabel kasebut offsetnuduhake indeks saka unsur pisanan saka array, lan lengthdawa subset sing bakal ditulis.

write(String str)cara

Nulis string diwenehi menyang stream output.

write(String str, int offset, int length)cara

Nulis bagean saka senar diwenehi kanggo stream output: senar diowahi kanggo Uploaded karakter. Variabel kasebut offsetnuduhake indeks saka unsur pisanan saka array, lan lengthdawa subset sing bakal ditulis.

flush()cara

Cara iki flush()digunakake kanggo meksa data apa wae sing duweni potensi buffer ing stream saiki supaya ditulis menyang stream target. Iki cocog nalika nggunakake buffering lan / utawa sawetara obyek stream disusun ing chain.

close()cara

Nulis data sing ora ditulis menyang obyek target. Cara kasebut close()ora perlu diarani yen sampeyan nggunakake try-with-resourcesblok.

Conto program sing nyalin file teks:

Kode Cathetan
String src = "c:\\projects\\log.txt";
String dest = "c:\\projects\\copy.txt";

try(FileReader reader = new FileReader(src);
FileWriter writer = new FileWriter(dest))
{
   char[] buffer = new char[65536]; // 128Kb
   while (reader.ready())
   {
      int real = reader.read(buffer);
      writer.write(buffer, 0, real);
   }
}



Readerkanggo maca saka file
Writerkanggo nulis menyang file

Buffer ing ngendi kita bakal maca data
Anggere ana data ing stream

Waca data menyang buffer
Tulis data saka buffer menyang stream kapindho

StringWriterkelas

Ana kelas menarik liyane sing marisi Writerkelas kasebut: diarani StringWriter. Isine string mutable - StringBufferobyek. Lan saben-saben sampeyan "nulis" soko kanggo StringWriterobyek, teks mung ditambahake menyang buffer internal.

Tuladha:

Kode Cathetan
StringWriter writer = new StringWriter();
writer.write("Hello");
writer.write(String.valueOf(123));

String result = writer.toString();
Aliran karakter target ( StringWriter) digawe
Senar ditulis menyang buffer ing jero StringWriter
Senar ditulis menyang buffer ing StringWriter

Ngonversi isi obyek dadi senar.

Ing kasus iki, StringWriterkelas ateges pambungkus liwat StringBufferkelas, nanging StringWriterkelas iku turunan saka Writerkelas stream, lan bisa digunakake ing rentengan obyek stream. Iki minangka properti sing migunani banget ing praktik.



Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION