Kev siv FileWriter hauv Java yog dab tsi?
Kev siv FileWriter hauv Java yog dab tsi?

Video: Kev siv FileWriter hauv Java yog dab tsi?

Video: Kev siv FileWriter hauv Java yog dab tsi?
Video: looj phaj kev siv 2024, Hlis ntuj nqeg
Anonim

Java FileWriter chav kawm yog siv sau tus cwj pwm-oriented cov ntaub ntawv rau ib cov ntaub ntawv. Nws yog character-oriented class uas yog siv rau cov ntaub ntawv tuav hauv java ua . Tsis zoo li FileOutputStream chav kawm, koj tsis tas yuav hloov txoj hlua rau hauv byte array vim nws muab txoj hauv kev los sau txoj hlua ncaj qha.

Hais txog qhov no, kev siv FileOutputStream hauv Java yog dab tsi?

Java FileOutputStream . FileOutputStream yog qhov tso zis tso zis rau sau cov ntaub ntawv mus rau Cov Ntaub Ntawv lossis FileDescriptor. FileOutputStream yog siv rau sau kwj ntawm raw bytes xws li cov ntaub ntawv duab. Nws yog qhov zoo rau siv nrog bytes ntawm cov ntaub ntawv uas tsis tuaj yeem sawv cev ua ntawv xws li PDF, Excel cov ntaub ntawv, cov ntaub ntawv duab thiab lwm yam.

Tom qab ntawd, lo lus nug yog, yuav ua li cas kuv thiaj hla cov ntaub ntawv Java? Yog koj sau a file hauv Java uas twb muaj nyob rau hauv qhov chaw, nws yuav overwritten txiav. Tsuas yog koj sau ntawv mus rau qhov ntawd ntaub ntawv nrog ib qho append chij teem rau True. FileWriter fw = new FileWriter(filename, cuav); Nws yuav overwrite cov ntaub ntawv i.e. clear cov ntaub ntawv thiab sau rau nws dua.

Tsis tas li ntawd, FileWriter sau rau qhov twg?

FileWriter yog siv rau sau rau cov ntaub ntawv cim. Nws sau () txoj kev tso cai rau koj sau cim (s) los yog hlua rau ib cov ntaub ntawv. FileWriters feem ntau yog qhwv los ntawm qib siab dua Tus kws sau ntawv cov khoom, xws li BufferedWriter lossis PrintWriter, uas muab kev ua tau zoo dua thiab qib siab dua, hloov pauv txoj hauv kev rau sau cov ntaub ntawv.

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: