当前位置: 技术问答>java相关
*******很简单的一个问题,帮帮忙!*********
来源: 互联网 发布时间:2015-02-06
本文导语: 为什么下面这个程序不能写那个文本文件,老是写不进去。 import java.io.*; public class OutputIO { File file=new File("err.txt"); FileWriter fw=null; PrintWriter rw=null; public OutputIO() { try { fw=new FileWriter(file,true); rw=new Pri...
为什么下面这个程序不能写那个文本文件,老是写不进去。
import java.io.*;
public class OutputIO
{
File file=new File("err.txt");
FileWriter fw=null;
PrintWriter rw=null;
public OutputIO()
{
try
{
fw=new FileWriter(file,true);
rw=new PrintWriter(fw);
}
catch(IOException e)
{
System.out.println("file not found or system have error");
}
}
public void write()
{
this.rw.println(new String("hello"));
}
public static void main(String[] args)
{
OutputIO a=new OutputIO();
a.write();
System.out.println("WRITE FILE OK! ! !");
}
}
import java.io.*;
public class OutputIO
{
File file=new File("err.txt");
FileWriter fw=null;
PrintWriter rw=null;
public OutputIO()
{
try
{
fw=new FileWriter(file,true);
rw=new PrintWriter(fw);
}
catch(IOException e)
{
System.out.println("file not found or system have error");
}
}
public void write()
{
this.rw.println(new String("hello"));
}
public static void main(String[] args)
{
OutputIO a=new OutputIO();
a.write();
System.out.println("WRITE FILE OK! ! !");
}
}
|
import java.io.*;
public class OutputIO
{
File file=new File("err.txt");
FileWriter fw=null;
PrintWriter rw=null;
public OutputIO()
{
try
{
fw=new FileWriter(file,true);
rw=new PrintWriter(fw);
}
catch(IOException e)
{
System.out.println("file not found or system have error");
}
}
public void write()
{
this.rw.println(new String("hello"));
this.rw.flush();
}
public static void main(String[] args)
{
OutputIO a=new OutputIO();
a.write();
System.out.println("WRITE FILE OK! ! !");
}
}
|
rw = new PrintWriter(fw,true);
~~~~~~ 自动flush();
flush();将内存中的数据写入 ;
import java.io.*;
public class OutputIO
{
File file=new File("err.txt");
FileWriter fw=null;
PrintWriter rw=null;
public OutputIO()
{
try
{
fw = new FileWriter(file);
rw = new PrintWriter(fw,true);
}
catch(IOException e)
{
System.out.println("file not found or system have error");
}
}
public void write()
{
rw.println("lkjklj");
}
public static void main(String[] args)
{
OutputIO a=new OutputIO();
a.write();
System.out.println("WRITE FILE OK! ! !");
a.closeall();
}
public void closeall(){
try{
rw.close();
fw.close();
}catch(IOException io){
}
}
}
~~~~~~ 自动flush();
flush();将内存中的数据写入 ;
import java.io.*;
public class OutputIO
{
File file=new File("err.txt");
FileWriter fw=null;
PrintWriter rw=null;
public OutputIO()
{
try
{
fw = new FileWriter(file);
rw = new PrintWriter(fw,true);
}
catch(IOException e)
{
System.out.println("file not found or system have error");
}
}
public void write()
{
rw.println("lkjklj");
}
public static void main(String[] args)
{
OutputIO a=new OutputIO();
a.write();
System.out.println("WRITE FILE OK! ! !");
a.closeall();
}
public void closeall(){
try{
rw.close();
fw.close();
}catch(IOException io){
}
}
}
|
rw = new PrintWriter(fw,true);
~~~~~~ 自动flush();
flush();将内存中的数据写入 底层流;
~~~~~~ 自动flush();
flush();将内存中的数据写入 底层流;