📜  Java中的 BufferedWriter newLine() 方法及示例

📅  最后修改于: 2022-05-13 01:55:37.217000             🧑  作者: Mango

Java中的 BufferedWriter newLine() 方法及示例

Java中BufferedWriter类的newLine()方法用于将下一行分隔为新行。它用作缓冲写入器流中的写入分隔符。

句法:

public void newLine()
            throws IOException

参数:此方法不接受任何参数。

返回值:此方法不返回任何值。

异常:如果发生 I/O 错误,此方法将引发IOException

下面的程序说明了 IO 包中 BufferedWriter 类中的 newLine() 方法:

方案一:

// Java program to illustrate
// BufferedWriter newLine() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create the string Writer
        StringWriter stringWriter
            = new StringWriter();
  
        // Convert stringWriter to
        // bufferedWriter
        BufferedWriter buffWriter
            = new BufferedWriter(
                stringWriter);
  
        // Write "A" to buffer writer
        buffWriter.write(65);
  
        // Revoke newLine() method
        buffWriter.newLine();
  
        // Write "B" to buffer writer
        buffWriter.write(66);
  
        buffWriter.flush();
  
        System.out.println(
            stringWriter.getBuffer());
    }
}
输出:
A
B

方案二:

// Java program to illustrate
// BufferedWriter newLine() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
        // Create the string Writer
        StringWriter stringWriter
            = new StringWriter();
  
        // Convert stringWriter to
        // bufferedWriter
        BufferedWriter buffWriter
            = new BufferedWriter(
                stringWriter);
  
        // Write "GEEKS" to buffered writer
        buffWriter.write(
            "GEEKSFORGEEKS", 0, 5);
  
        // Revoke newLine() method
        buffWriter.newLine();
  
        // Write "GEEKSFORGEEKS"
        // to buffered writer
        buffWriter.write(
            "GEEKSFORGEEKS", 0, 13);
  
        buffWriter.flush();
  
        System.out.println(
            stringWriter.getBuffer());
    }
}
输出:
GEEKS
GEEKSFORGEEKS

参考:
https://docs.oracle.com/javase/10/docs/api/java Java()