Cov txheej txheem:

FileWriter puas yuav tsim cov ntaub ntawv?
FileWriter puas yuav tsim cov ntaub ntawv?

Video: FileWriter puas yuav tsim cov ntaub ntawv?

Video: FileWriter puas yuav tsim cov ntaub ntawv?
Video: MANUSIA BIASA YANG MENCOBA MEMBUNUH DEWA (Alur Cerita Film A Writer's Odyssey) 2024, Kaum ib hlis
Anonim

FileWriter ( Cov ntaub ntawv ): Tsim a FileWriter khoom siv tau teev tseg Cov ntaub ntawv khoom. Nws cuam IOException yog tias ntaub ntawv muaj tab sis yog ib phau ntawv es tsis yog ib txwm ntaub ntawv los yog ua tsis muaj nyob tab sis tsis tuaj yeem tsim tau, lossis tsis tuaj yeem qhib rau lwm qhov laj thawj.

Hauv qhov no, FileOutputStream puas tsim cov ntaub ntawv tshiab?

FileOutputStream ( Cov ntaub ntawv ): Tsim a cov ntaub ntawv tso zis kwj sau ntawv rau ntaub ntawv sawv cev los ntawm kev teev tseg ntaub ntawv khoom. Yog tias ntaub ntawv muaj tab sis yog ib phau ntawv es tsis yog ib txwm ntaub ntawv , ua tsis muaj nyob tab sis ua tsis tau tsim , lossis tsis tuaj yeem qhib rau lwm yam laj thawj ces FileNotFoundException raug pov tseg.

Ib yam li ntawd, FileWriter puas overwrite cov ntaub ntawv uas twb muaj lawm? Los ntawm default, lub FileWriter sau mus rau qhov pib ntawm lub ntaub ntawv (yuav overwrite uas twb muaj lawm cov ntaub ntawv). Teem lub yeem ntxiv boolean rau qhov tseeb hauv qhov FileWriter cov constructor pib sau mus rau qhov kawg ntawm lub ntaub ntawv.

Ib sab ntawm no, ua li cas koj tsim cov ntaub ntawv tshiab thiab sau nws hauv Java?

Java tsim cov ntaub ntawv

  1. Cov ntaub ntawv. createNewFile() java.
  2. FileOutputStream. sau (byte b) Yog tias koj xav tsim cov ntaub ntawv tshiab thiab tib lub sijhawm sau qee cov ntaub ntawv rau hauv nws, koj tuaj yeem siv FileOutputStream sau txoj kev.
  3. Java NIO Files.write() Peb tuaj yeem siv Java NIO Files chav kawm los tsim cov ntaub ntawv tshiab thiab sau qee cov ntaub ntawv rau hauv nws.

Qhov txawv ntawm FileWriter thiab BufferedWriter yog dab tsi?

FileWriter sau ncaj qha rau hauv Cov Ntaub Ntawv thiab yuav tsum tau siv tsuas yog thaum cov ntawv sau tsawg dua. BufferedWriter : BufferedWriter yuav luag zoo ib yam li FileWriter tab sis nws siv internal buffer los sau cov ntaub ntawv rau hauv File. Koj yuav tsum siv BufferedWriter thaum tus naj npawb ntawm kev sau ntawv ntau dua.

Pom zoo: