|
|
minijava
最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中write(char[] ch,int off,int length),flush()和close()方法为抽象方法,Reader中read(char[] ch,int off,int length)和close()方法是抽象方法。子类应该分别实现他们。
当我们读写文本文件的时候,采用Reader是非常方便的,比如FileReader,InputStreamReader和BufferedReader。其中最重要的类是InputStreamReader,它是字节转换为字符的桥梁。你可以在构造器重指定编码的方式,如果不指定的话将采用底层操作系统的默认编码方式,例如GBK等。当使用FileReader读取文件的时候。
FileReader fr = new FileReader("ming.txt");
int ch = 0;
while((ch = fr.read())!=-1 )
{
System.out.print((char)ch);
}
其中read()方法返回的是读取得下个字符。当然你也可以使用read(char[] ch,int off,int length)这和处理二进制文件的时候类似,不多说了。如果使用InputStreamReader来读取文件的时候
while((ch = isr.read())!=-1)
{
System.out.print((char)ch);
}
这和FileReader并没有什么区别,事实上在FileReader中的方法都是从InputStreamReader中继承过来的。read()方法是比较好费时间的,如果为了提高效率我们可以使用BufferedReader对Reader进行包装,这样可以提高读取得速度,我们可以一行一行的读取文本,使用readLine()方法。
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("ming.txt")));
String data = null;
while((data = br.readLine())!=null)
{
System.out.println(data);
}
当你明白了如何用Reader来读取文本文件的时候那么用Writer写文件同样非常简单。有一点需要注意,当你写文件的时候,为了提高效率,写入的数据会先放入缓冲区,然后写入文件。因此有时候你需要主动调用flush()方法。与上面对应的写文件的方法为
FileWriter fw = new FileWriter("hello.txt");
String s = "hello world";
fw.write(s,0,s.length());
fw.flush();
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("hello2.txt"));
osw.write(s,0,s.length());
osw.flush();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("hello3.txt")),true);
pw.println(s);
不要忘记用完后关闭流!下面是个小例子,帮助新手理解。其实有的时候java的IO系统是需要我们多记记的,不然哪天就生疏了。测试文件的内容为
ming.txt
欢迎光临J2ME开发网12345
hello world i like java language
import java.io.*;
public class TestFile2
{
public static void main(String[] args) throws IOException
{
FileReader fr = new FileReader("ming.txt");
char[] buffer = new char[1024];
int ch = 0;
while((ch = fr.read())!=-1 )
{
System.out.print((char)ch);
}
InputStreamReader isr = new InputStreamReader(new FileInputStream("ming.txt"));
while((ch = isr.read())!=-1)
{
System.out.print((char)ch);
}
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("ming.txt")));
String data = null;
while((data = br.readLine())!=null)
{
System.out.println(data);
}
FileWriter fw = new FileWriter("hello.txt");
String s = "hello world";
fw.write(s,0,s.length());
fw.flush();
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("hello2.txt"));
osw.write(s,0,s.length());
osw.flush();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("hello3.txt")),true);
pw.println(s);
fr.close();
isr.close();
br.close();
fw.close();
osw.close();
pw.close();
}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=106129
[点击此处收藏本文] mingjava发表于 2004年09月16日 08:42:00
Linux百人免费培训,火热报名!高薪没有捷径——唯有专业享受高薪 签北京就业合同http://www.uplooking... CSDN项目外包平台软件项目外包,软件交易,发项目接项目一切轻松搞定http://prj.csdn.net CSDN邀您共同发展好工作好前途尽在CSDN,快来试试吧http://job.csdn.net/...
aa 发表于2004-12-02 19:17:00 IP: 61.129.97.*
BufferedReader br readline维和不能使用substring
jason 发表于2005-02-28 12:27:00 IP: 61.158.105.*
为什么我的这个程序执行不了,请各位高手赐教!请您写的详细些‘
因为我是刚刚学java的。谢谢
abstract class abstractClass {
abstract void caculate(int x,int y);
void abstractPrint(){
System.out.println("This is in abstract class.");
}
}
abstract class SubClass1 extends abstractClass{
private double r;
void Caculate(int x,int y){
r=x*y;
}
double getR(){
return r;
}
void print(){
System.out.println("This is in subclass1.");
System.out.println("rectangle is:"+r);
}
}
abstract class SubClass2 extends abstractClass{
private double r=1;
void Caculate(int x,int y){
if(y>0)
{
for (int i=1;i<=y;i++)
r*=x;
}}
double getR(){
return r;
}
void print(){
System.out.println("This is in SubClass2.");
System.out.println("y is x e:"+r);
}
}
abstract class SubClass3 extends abstractClass{
private double r;
void Caculate(int x,int y){
r=2*(x+y);
}
double getR(){
return r;
}
void Print(){
System.out.println("This is in Subclass3.");
System.out.println("rectangle's is abc:"+r);
}
}
class AbstractclassTest{
public static void main(String args[]){
SubClass1 subObj1=new SubClass1();
subObj1.abstractPrint();
subObj1.Cacularte(3,4);
subObj1.print();
SubClass2 subObj2=new SubClass2();
subObj2.caculate(3,4);
subObj2.print();
SubClass subObj3=new subClass3();
subObj3.Caculate(3,4);
subObj3.print();
}
}
|
|