简体   繁体   中英

Reading from a file and writing it back to a file in java(Having trouble understanding BufferedWriter write method in java)

I'm confused with the the java BufferedWriter write method. The java documents says that write takes int as an argument.

void write(int c) Writes a single character.

But I have used write method in my code, and I have passed String into it

BufferedReader br=new BufferedReader(file);
BufferedWriter bw=new BufferedWriter(file1);
String line=null;
while((line=br.readLine())!=null) 
{
  bw.write(line); // Why is this statement working?
  bw.write("\r\n");
}

I passing "line" to bw.write(line) and "line" is a string, why is this working?Is write 
method  overloaded?But I don't see it in docs.

因为存在一个接受字符串的write()BufferedWriter “ IS-A” Writer子类,所以这就是它具有write(String)原因,因为Writer类具有它。

Api copy / paste : http://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html

Class BufferedWriter

java.lang.Object java.io.Writer <------ *has method write(String str) java.io.BufferedWriter

There is a write method which is capable to take a string as param and write it.

As Luis Alberto allready mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM