"Aku maneh."
"Halo, Ellie!"
"Dina iki aku arep ngandhani kabeh babagan BufferedReader lan BufferedWriter ."
"Sampeyan wis marang kula bab wong-wong mau. Padha pancene ora rumit."
"OK. Banjur marang kula carane BufferedReader dianggo."
" BufferedReader kaya konverter 110/220V."
"Sampeyan kudu pass menyang konstruktor BufferedReader obyek Reader sing data bakal diwaca saka. A obyek BufferedReader maca potongan gedhe saka data saka Reader lan nyimpen internal ing buffer. Mulane nggunakake BufferedReader kanggo maca saka Reader luwih cepet. tinimbang maca langsung saka Pembaca ."
"Bener. Lan kepiye BufferedWriter ?"
"Iku Piece saka jajan. Upamane kita nulis menyang FileWriter . Data ditulis menyang disk langsung. Yen kita nulis bit sethitik saka data kerep, banjur kita bakal mencet disk akeh, kang bakal alon mudhun program. Nanging yen kita nggunakake BufferedWriter minangka 'converter', banjur operasi nulis luwih cepet. Nalika sampeyan nulis menyang BufferedWriter , nyimpen data ing buffer internal. Nalika buffer kebak, nulis data kanggo Writer minangka siji potongan gedhe. Iki luwih cepet."
"Hmm. Titik ing. Nanging apa sampeyan lali?"
"Sawise sampeyan wis rampung nulis, sampeyan kudu nelpon flush () cara ing obyek BufferedWriter kanggo meksa kanggo ngirim data isih ing buffer kanggo Writer .
"Lan apa maneh?"
"Apa maneh? Oh! Anggere buffer durung ditulis kanggo Writer , data bisa dibusak lan / utawa diganti."
"Amigo! Aku kesengsem! Sampeyan ahli! Inggih, banjur aku bakal pitutur marang kowe bab sawetara kelas anyar: ByteArrayStream lan PrintStream ."
"Contone, ByteArrayInputStream lan ByteArrayOutputStream ."
"Kelas iki rada kaya StringReader lan StringWriter . Kajaba StringReader maca karakter ( char ) saka string ( String ), nanging InputStream maca bita saka array byte ( ByteArray ).
StringWriter nulis karakter ( char ) menyang string, nalika ByteArrayOutputStream nulis bita menyang array bait internal. Nalika sampeyan nulis menyang StringWriter , senar internal saya tambah dawa, lan nalika sampeyan nulis menyang ByteArrayOutputStream array byte internal uga berkembang kanthi dinamis.
"Elinga conto sing diwenehake ing pawulangan pungkasan:"
public static void main (String[] args) throws Exception
{
String test = "Hi!\n My name is Richard\n I'm a photographer\n";
StringReader reader = new StringReader(test);
StringWriter writer = new StringWriter();
executor(reader, writer);
String result = writer.toString();
System.out.println("Result: "+ result);
}
public static void executor(Reader reader, Writer writer) throws Exception
{
BufferedReader br = new BufferedReader(reader);
String line;
while ((line = br.readLine()) != null) {
StringBuilder sb = new StringBuilder(line);
String newLine = sb.reverse().toString();
writer.write(newLine);
}
}
"Iki carane bakal katon yen digunakake nggunakake bita tinimbang karakter:"
public static void main (String[] args) throws Exception
{
String test = "Hi!\n My name is Richard\n I'm a photographer\n";
InputStream inputStream = new ByteArrayInputStream(test.getBytes());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
executor(inputStream, outputStream);
String result = new String(outputStream.toByteArray());
System.out.println("Result: "+ result);
}
public static void executor(InputStream inputStream, OutputStream outputStream) throws Exception
{
BufferedInputStream bis = new BufferedInputStream(inputStream);
while (bis.available() > 0)
{
int data = bis.read();
outputStream.write(data);
}
}
"Kabeh padha karo conto ing ndhuwur. Kajaba, kita ngganti String karo ByteArray, Reader karo InputStream, lan Writer karo OutputStream."
"Mung rong langkah liyane Ngonversi String menyang ByteArray lan bali maneh. Nalika sampeyan bisa ndeleng, iki rampung cukup gampang: "
public static void main (String[] args) throws Exception
{
String test = "Hi!\n My name is Richard\n I'm a photographer\n";
byte[] array = test.getBytes();
String result = new String(array);
System.out.println("Result: "+ result);
}
"Kanggo entuk bita sing wis ditambahake menyang ByteArrayOutputStream, nelpon metode toByteArray ()."
"Ah. Persamaan karo StringReader / StringWriter cukup kuwat, utamane sawise sampeyan nudingake aku. Matur nuwun, Ellie, kanggo pelajaran sing menarik banget."
"Sampeyan cepet-cepet menyang ngendi? Aku isih duwe hadiah cilik kanggo sampeyan. Aku arep ngomong babagan kelas PrintStream."
"PrintStream? Sepisanan aku krungu kelas iku."
"Yep. Utamane yen sampeyan ora ngetung kasunyatan sing wis digunakake wiwit dina pisanan sinau basa Jawa. Apa sampeyan kelingan System.out ? Nah, System.out minangka variabel statis (kelas) saka Sistem . kelas, lan jinise yaiku... PrintStream ! Iki ngendi kabeh cara print, println, lan liya-liyane.
"Wah, menarik banget. Aku ora tau nganggep kaya ngono. Ngomong maneh."
"Apik. Oke, ngrungokake. Kelas PrintStream diciptakake kanggo output sing bisa diwaca. Iku meh kabeh cara print lan println. Deleng tabel iki:"
Metode | Metode |
---|---|
void print(boolean b) |
void println(boolean b) |
void print(char c) |
void println(char c) |
void print(int c) |
void println(int c) |
void print(long c) |
void println(long c) |
void print(float c) |
void println(float c) |
void print(double c) |
void println(double c) |
void print(char[] c) |
void println(char[] c) |
void print(String c) |
void println(String c) |
void print(Object obj) |
void println(Object obj) |
void println() |
|
PrintStream format (String format, Object ... args) |
|
PrintStream format (Locale l, String format, Object ... args) |
"Ana uga sawetara cara format supaya sampeyan bisa ngasilake data nggunakake string format. Contone:"
String name = "Kolan";
int age = 25;
System.out.format("My name is %s. My age is %d.", name, age);
My name is Kolan. My age is 25.
"Ya, aku elinga. Kita wis sinau metode format kelas String."
"Iku kabeh kanggo saiki."
"Matur nuwun, Ellie."
GO TO FULL VERSION