来自:mjsws > 馆藏分类
配色: 字号:
JAVA6用多种方式实现从文件按行读数据和写入文件数据
2018-06-25 | 阅:  转:  |  分享 
  
JAVA6用多种方式实现从文件按行读数据和写入文件数据作为一个基本知识,对读写文件相信我们在写代码的时候都遇到过,对于我而言我一般是直接Bu
fferReader或者BufferWriter安排上就完事了,但是我们的实验从来不让我失望,他这一次要求我们用三种不同的方式进行
读写文件,没错就是三种。在我好不容易完成了他的要求之后我赶紧将这三种方式变成我的博客嘿嘿嘿1.ReaderandWriter作
为Reader和Writer,他们和InputStream和OutputStream的最大区别是什么,是一次性操作的字节数目,前者
一次操作两个后者一次操作一个。但是在这里我们的主要观测点是在他们的方法上,作为最常用的读写文件的类,Reader和Writer为我
们提供了很方便的方法,再写方面Writer和BufferedWriter都为我们提供了写字符串的方法。直接write()里面放个字
符串就可以了,凡是Reader则没有给我们提供直接按行读取字符串的方法,但是BufferedReader的readline()方法
则可以直接读一行字符串,直接调用即可。太简单了这里就不放代码了。。2.ScannerScanner经常被我们用于读取控制台的输入,
但是我们只需要将他构造函数中的System.in替换成一个File文件,他就可以变成一个读取文件的Scanner了,Scanner
的按行读取方法是nextLine()我们在使用它的时候可以在前面判断它时候hasNextLine()。也是十分方便?1234567
89101112131415Filefile=newFile(fileSource);?try{?in=newScanne
r(file);?}catch(FileNotFoundExceptione){?//TODOAuto-generat
edcatchblock?e.printStackTrace();?}}?@Override?publicStringre
adline(){?if(in.hasNextLine()){?returnin.nextLine(http://www.ro
dlg.com);?}?returnnull;?}3.InputStream和OutputStream下面就是倒霉的FileOu
tputStream和FileInputStream了,这俩倒霉孩子虽说在有时候挺好使的,但是要是按行来是真的麻烦啊,由于这两个只
能读/写byte数组,所以再写的时候我们将字符串调用getBytes()方法转换成byte数组来写?123456789101112
1314151617FileOutputStreamfos;?publicOUTStream(Filefile){?try
{?fos=newFileOutputStream(file);?}catch(FileNotFoundException
e){?//TODOAuto-generatedcatchblock?System.out.println("file
notfind");?e.printStackTrace();?}?}??@Override?publicvoidwrit
e(Stringstring){?//TODOAuto-generatedmethodstub?byteb[]=st
ring.getBytes();?try{?fos.write(b);要说到FileInputStream就更麻烦了我们判断他是
否换行的唯一依据就是\r\n,所以我们只能够一个字节一个字节的读取,并把每次读取的字节转换为char类型存在一个足够大的char数
组中,当读到\r的时候证明这行读完了(当read()返回-1的时候证明文件读完了)这时候我们将char数组变为String返回?1
23456789101112131415161718192021222324252627282930313233343536373
8publicclassINStreamimplementsINstrategy{//useStreamasINs
trategy?privatebyteb[]=newbyte[1];?privatechar[]cs=newchar[
1024];?privateFileInputStreamfis=null;?publicINStream(Stringf
ileSource){?try{?fis=newFileInputStream(fileSource);?}catch(F
ileNotFoundExceptione){?//TODOAuto-generatedcatchblock?Syst
em.out.println("filenotfind");?System.exit(0);?}?}?@Override?pu
blicStringreadline(http://www.482223.com)?{?try{?inti=0;?int
j=0;?cs=newchar[1024];?fis.read(b);?while((b[0]!=''\r'')){//判断是否换
行?if(j==-1){?returnnull;?}?cs[i]=(char)b[0];?j=fis.read(b);//判断是
否读完?i++;?}?if(j!=-1){?fis.read(b);?}?}catch(IOExceptione){?//
TODOAuto-generatedcatchblock?e.printStackTrace();?}?returnnew
String(cs);?}3.FileChannel刚才说了这么多我们很容易发现,这写还差一种啊,不着急最后一种就是我以前根本就
不知到的FileChannel方法,FileChannel翻译过来就是文件通道,按这个翻译看起来应该是能够读写文件的。FileCh
annel类的使用必须配合一个byteBuffer类使用我们再创建一个FileOutputStream对象之后使用getChann
el()方法来创建一条通道,同时我们将要写入的字符串是用getBytes()存在byteBuffer中,翻转byteBuffer使
它变为写状态,只要byteBuffe不为空就像FileChannel中写,最后清空bytebuffer完成写操作?12345678
91011121314151617181920212223242526272829publicclassOUTBufferi
mplementsOUTStrategy{?FileChanneloutChannel;?ByteBufferbyteBuf
fer1=ByteBuffer.allocate(1024);?publicOUTBuffer(Filefile){?try
{?FileOutputStreamfos=newFileOutputStream(file);?outChannel=fo
s.getChannel();?}catch(FileNotFoundExceptione){?System.out.pr
intln("filenotfind");?//TODOAuto-generatedcatchblock?e.prin
tStackTrace();?}?}??@Override?publicvoidwrite(Stringstring){?
//TODOAuto-generatedmethodstub?byteBuffer1.put(string.getByte
s());?byteBuffer1.flip(http://www.267774.com);?while(byteBuffer1.hasRemaining()){?try{?outChannel.write(byteBuffer1);?}catch(IOExceptione){?//TODOAuto-generatedcatchblock?e.printStackTrace();?}?}?byteBuffer1.clear();?}
献花(0)
+1
(本文系mjsws首藏)