黑马程序员技术交流社区
标题:
FileReader,FileWriter
[打印本页]
作者:
ifuzhen
时间:
2014-4-19 22:46
标题:
FileReader,FileWriter
import java.util.*;
import java.io.*;
class Test
{
public static void main(String [] args)
{
fileReader();
}
public static void fileReader()
{
FileReader fr =null;
try
{
fr=new FileReader("d:\\who.txt");
int ch =0;
/*while(true)
{
ch=fr.read();
if (ch==-1)
break;
sop("ch=="+(char)ch);
}*/
while((ch=fr.read())!=-1)
{
sop("charinwhile2:"+(char)ch);
}
}
catch (IOException e)
{
sop("FileNotFoundException:"+e.toString());
}
finally
{
try
{
fr.close();
}
catch (IOException e)
{
sop(e.toString());
}
}
}
public static void filedemo()
{
FileWriter fw=null;
try
{
fw =new FileWriter ("d:\\who.txt",true);
fw.write("\r\n come on");
}
catch (IOException e)
{
sop(e.toString());
}
finally
{
try
{
if (!(fw==null))
{
//fw.write("ppp");
fw.close();
}
}
catch (IOException e )
{
sop("catch:"+e.toString());
}
}
}
}
复制代码
作者:
ifuzhen
时间:
2014-4-19 23:03
public static void FileReaderDemo2()
{
FileReader fr=null;
try
{
fr =new FileReader("d:\\who.txt");
char[] buf =new char[1024];
int num =0;
while ((num=fr.read(buf))!=-1)
{
sop(new String(buf,0,num));
}
}
catch (IOException e)
{
sop("catch:"+e.toString());
}
finally
{
try
{
fr.close();
}
catch (IOException e)
{
sop("finally catch:"+e.toString());
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2