CodeGym /جاوا بلاگ /Random-SD /BufferedReader ۽ BufferedWriter
John Squirrels
سطح
San Francisco

BufferedReader ۽ BufferedWriter

گروپ ۾ شايع ٿيل
بفرڊ ريڊر ۽ بفرڊ رائٽر - 1جاوا جو BufferedReaderطبقو متن کي علامتن جي هڪ وهڪري مان پڙهي ٿو، علامتن کي بفر ڪري ٿو ته جيئن اکر، صفن ۽ تارن کي موثر طريقي سان پڙهڻ لاءِ. توھان پاس ڪري سگھوٿا بفر سائيز کي ٺاھيندڙ ڏانھن ھڪڙي ٻئي دليل جي طور تي. بفرڊ ريڊر ۽ بفرڊ رائٽر - 2تعمير ڪندڙ:
BufferedReader(Reader in) // Creates a buffered stream for reading symbols. It uses the default buffer size.
BufferedReader(Reader in, int sz) // Creates a buffered stream for reading symbols. It uses the specified buffer size.
طريقا:
close() // Close the stream
mark(int readAheadLimit) // Mark the position in the stream
markSupported() // Indicates whether stream marking is supported
int	read() // Read the buffer
int	read(char[] cbuf, int off, int len) // Read the buffer
String	readLine() // Next line
boolean	ready() // Is the stream ready to read?
reset() // Reset the stream
skip(long n) // Skip characters

هتي استعمال ڪرڻ جو هڪ مثال آهي BufferedReader۽ BufferedWriterڪلاس:

فائل پڙهڻ:
import java.io.*;

public class ReadFile {
	public static void main(String[] args) {
		try {
			File file = new File("file.txt");
			FileReader fileReader = new FileReader(file); // A stream that connects to the text file
			BufferedReader bufferedReader = new BufferedReader(fileReader); // Connect the FileReader to the BufferedReader

			String line;
			while((line = bufferedReader.readLine()) != null) {
				System.out.println(line); // Display the file's contents on the screen, one line at a time
			}

			bufferedReader.close(); // Close the stream
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
جاوا جو BufferedWriterڪلاس ٽيڪسٽ لکندو آهي هڪ آئوٽ پُٽ ڪيڪٽر اسٽريم تي، ڪردارن کي بفرنگ ڪرڻ لاءِ اکر، صفن ۽ تارن کي موثر انداز ۾ لکڻ لاءِ. توھان پاس ڪري سگھوٿا بفر سائيز کي ٺاھيندڙ ڏانھن ھڪڙي ٻئي دليل جي طور تي. تعمير ڪندڙ:
BufferedWriter(Writer out) // Create a buffered output character stream that uses the default buffer size.
BufferedWriter(Writer out, int sz) // Creates a buffered character output stream that uses a buffer with the specified size.
طريقا:
close() // Close the stream
flush() // Send the data from the buffer to the Writer
newLine() // Move to a new line
write(char[] cbuf, int off, int len) // Write to the buffer
write(int c) // Write to the buffer
write(String s, int off, int len) // Write to the buffer

هتي جاوا BufferedReader۽ BufferedWriterڪلاس استعمال ڪرڻ جو هڪ مثال آهي:

فائل ڏانهن لکڻ:
import java.io.*;

public class WriteFile {
    public static void main(String[] args) {
        String[] list = {"one", "two", "three", "fo"};
        try {
            File file = new File("file.txt");
            FileWriter fileReader = new FileWriter(file); // A stream that connects to the text file
            BufferedWriter bufferedWriter = new BufferedWriter(fileReader); // Connect the FileWriter to the BufferedWriter

            for (String s : list) {
                bufferedWriter.write(s + "\n");
            }

            bufferedWriter.close (); // Close the stream
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
FileWriterفوري طور تي ڊسڪ تي ڊيٽا لکي ٿو. هر دفعي جڏهن اسان ان تائين رسائي ڪريون ٿا، ان جي چوڌاري لپيل بفر اسان جي ايپليڪيشن کي تيز ڪري ٿو. بفر ڊيٽا کي اندروني طور تي لکندو، ۽ پوء بعد ۾ ڊسڪ تي فائلن جو وڏو حصو لکندو. اسان ڪنسول مان ڊيٽا پڙهون ٿا ۽ ان کي فائل ۾ لکون ٿا:
import java.io.*;

class ConsoleRead {
    public static void main(String[] args) {
        try {
            File file = new File("file.txt");
            InputStreamReader inputStreamReader = new InputStreamReader(System.in); // A stream for reading from the console
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader); // Connect InputStreamReader to a BufferedReader

            FileWriter fileReader = new FileWriter(file); // A stream that connects to the text file
            BufferedWriter bufferedWriter = new BufferedWriter(fileReader); // Connect the FileWriter to the BufferedWriter

            String line;
            while(!(line = bufferedReader.readLine()).equals("exit")) {
                bufferedWriter.write(line);
            }

            bufferedReader.close(); // Close the stream
            bufferedWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
تبصرا
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION