1、File
一个File类的对象,表示了磁盘上的文件或目录
•File类提供了与平台无关的方法来对磁盘上的文件或目录进行操作•File类没有指定信息怎样从文件读取或向文件存储
File myFile = new File(" myfile. txt"); File myDir = new File(" MyDocs"); File myFile = new File( myDir, "myfile. txt");这些构造方法取决于访问文件的方式。例如,若在应用程序里只用一个文件,第一种创建文件的结构是最容易的。但若在同一目录里打开数个文件,则后种方法更好一些。
判断是文件还是目录
file.isDirectory(); file.isFile();创建目录
File file =new File("D:/wang/bing/jia"); System.out.println(file.isDirectory()); file.mkdirs(); System.out.println(file.isDirectory());过滤文件:利用匿名内部类
String [] names= file.list(new FilenameFilter() { @Override public boolean accept(File dir, String name) { if (name .endsWith(".txt")) { return true; } return false; } });
2、stream
字节流
InputStream
FileInputStream fileInputStream1=new FileInputStream("D:/bingjia.txt"); byte []buffer=new byte [1024]; int length=fileInputStream1.read(buffer,0,buffer.length); while(length!=-1) { String string =new String(buffer,0,length); System.out.println(string); length=fileInputStream1.read(buffer,0,buffer.length); } fileInputStream1.close();OutPutStream
OutputStream outputStream =new FileOutputStream("D:/miss.txt"); String string ="i miss you"; byte [] bufferwrite=string.getBytes(); outputStream.write(bufferwrite); outputStream.close();缓冲字节流
OutputStream outputStream=new FileOutputStream("D:/miss1.txt"); BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(outputStream); bufferedOutputStream.write("i miss you".getBytes()); bufferedOutputStream.close();
数据文件流
DataOutputStream(写入二进制文件)
写文件
DataOutputStream dataOutputStream=new DataOutputStream (new BufferedOutputStream(new FileOutputStream("D:/missbingjia.txt"))); byte a=1; int b=222; char c='b'; dataOutputStream.writeByte(a); dataOutputStream.writeInt(b); dataOutputStream.writeChar(c); dataOutputStream.close();
读文件
DataInputStream dataInputStream=new DataInputStream(new BufferedInputStream(new FileInputStream("D:/missbingjia"))); System.out.println(dataInputStream.readByte()); System.out.println(dataInputStream.readInt()); System.out.println(dataInputStream.readChar());
由于Java采用16位的Unicode字符,因此需要基于字符的输入/输出操作。从Java1.1版开始,加入了专门处理字符流的抽象类Reader和Writer,前者用于处理输入,后者用于处理输出。这两个类类似于InputStream和OuputStream,也只是提供一些用于字符流的规定,本身不能用来生成对象
Java程序语言使用Unicode来表示字符串和字符, Unicode使用两个字节来表示一个字符,即一个字符占16位
FileOutputStream fos = new FileOutputStream("file.txt"); OutputStreamWriter osw = new OutputStreamWriter(fos); BufferedWriter bw = new BufferedWriter(osw); bw.write("http://www.google.com"); bw.write("\n"); bw.write("http://www.baidu.com"); bw.close(); FileInputStream fis = new FileInputStream("file.txt"); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); String str = br.readLine(); while(null != str) { System.out.println(str); str = br.readLine(); } br.close();
FileReader fileReader=new FileReader("D:/bingjia.txt"); char []buffer=new char [1024]; fileReader.read(buffer); fileReader.close(); System.out.println(new String(buffer));
ASCII码与一个8位(bit)二进制数对应。其最高位是0,相应的十进制数是0-127。如,数字“0”的编码用十进制数表示就是48。另有128个扩展的ASCII码,最高位都是1,由一些制表符和其它符号组成。ASCII是现今最通用的单字节编码系统。
Unicode
Unicode:这是一种通用的字符集,对所有语言的文字进行了统一编码,对每一个字符都用2个字节来表示,对于英文字符采取前面加“0”字节的策略实现等长兼容。如 “a” 的ASCII码为0x61,UNICODE就为0x00,0x61。(在internet上传输效率较低)
UTF-8
Eight-bit UCS Transformation Format,(UCS,Universal Character Set,通用字符集,UCS 是所有其他字符集标准的一个超集)。一个7位的ASCII码值,对应的UTF码是一个字节。如果字符是0x0000,或在0x0080与0x007f之间,对应的UTF码是两个字节,如果字符在0x0800与0xffff之间,对应的UTF码是三个字节(汉字为3个字节) 不等长
RandomAccessFile
RandomAccessFile类同时实现了DataInput和DataOutput接口,提供了对文件随机存取的功能,利用这个类可以在文件的任何位置读取或写入数据。
•RandomAccessFile类提供了一个文件指针,用来标志要进行读写操作的下一数据的位置。
RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");
public void write(RandomAccessFile raf) throws Exception <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>raf.writeInt(this.id); <span style="white-space:pre"> </span>raf.writeUTF(this.name); <span style="white-space:pre"> </span>raf.writeDouble(this.height); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void read(RandomAccessFile raf) throws Exception <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>this.id = raf.readInt(); <span style="white-space:pre"> </span>this.name = raf.readUTF(); <span style="white-space:pre"> </span>this.height = raf.readDouble(); <span style="white-space:pre"> </span>}
将对象转换为字节流保存起来,并在以后还原这个对象,这种机制叫做对象序列化。
一个类若想被序列化,则需要实现java.io.Serializable接口,该接口中没有定义任何方法,是一个标识性接口(Marker Interface),当一个类实现了该接口,就表示这个类的对象是可以序列化的。
在序列化时,static变量是无法序列化的;如果A包含了对B的引用,那么在序列化A的时候也会将B一并地序列化;如果此时A可以序列化,B无法序列化,那么当序列化A的时候就会发生异常,这时就需要将对B的引用设为transient,该关键字表示变量不会被序列化。
class Person implements Serializable { int age; String name; double height; public Person(int age, String name, double height) { this.age = age; this.name = name; this.height = height; } }
public static void main(String[] args) throws Exception { Person p1 = new Person(20, "zhangsan", 4.55); Person p2 = new Person(50, "lisi", 4.67); Person p3 = new Person(10, "wangwu", 17.78); FileOutputStream fos = new FileOutputStream("person.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(p1); oos.writeObject(p2); oos.writeObject(p3); oos.close(); System.out.println("--------------------"); FileInputStream fis = new FileInputStream("person.txt"); ObjectInputStream ois = new ObjectInputStream(fis); Person p = null; for (int i = 0; i < 3; i++) { p = (Person) ois.readObject(); System.out.println(p.age + "," + p.name + "," + p.height); } ois.close(); }