JAVA伊奥。OutputStreamWriter类方法

io.OutputStreamWriter Class methods

null

OutputStreamWriter 类将字符流连接到字节流。它使用指定的字符集将字符编码为字节。 宣言:

public class OutputStreamWriter   extends Writer

施工人员:

  • OutputStreamWriter(OutputStream geek_out): 创建一个“geek_out”OutputStreamWriter,使用 用于编码的默认字符集。
  • OutputStreamWriter(OutputStream geek_out、Charset geek_set): 创建一个“geek_out”输出StreamWriter,它使用“geek_set”字符集进行编码。
  • OutputStreamWriter(OutputStream geek_out,字符集编码器编码): 创建使用给定编码器的“geek_out”OutputStreamWriter。
  • OutputStreamWriter(OutputStream geek_out,字符串setName): 创建使用命名字符集的“geek_out”OutputStreamWriter。

方法:

  • flush():java。伊奥。OutputStreamWriter。同花顺 冲水。 语法:
public void flush()Parameters : ------Return : voidException : -> IOException : if in case anu I/O error occurs.
  • close():java。伊奥。OutputStreamWriter。关闭() 关闭冲洗过的水流。 语法:
public void close()Parameters : ------Return : voidException : -> IOException : if in case anu I/O error occurs, e.g writing after closing the stream
  • write(int char):java。伊奥。OutputStreamWriter。写入(int char) 只写一个字符。 语法:
public void write(int char)Parameters : char : character to be writtenReturn : void

JAVA

// Java code explaining the working of write(int char), flush(), close()
import java.io.*;
public class NewClass
{
public static void main(String[] args)
{
try
{
// Creation of new OutputStreamWriter
OutputStream g = new FileOutputStream( "test.txt" );
OutputStreamWriter geeks_out1 = new OutputStreamWriter(g);
// Creating an Input Stream
FileInputStream in = new FileInputStream( "test.txt" );
// Use of write(int char) :
// Writing character values to the "test.txt"
geeks_out1.write( 71 );
geeks_out1.write( 69 );
geeks_out1.write( 69 );
geeks_out1.write( 75 );
geeks_out1.write( 83 );
// flush the stream
geeks_out1.flush();
// read what we write
for ( int i = 0 ; i < 5 ; i++)
{
// Reading the content of "test.txt" file
System.out.println( "write(int char) : " + ( char ) in.read());
}
geeks_out1.close();
}
catch (Exception ex)
{
System.out.println( "Error" );
ex.printStackTrace();
}
}
}


  • 输出:
write(int char) : Gwrite(int char) : Ewrite(int char) : Ewrite(int char) : Kwrite(int char) : S
  • write(字符串极客、int offset、int strlen):java。伊奥。OutputStreamWriter。写入(字符串极客、整数偏移量、整数字符串) 写入从“偏移位置”到“strlen”长度的“geek”字符串的一部分。 语法:
public void write(String geek, int offset, int strlen)Parameters : geek : string whose portion is to be written offset : starting position from where to writestrlen : length upto which we need to writeReturn : voidException : -> IOException : if in case any I/O error occurs.

JAVA

// Java code explaining the working of write(String geek, int offset, int strlen))
import java.io.*;
public class NewClass
{
public static void main(String[] args)
{
String geek = "GEEKSForGeeks" ;
try
{
// Creation of new OutputStreamWriter
OutputStream g = new FileOutputStream( "test.txt" );
OutputStreamWriter geeks_out1 = new OutputStreamWriter(g);
// Creating an Input Stream
FileInputStream in = new FileInputStream( "test.txt" );
// Use of writewrite(String geek, int offset, int strlen)) :
// Writing character values to the "test.txt"
geeks_out1.write(geek, 4 , 9 );
// flush the stream
geeks_out1.flush();
// read what we write
for ( int i = 0 ; i < 5 ; i++)
{
// Reading the content of "test.txt" file
System.out.println( "write(int char) : " + ( char ) in.read());
}
geeks_out1.close();
}
catch (Exception ex)
{
System.out.println( "Error" );
ex.printStackTrace();
}
}
}


  • 输出:
write(int char) : Swrite(int char) : Fwrite(int char) : owrite(int char) : rwrite(int char) : G
  • 写(char[]geek,int offset,int strlen):java。伊奥。OutputStreamWriter。写入(字符[]极客,整数偏移量,整数strlen) 写入从“偏移位置”到“strlen”长度的“geek”字符数组的一部分。 语法:
public void write(char[] geek, int offset, int strlen)Parameters : geek : character array whose portion is to be written offset : starting position from where to writestrlen : length upto which we need to writeReturn : voidException : -> IOException : if in case anu I/O error occurs.
  • getEncoding():java。伊奥。OutputStreamWriter。getEncoding() 告诉所述流中使用的字符编码的名称。 如果存在预定义名称,则返回该名称,否则返回编码的规范名称。 如果流已经关闭,则返回Null。 语法:
public String getEncoding()Parameters : ------Return : Name of the charset encoding usedException : -> IOException : if in case anu I/O error occurs.

JAVA

// Java code explaining write(char[] geek, int offset, int strlen)
// and getEncoding() method
import java.io.*;
public class NewClass
{
public static void main(String[] args)
{
char [] geek = { 'G' , 'E' , 'E' , 'K' , 'S' };
try
{
// Creation of new OutputStreamWriter
OutputStream g = new FileOutputStream( "test.txt" );
OutputStreamWriter geeks_out1 = new OutputStreamWriter(g);
// Creating an Input Stream
FileInputStream in = new FileInputStream( "test.txt" );
// Use of writewrite(char[] geek, int offset, int strlen)) :
// Writing character values to the "test.txt"
geeks_out1.write(geek, 0 , 3 );
// flush the stream
geeks_out1.flush();
// read what we write
for ( int i = 0 ; i < 3 ; i++)
{
// Reading the content of "test.txt" file
System.out.println( "char[] geek, int offset, int strlen) : "
+ ( char ) in.read());
}
// get and print the encoding for this stream
System.out.println( "Name of the charset : "
+ geeks_out1.getEncoding());
// Closing the OutputStreamWriter
geeks_out1.close();
}
catch (Exception ex)
{
System.out.println( "Error" );
ex.printStackTrace();
}
}
}


  • 输出:
char[] geek, int offset, int strlen) : Gchar[] geek, int offset, int strlen) : Echar[] geek, int offset, int strlen) : EName of the charset : UTF8

本文由 莫希特·古普塔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享