文件IO操作

来源:互联网 发布:java异常中的finally 编辑:程序博客网 时间:2024/06/06 02:26

1.File类是IO包中唯一代表磁盘文件本身的对象。

2.RandomAccessFile类支持“随机访问”,可以跳转到文件的任意位置处读写数据。RandomAccessFile类有个文件指示器,指向当前读写处的位置。

RandomAccessFile可以只读或读写方式打开文件:

1)new RandomAccessFile(f,“rw”);//读写方式

2)new RandomAccessFile(f,“r”);//只读方式

package java_test;import java.io.FileNotFoundException;import java.io.RandomAccessFile;public class RandomFileDemo {public static void main(String[] args) throws Exception{Employee e1=new Employee("zhangsan",23);Employee e2=new Employee("wangwu",26);RandomAccessFile ra = new RandomAccessFile("C:\\Users\\xuhai\\Desktop\\e.txt","rw");ra.write(e1.name.getBytes());ra.writeInt(e1.age);ra.write(e2.name.getBytes());ra.writeInt(e2.age);ra.close();RandomAccessFile raf=new RandomAccessFile("C:\\Users\\xuhai\\Desktop\\e.txt","r");int len=8;raf.skipBytes(12);//跳过第一个员工的信息,其姓名8字节,年龄4字节System.out.println("第二个员工的信息");String str="";for(int i=0;i<len;i++)str=str+(char)raf.readByte();System.out.println("name:"+str);System.out.println("age:"+raf.readInt());System.out.println("第一个员工的信息");raf.seek(0);//将文件指针移动到文件开始的位置(其开销较大,一般不使用)str="";for(int i=0;i<len;i++)str=str+(char)raf.readByte();System.out.println("name:"+str);System.out.println("age:"+raf.readInt());raf.close();}}class Employee{String name;int age;final static int LEN=8;//定义字节长度为8public Employee(String name,int age){if(name.length()>LEN){name=name.substring(0, 8);//此函数能够取出一部分字节}//若名字超过8字节,则截取0到8elsewhile(name.length()<LEN)name=name+"\u0000";//若名字少于8字节,则后面补全this.name=name;this.age=age;}}

3)流类

java的流式输入/输出建立在4个抽象类的基础上,InputString、OutputString、Reader、Writer。

字节流类:InputStream/OutputStream(处理字节或二进制对象)

字符流类:Reader/Writer(处理字符或字符串)

一般的文件操作,不管是字节流还是字符流,都可以按照下面的方式进行。

1)使用File类找到一个文件

2)通过File类的对象去实例化字节流或字符流的子类

3)进行字节或字符的读写操作

4)关闭文件流

字节流:以inputStream和OutputStream为顶层,但是多数方法由他们的子类来实现

FileInputStream(从文件中读取字节的类),常用的两个构造方法:

FileInputStream(String filepath)

FileInputStream(File fileObj)

FileOutputStream(文件输出流)

FileOutputStream(StringfilePath)

FileOutputStream(File fileObject)

FileOutputStream(String filePath,boolean append)

其中:filePath是文件的绝对路径,fileObj描述该文件的File对象,若append为true,文件则以搜索路径模式打开。

字节流

package java_test;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;public class StreamDemo {public static void main(String [] args){File f= new File("C:\\Users\\xuhai\\Desktop\\e.txt");OutputStream out=null;try {out=new FileOutputStream(f);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}byte b[]="Hellow World".getBytes();//将字符串改为字符数组try {out.write(b);//将byte数组写入到文件中} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {out.close();//记得关闭文件操作} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}InputStream in=null;try {in =new FileInputStream(f);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}byte b1[]=new byte[1024];//开辟一个空间用于接收文件读进来的数据int i=0;try {i=in.read(b1);//将b1的引用传递到read()方法中,同时此方法返回读入数据的个数} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {in.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(new String(b1,0,i));//将byte数组转换为字符串输出     String(byte[] byte,int start,int length)}}

字符流:顶层是Reader和Writer抽象类

FileReader类创建了一个可以读取文件内容的Reader类,构造方法:

FileReader(String filePath)

FileReader(File fileObject)

FileWriter创建一个可以写文件的Writer类,构造方法:

FileWriter(String filePath)

FileWriter(String filePath,boolean append)

FileWriter (File fileObj)

参数和前面的一样

3)管道流:主要用于连接两个线程之间的通信。管道流也分为字节流(PipedInputStreutm、PipedOutputStream)和字符流(PipedReader、PipedWriter)。

字节流:PipedOutputStream(向管道内写入数据)PipedInputStream(从管道中读取PipedOutputStream写入的数据),主要完成线程间的通信。


package java_test;import java.io.IOException;import java.io.PipedInputStream;import java.io.PipedOutputStream;public class PipedStreamDemo {public static void main(String args[]){Sender sender=new Sender();  //产生两个线程对象Receiver receiver=new Receiver();PipedOutputStream out=sender.getOutputStream();//写入PipedInputStream in=receiver.getInputSream();//读出try {out.connect(in);//将输出发送发到输入sender.start();//启动线程receiver.start();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}class Sender extends Thread{private PipedOutputStream out= new PipedOutputStream();public PipedOutputStream getOutputStream(){return out;}public void run(){String s=new String("Receiver 你好!");try {out.write(s.getBytes());//写入(发送)} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {out.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}class Receiver extends Thread{private PipedInputStream in=new PipedInputStream();public PipedInputStream getInputSream(){return in;}public void run(){String s=null;byte[] buf=new byte[1024];try {int len=in.read(buf);//读出数据s=new String(buf,0,len);System.out.println("收到了以下消息:"+s);in.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}



0 0
原创粉丝点击