- Претпоставимо да се подаци читају са тастатуре у меморију помоћу ДатаИнпутСтреам-а и потребна је 1 секунда да се прочита 1 знак у меморију и овај знак се уписује у датотеку помоћу ФилеОутпутСтреам-а трошењем још 1 секунде.
- Дакле, за читање и писање датотеке ће бити потребно 200 секунди. Ово је губљење пуно времена. С друге стране, ако се користи Буфферед цлассед, они обезбеђују бафер који се прво попуњава знаковима из бафера који се могу одједном уписати у датотеку. Бафероване класе треба да се користе у вези са другим класама тока.
- First the DataInputStream reads data from the keyboard by spending 1 sec for each character. This character is written into the buffer. Thus to read 100 characters into a buffer it will take 100 second time. Now FileOutputStream will write the entire buffer in a single step. So reading and writing 100 characters took 101 sec only. In the same way reading classes are used for improving the speed of reading operation. Attaching FileOutputStream to BufferedOutputStream as:
BufferedOutputStream bout=new BufferedOutputStream(fout1024);
Here the buffer size is declared as 1024 bytes. If the buffer size is not specified then a default size of 512 bytes is used - ЦхарацтерСтреам вс БитеСтреам
- Класа датотеке у Јави
- Руковање датотекама у Јави користећи ФилеВритер и ФилеРеадер
DataInputStream dis =new DataInputStream(System.in);Here System.in represent the keyboard which is linked with DataInputStream object
FileOutputStream fout=new FileOutputStream(file.txt);
ch=(char)dis.read(); fout.write(ch);
//Java program to demonstrate creating a text file using FileOutputStream import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.FileOutputStream; import java.io.IOException; class Create_File { public static void main(String[] args) throws IOException { //attach keyboard to DataInputStream DataInputStream dis=new DataInputStream(System.in); // attach file to FileOutputStream FileOutputStream fout=new FileOutputStream('file.txt'); //attach FileOutputStream to BufferedOutputStream BufferedOutputStream bout=new BufferedOutputStream(fout1024); System.out.println('Enter text (@ at the end):'); char ch; //read characters from dis into ch. Then write them into bout. //repeat this as long as the read character is not @ while((ch=(char)dis.read())!='@') { bout.write(ch); } //close the file bout.close(); } }
If the Program is executed again the old data of file.txt will be lost and any recent data is only stored in the file. If we don’t want to lose the previous data of the file and just append the new data to the end of already existing data and this can be done by writing true along with file name. FileOutputStream fout=new FileOutputStream(file.txttrue);
Побољшање ефикасности коришћењем БуффередОутпутСтреам-а
Normally whenever we write data to a file using FileOutputStream as:fout.write(ch);Here the FileOutputStream is invoked to write the characters into the file. Let us estimate the time it takes to read 100 characters from the keyboard and write all of them into a file.
C:> javac Create_File.java C:> java Create_File Enter text (@ at the end): This is a program to create a file @ C:/> type file.txt This is a program to create a fileПовезани чланци: