1. File类是代表文件信息的类,而不是文件的内容
public class FileTest { public static void main(String[] args) { File f = new File("1.txt"); if(f.exists()){ f.delete(); }else{ try { f.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("File name :" + f.getName()); System.out.println("File path :" + f.getPath()); System.out.println("File abs path :" + f.getAbsolutePath()); } }
2. 文件分隔符的区别:在win中是‘\’在unix中是‘/’
3. RandomAccessFile类的作用,是功能比较全的文件访问类,它具有文件指示器;
创建雇员类:
publicclass Employee { public String name; publicintage; publicstaticfinalintLEN = 8; public Employee(String name, int age) { if (name.length() > LEN) { name = name.substring(0, LEN); } else { while (name.length() < LEN) { name += '\u0000'; } } this.name = name; this.age = age; } }
创建读取信息类:
public class RandomFileTest { public static void main(String[] args) throws Exception { Employee e1 = new Employee("张三", 21); Employee e2 = new Employee("lisi", 24); Employee e3 = new Employee("wangwu", 23); RandomAccessFile accessFile = new RandomAccessFile("employee.txt", "rw"); accessFile.writeChars(e1.name); accessFile.writeInt(e1.age); accessFile.writeChars(e2.name); accessFile.writeInt(e2.age); accessFile.writeChars(e3.name); accessFile.writeInt(e3.age); accessFile.close(); String strName = ""; RandomAccessFile accessFile2 = new RandomAccessFile("employee.txt", "r"); accessFile2.skipBytes(Employee.LEN*2 + 4); for(int i = 0;i < Employee.LEN;i++){ strName += accessFile2.readChar(); } System.out.println(strName.trim() + " : " + accessFile2.readInt()); strName = ""; accessFile2.seek(0); for(int i = 0;i < Employee.LEN;i++){ strName += accessFile2.readChar(); } System.out.println(strName.trim() + " : " + accessFile2.readInt()); strName = ""; accessFile2.skipBytes(Employee.LEN*2 + 4); for(int i = 0;i < Employee.LEN;i++){ strName += accessFile2.readChar(); } System.out.println(strName.trim() + " : " + accessFile2.readInt()); accessFile2.close(); } }
注意Unicode编码占用的字节数是两个字节;在进行写入操作的时候,对于字母的写入占用一个字节,而对于中文的写入占用两个字节,所以容易出现乱码的现象;所以使用RandomAccessFile类的readChar和writeChars方法进行name的读写;同时读写age时使用writeInt方法和readInt方法;
4. 节点流
5. 注意文件和流的区别:文件是数据的存储形式,流是指数据传输时的形态;
6. 流分为两大类:节点流和过滤流
InputStream类的使用,此抽象类是表示字节输入流的所有类的超类。
7. 有了垃圾回收机制为什么还要调用close方法,理解Java对象的开销和运行过程中产生的资源的区别;
8. 输入和输出都是相对于应用程序而言的,不是相对于文件而言的;
9. 向二进制文件中写入和读出内容:
public class FileStream { public static void main(String[] args) throws Exception { FileOutputStream out = new FileOutputStream("hello.txt"); out.write("www.baidu.com".getBytes()); out.close(); File f = new File("hello.txt"); byte[] buf = newbyte[1024]; int len = 0; FileInputStream in = new FileInputStream(f); len = in.read(buf); System.out.println(new String(buf, 0, len)); in.close(); } }
10.处理字符的输入和输出:reader和writer(和inputstream和ouputstream相对)
11.二进制文件和文本文件的区别
对文本文件进行读写:
public class FileStream2 { public static void main(String[] args) throws Exception { FileWriter writer = new FileWriter("hello2.txt"); writer.write("www.google.com"); writer.close(); File f = new File("hello2.txt"); char[] buf = newchar[1024]; FileReader reader = new FileReader(f); int len = reader.read(buf); reader.close(); System.out.println(new String(buf,0,len)); } }
12.PipedInputStream和PipedOutputStream类的使用,进行管道输入输出流的操作;
13.线程间的通信:
发送类:
public class Sender extendsThread{ private PipedOutputStream out = new PipedOutputStream(); public PipedOutputStream getPipedOutputStream(){ returnout; } @Override publicvoid run() { String strInfo = "hello,receiver!"; try { out.write(strInfo.getBytes()); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
接受类:
public class Receiver extends Thread { private PipedInputStream in = new PipedInputStream(); public PipedInputStream getPipedInputStream() { returnin; } @Override publicvoid run() { byte[] buf = newbyte[1024]; int len = 0; try { len = in.read(buf); in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(new String(buf, 0, len)); } }
测试类:
public class PipedStreamTest { public static void main(String[] args) throws Exception { Sender sender = new Sender(); Receiver receiver = new Receiver(); PipedOutputStream out =sender.getPipedOutputStream(); PipedInputStream in = receiver.getPipedInputStream(); out.connect(in);//将管道流进行连接 sender.start(); receiver.start(); } }
14.ByteArrayInputStream类和ByteArrayOutputStream类的操作,实现用IO流的形式对字节数组进行操作,它的作用是模拟出虚拟文件;
public class ByteArrayTest { public static void main(String[] args) { String tmp = "abcdefghijklmnopq"; byte[] src = tmp.getBytes(); ByteArrayInputStream in = new ByteArrayInputStream(src); ByteArrayOutputStream out = new ByteArrayOutputStream(); transform(in, out); System.out.println(new String(out.toByteArray())); } publicstaticvoid transform(InputStream in, OutputStream out){ int ch = 0; try { while ((ch = in.read()) != -1) { int upperCh = Character.toUpperCase(ch); out.write(upperCh); } in.close(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
上边的实例很类似于屏幕共享软件的原理;
15.字符编码
16.过滤流和包装类
17.包装类用于将不同类型的数据写入到输出设备中;
比如DataOutputStream类,数据输出流允许应用程序以适当方式将基本 Java 数据类型写入输出流中。然后,应用程序可以使用数据输入流将数据读入。
18.BufferedInputStream 为另一个输入流添加一些功能,即缓冲输入以及支持 mark 和 reset 方法的能力。
19.
public class DataStreamTest { public static void main(String[] args) throws Exception { FileOutputStream fos = new FileOutputStream("dataStream.txt"); BufferedOutputStreambos = newBufferedOutputStream(fos); DataOutputStreamdos = newDataOutputStream(bos); dos.writeUTF("ab中国"); dos.writeBytes("ab中国"); dos.writeChars("ab中国"); dos.close(); FileInputStream fis = new FileInputStream("dataStream.txt"); BufferedInputStreambis = newBufferedInputStream(fis); DataInputStreamdis = newDataInputStream(bis); System.out.println(dis.readUTF()); } }
20.ObjectInputStream类和ObjectOutputStream类,ObjectOutputStream 将 Java 对象的基本数据类型和图形写入 OutputStream。可以使用 ObjectInputStream 读取(重构)对象。通过在流中使用文件可以实现对象的持久存储。如果流是网络套接字流,则可以在另一台主机上或另一个进程中重构对象。这两个类接受的参数必须是可序列化的对象;
学生类:
public class Student implements Serializable{ intid; String name; intage; String department; public Student(int id, String name, int age, String department) { this.id = id; this.name = name; this.age = age; this.department = department; } }
测试类:
public class Serialization { public static void main(String[] args) throws Exception { Student s1 = new Student(12, "张三", 21, "huaxue"); Student s2 = new Student(13, "lisi", 22, "wuli"); FileOutputStream fos = new FileOutputStream("student.txt"); ObjectOutputStreamoos = newObjectOutputStream(fos); oos.writeObject(s1); oos.writeObject(s2); oos.close(); FileInputStream fis = new FileInputStream("student.txt"); ObjectInputStreamois = newObjectInputStream(fis); s1= (Student) ois.readObject(); s2= (Student) ois.readObject(); ois.close(); System.out.println("id:" + s1.id); System.out.println("name:" + s1.name); System.out.println("age:" + s1.age); System.out.println("department:" + s1.department); System.out.println("id:" + s2.id); System.out.println("name:" + s2.name); System.out.println("age:" + s2.age); System.out.println("department:" + s2.department); } }
21.字节流与字符流的转换:从键盘读取一行字符串
22.
OutputStreamWriter类,OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的 charset 将要写入流中的字符编码成字节。它使用的字符集可以由名称指定或显式给定,否则将接受平台默认的字符集。注意:
23.Java程序与其他进程的数据通信
publicclass TestInOut implements Runnable { Process p = null; publicstaticvoid main(String[] args) { TestInOut tio = new TestInOut(); tio.send(); } public TestInOut() { try { p = Runtime.getRuntime().exec("java MyTest"); new Thread(this).start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } publicvoid send() { OutputStream ops = p.getOutputStream(); try { while(true){ ops.write("help\r\n".getBytes()); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override publicvoid run() { // TODOAuto-generated method stub InputStream ips = p.getInputStream(); BufferedReader bfr = new BufferedReader(new InputStreamReader(ips)); String strLine = ""; try { while (true) { strLine = bfr.readLine(); if (strLine != null) { System.out.println(strLine); } else { return; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
编写的MyTest类:作为子进程
publicclass MyTest { publicstaticvoid main(String[] args) { BufferedReader bfr = new BufferedReader( new InputStreamReader(System.in)); while (true) { try { String strLine = bfr.readLine(); if (strLine != null) { System.out.println("hi:" +strLine); } else { return; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
24.IO的层次结构
25.包装模式