> Java > java지도 시간 > 본문

자바 버퍼링 라이터

王林
풀어 주다: 2024-08-30 16:08:36
원래의
581명이 탐색했습니다.

The java BufferedWriter is a class that is used to provide buffering for writing text to the character output stream. The BufferedWriter makes the fast performance and efficient writing of the character, string, and single array. The BufferedWriter class is a built-in class in java that is defined in the java.io.BufferedWriter package.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The java.io.Writer class is a superclass of the BufferedWriter. The buffer size may or may not be specified; when the size of the buffer is not specified, then its size will be the default size that is large enough for most of the purpose. In comparison to Filewriter, the BufferedWriter writes the large chunks of data, so it is used to make FileWriter (lower level class) more efficient and easy to use.

Syntax

Following is the declaration for the java.io.BufferedWriter class.

public class BufferedWriter extends Writer
{
// Constructors and methods of the BufferedWriter class
}
로그인 후 복사

The above is the syntax of the BufferedWriter, where it is extended to the Writer class.

  • BufferedWriter class member Functions: The BufferedWriter class contains constructors and some functions as a BufferedWriter class member function.

Constructors

  • BufferedWriter( Writer out): This constructor creates an instance of BufferedWriter, which uses a default size of an output buffer.
  • BufferedWriter( Writer out, int size): This constructor creates an instance of BufferedWriter, which uses an output buffer of the specified size.

Functions of BufferedWriter Class

Below are the functions mentioned :

1. write(int character): This function is used to write a single character.

Syntax:

public void writes (int character).
로그인 후 복사

2. write(char[] cbuf, int off, intlen): This function is used to write an array cbufof characters len bytes at offset off to the file.

Syntax:

public void write(char[] cbuf, int off, intlen)
로그인 후 복사

3. write(String s, int off, intlen): This function is uses to write a string of len length at offset off to the file.

Syntax:

public void write(String s, int off, intlen)
로그인 후 복사

4. flush(): This function is uses to flushes the input stream.

Syntax:

public void flush()
로그인 후 복사

5. newLine(): This function is used to write a line separator defined by the system property line.separator.

Syntax:

public void newLine()
로그인 후 복사

6. close(): This function is used to closes the file and release the input stream resource.

Syntax:

public void close()
로그인 후 복사

Examples to Implement Java BufferedWriter

Working and examples are mentioned below:

Example #1

Next, we write the java code to understand the BufferedWriter class more clearly. The following example is where we create a BufferedWriter object by using the BufferedWriter class constructor and passing the file name to write a character, as below.

Code:

//package p1;
import java.io.BufferedWriter;
import java.io.FileWriter;
public class Demo
{
public static void main( String[] arg) {
// create object of file input stream by opening connection data.txt file
try {
FileWriter fobj=new FileWriter("D:\\data.txt");
// create object of the BufferedWriter
BufferedWriter bobj = new BufferedWriter(fobj);
System.out.println("Buffered start writing : ");
// Use write() function
bobj.write('H');
bobj.write('E');
// writing l
bobj.write(108);
bobj.write(108);
// writing o!
bobj.write('o');
bobj.write('!');
// Closing BufferWriter to end operation
bobj.close();
System.out.println("Buffered writing done. you can open the file.");
}catch(Exception e)
{
System.out.println(e);
}
}
}
로그인 후 복사

An output of the above code is

자바 버퍼링 라이터

Note: While we are performing any write operations to the file, we should not keep open that file; if we do, then the file cannot be updated or written.

Now, if we open the thedata.txt file, we can see that the content is written, as we can see below.

자바 버퍼링 라이터

Example #2

Next, we write the java code to understand the BufferedWriter class more clearly, where we create aBufferedWriter object to write the string to the file with the associated buffer, as below.

Code:

//package p1;
import java.io.BufferedWriter;
import java.io.FileWriter;
public class Demo
{
public static void main( String[] arg) {
// create object of file input stream by opening connection data.txt file
try {
FileWriter fobj=new FileWriter("D:\\data.txt");
// create object of the BufferedWriter
BufferedWriter bobj = new BufferedWriter(fobj);
System.out.println("Buffered start writing : ");
int offs = 4;
String str = "This is an example for Buffered writer";
bobj.write(str,offs,str.length()-offs);
//CloseBufferWriter to end operation
bobj.close();
System.out.println("Buffered writing done. you can open the file.");
}catch(Exception e)
{
System.out.println(e);
}
}
}
로그인 후 복사

An output of the above code is:

자바 버퍼링 라이터

When we open the data.txt file, we can see that the content is written, as we can see below:

자바 버퍼링 라이터

Example #3

Next, we write the java code to understand the BufferedWriter class more clearly where we create a BufferedWriter object to write the string to the file and to move the next line while writing; we will use the newLine() function, as below.

Code:

//package p1;
import java.io.BufferedWriter;
import java.io.FileWriter;
public class Demo
{
public static void main( String[] arg) {
// create object of file input stream by opening connection data.txt file
try {
FileWriter fobj=new FileWriter("D:\\data.txt");
// create object of the BufferedWriter
BufferedWriter bobj = new BufferedWriter(fobj);
System.out.println("Buffered start writing : ");
bobj.write("This is an ");
// Next line
bobj.newLine();
bobj.write("Example for");
// Next line
bobj.newLine();
// Printing "GEEKS"
bobj.write("BufferedWriter");
// close BufferWriter to end operation
bobj.close();
System.out.println("Buffered writing done. you can open the file.");
}catch(Exception e)
{
System.out.println(e);
}
}
}
로그인 후 복사

An output of the above code is:

자바 버퍼링 라이터

If we open the data.txt file, we can see that the content is written, as we can see below:

자바 버퍼링 라이터

결론

BufferedWriter는 문자 출력 스트림에 텍스트를 쓰기 위한 버퍼링을 제공하는 데 사용되는 Java의 내장 클래스입니다. BufferedWriter 클래스는 java.io.BufferedWriter 패키지에 정의되어 있습니다. 이 클래스는 일반적으로 빠르고 효율적인 글쓰기가 필요한 애플리케이션에 사용됩니다.

위 내용은 자바 버퍼링 라이터의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿