62,620
社区成员
发帖
与我相关
我的任务
分享
import java.io.*;
public class WriterDemo
{
public static void main(String[] args)
{
OutputStreamWriter osw=null;
PrintWriter pw=null;
BufferedWriter bw=null;
try{
osw=new OutputStreamWriter(new FileOutputStream("c:/write.txt",true),"GBK");
pw=new PrintWriter("c:/write.txt","GBK");
bw=new BufferedWriter(new FileWriter("c:/write.txt",true));
String str="9. 分别使用OutputStreamWriter、PrintWriter和BufferedWriter";
String str2="类将三行字符串写入到文本文件中,";
String str3="仔细分析有何差别";
String str4="youedkdkdkdkdlkaldalelrladl";
String str5="youedkdkdkdkdlkaldalelrladl";
String str6="youedkdkdkdkdlkaldalelrladl";
String str7="ssssssssssssssssssdtfwserewdd";
String str8="ssssssssssssssssssdtfwserewdd";
String str9="ssssssssssssssssssdtfwserewdd";
pw.println(str);
pw.println(str2);
pw.println(str3);
osw.write(str4+"\r\n");
osw.write(str5+"\r\n");
osw.write(str6+"\r\n");
bw.write(str7);
bw.newLine();
bw.write(str8);
bw.newLine();
bw.write(str9);
}
catch(UnsupportedEncodingException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
finally{
if(osw!=null)
try{
osw.close();
}catch(IOException e){}
if(pw!=null)
pw.close();
if(bw!=null)
try{
bw.close();
}catch(IOException e){}
}
}
}
public void write(String str, int off, int len) throws IOException {
synchronized (lock) {
char cbuf[];
if (len <= writeBufferSize) {
if (writeBuffer == null) {
writeBuffer = new char[writeBufferSize];
}
cbuf = writeBuffer;
} else { // Don't permanently allocate very large buffers.
cbuf = new char[len];
}
str.getChars(off, (off + len), cbuf, 0);
write(cbuf, 0, len);
}
}
public PrintWriter(String fileName) throws FileNotFoundException {
this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),
false);
}