• Tags ,
  •         
  • www.breakyizhan.com
  •    

    Java中的Java.io.BufferedWriter类方法

    Bufferreader类将文本写入字符输出流,缓冲字符。因此,可以高效地写入单个数组,字符和字符串。需要指定缓冲区大小,如果不是,则需要默认值。
    Writer立即将输出设置为底层字符或字节流。

    类声明

    public class BufferedWriter
       extends Writer

    构造函数

    • BufferedWriter(Writer out):创建使用默认大小输出缓冲区的缓冲字符输出流。
    • BufferedWriter(Writer out,int size):创建一个新的缓冲字符输出流,该流使用给定大小的输出缓冲区。

    方法:

    • write():java.io.BufferedWriter.write(int arg)写入由整数参数指定的单个字符。
      句法 :

      public void write(int arg)
      Parameters : 
      arg : integer that specifies the character to write          
      Return :
      Doesn't return any value.

      实现:

      //Java program illustrating use of write(int arg) method
      
      import java.io.*;
      public class NewClass
      {
          public static void main(String[] args)
          {
              //initializing FileWriter
              FileWriter geek_file;
              try
              {
                  geek_file = new FileWriter("ABC.txt");
      
                  // Initialing BufferedWriter
                  BufferedWriter geekwrite = new BufferedWriter(geek_file);
                  System.out.println("Buffered Writer start writing :)");
      
                  // Use of write() method to write the value in 'ABC' file
                  // Printing E
                  geekwrite.write(69);
      
                  // Printing 1
                  geekwrite.write(49);
      
                  // Closing BufferWriter to end operation
                  geekwrite.close();
                  System.out.println("Written successfully");
              }
              catch (IOException excpt)
              {
                  excpt.printStackTrace();
              }
      
          }
      }
      

      注意:在给定的输出中,您看不到它是文件上的操作。在设备中的任何编译器上运行此代码。它创建一个新文件'ABC'并在其中写入“E 1”。

      Output : 
      Buffered Writer start writing :)
      Written successfully
    • write():java.io.BufferedWriter.write(String arg,int offset,int length)根据Java代码中提到的参数在文件中写入String。
      句法 :

      public void write(String arg, int offset, int length)
      Parameters : 
      arg : String to be written
      offset : From where to start reading the string
      length : No. of characters of the string to write          
      Return :
      Doesn't return any value.

      实现:

      //Java program illustrating use of write(String arg, int offset, int length) method
      
      import java.io.*;
      public class NewClass
      {
          public static void main(String[] args)
          {
              //Initializing a FileWriter
              FileWriter geek_file;
              try
              {
                  geek_file = new FileWriter("ABC.txt");
      
                  // Initializing a BufferedWriter
                  BufferedWriter geekwrite = new BufferedWriter(geek_file);
                  System.out.println("Buffered Writer start writing :)");
                  String arg = "Hello Geeks";
                  int offset = 6;
                  geekwrite.write(arg,offset,arg.length()-offset);
      
                  // Closing Buffer
                  geekwrite.close();
                  System.out.println("Written successfully");
              }
              catch (IOException except)
              {
                  except.printStackTrace();
              }
      
      
          }
      }
      

      注意:在给定的输出中,您看不到它是文件上的操作。在设备中的任何编译器上运行此代码。它创建一个新文件'ABC'并在其中写入“极客”。在这里,

      arg = Hello Geeks
      offset = 6
      length = arg.length So, when we minus offset : 6, it will write 'Geeks' only in the file.

      输出:

      Buffered Writer start writing :)
      Written successfully
    • newLine():java.io.BufferedWriter.newLine()打破/分隔线条。
      句法 :

      public void newLine()       
      Return :
      Doesn't return any value.

      实现:

      //Java program explaining use of newLine() method
      
      import java.io.*;
      public class NewClass
      {
          public static void main(String[] args)
          {
              //initializing FileWriter
              FileWriter geek_file;
              try
              {
                  geek_file = new FileWriter("ABC.txt");
      
                  // Initialing BufferedWriter
                  BufferedWriter geekwrite = new BufferedWriter(geek_file);
                  System.out.println("Buffered Writer start writing :)");
      
                  // Use of write() method to write the value in 'ABC' file
                  // Printing "GEEKS"
                  geekwrite.write("GEEKS");
      
                  // For next line
                  geekwrite.newLine();
      
                  // Printing "FOR"
                  geekwrite.write("FOR");
      
                   // For next line
                  geekwrite.newLine();
      
                  // Printing "GEEKS"
                  geekwrite.write("FOR");
      
                  // Closing BufferWriter to end operation
                  geekwrite.close();
                  System.out.println("Written successfully");
              }
              catch (IOException excpt)
              {
                  excpt.printStackTrace();
              }
      
          }
      }
      

      注意:在给定的输出中,您看不到它是文件上的操作。在设备中的任何编译器上运行此代码。它创建一个新文件'ABC'并写入

      输出:

      Buffered Writer start writing :)
      Written successfully
    • flush():java.io.BufferedWriter.flush()从写入缓冲区刷新字符。
      句法 :

      public void flush()    
      Return :
      Doesn't return any value.
    • close():java.io.BufferedWriter.close()从写入缓冲区刷新字符,然后关闭它。
      句法 :

      public void close()    
      Return :
      Doesn't return any value.

      flush(),close()方法的实现:

      //Java program illustrating use of flush(), close() method
      
      import java.io.*; //BufferedWriter, FileWriter, IOException
      public class NewClass
      {
          public static void main(String[] args)
          {
              FileWriter geek_file; //initializing FileWriter
              try
              {
                  geek_file = new FileWriter("ABC.txt");
                  // Initialing BufferedWriter
                  BufferedWriter geekwrite = new BufferedWriter(geek_file);
                  System.out.println("Buffered Writer start writing :)");
                  // Use of write() method to write the value in 'ABC' file
      
                  geekwrite.write(69); // Printing E
                  geekwrite.newLine(); // For next line
                  geekwrite.write(49); // Printing 1
      
                  // flush() method : flushing the stream
                  geekwrite.flush();
                  // close() method : closing BufferWriter to end operation
                  geekwrite.close();
                  System.out.println("Written successfully");
              }
              catch (IOException excpt)
              {
                  excpt.printStackTrace();
              }
      
          }
      }
      

      注意:您无法看到它是文件操作。在设备中的任何编译器上运行此代码。它创建一个新文件'ABC'并写入
      | E |
      | 1 |
      在这里。flush()方法刷新流,close()方法关闭编写器。
      输出:

      Output : 
      Buffered Writer start writing :)
      Written successfully

     
    转载请保留页面地址:https://www.breakyizhan.com/java/4273.html