— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet413/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   409   410   411   412   413   414   415   416   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

serialization 
and
file I/O
you are here
4
447
Writing a String to a Text File
Saving objects, through serialization, is the easiest way to save and 
restore data between runnings of a Java program. But sometimes you 
need to save data to a plain old text fi le. Imagine your Java program 
has to write data to a simple text fi le that some other (perhaps non-
Java) program needs to read. You might, for example, have a servlet 
(Java code running within your web server) that takes form data the 
user typed into a browser, and writes it to a text fi le that somebody else 
loads into a spreadsheet for analysis. 
Writing text data (a String, actually) is similar to writing an object, 
except you write a String instead of an object, and you use a 
FileWriter instead of a FileOutputStream (and you don’t chain it to an 
ObjectOutputStream).
50,Elf,bow, sword,dust
200,Troll,bare hands,big ax
120,Magician,spells,invisibility
What the game character data 
might look like if you wrote it 
out as a human-readable text file.
To write a serialized object:
objectOutputStream.writeObject(someObject);
To write a String:
fi leWriter.write(“My fi rst String to save”);
import java.io.*;
class WriteAFile {
public static void main (String[] args) {
try {
FileWriter writer = new FileWriter(“Foo.txt”);
writer.write(“hello foo!”);
writer.close();
} catch(IOException ex) {
ex.printStackTrace();
}

}
We need the java.io package for FileWriter 
ALL the I/O stuff 
must be in a try/catch. 
Everything can throw an 
IOException!!
If the file “Foo.txt” does not 
exist, FileWriter will create it.
The write() method takes 
a String
Close it when you’re done!



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   409   410   411   412   413   414   415   416   ...   555




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish