1.文件
1.1认识File类
1.2 File类的应用
windows系统中文件路径使用“/”或者“\”作为路径的分隔符,但是在Linux操作系统中只能使用“/”符号分割路径。
import java.io.File; import java.io.IOException; //文件创建 public class Demo10_1 { public static void main(String[] args) { File f1 = new File("d:/dir/File.txt"); if(!f1.exists()) { try { f1.createNewFile(); } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } } else { System.out.print("该文件已存在! "); } } }
import java.io.File; //文件读写判断与文件删除 public class Demo10_1 { public static void main(String[] args) { File f1 = new File("d:/File.txt"); if(!f1.canRead()) { System.out.print("该文件可读! "); } else { System.out.print("该文件不可读! "); } if(!f1.canWrite()) { System.out.print("该文件可写! "); } else { System.out.print("该文件不可写! "); } if (f1.delete()) { System.out.print("文件删除成功! "); } } }
2.字节流与字符流
2.1字节流
以字节B为单位存取数据,输入流使用的是InputStream,输出流使用的是OutputStream。
字节输入流:FileInputStream读取文件信息、ObjectInputStream对象序列化使用、PipedInputStream管道输入流、ByteArrayInputStream字节数组输入流、FilterInputStream过滤输入流
InputStream方法:int read()、int read(byte[] buffer)、int read(byte[] buffer,int offset,int length)、void close()
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Scanner; //ByteArrayOutputStream使用 public class Demo10_2 { public static void main(String[] args) { byte[] b = new byte[1024]; Scanner sc1 = new Scanner(System.in); ByteArrayOutputStream ba1 = new ByteArrayOutputStream(); System.out.println("请输入用户名和密码:"); String str1 = sc1.next(); String str2 = sc1.next(); b = (str1 + ";" + str2).getBytes();//产生字符数组 try { ba1.write(b);//写入字符数组 ba1.flush(); } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } finally { try { ba1.close(); } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } } System.out.print(ba1.toString().toUpperCase());//打印输出 } }
字节输出流:FileOutputStream读取文件信息、ObjectOutputStream对象序列化使用、PipedOutputStream管道输出流、ByteArrayOutputStream字节数组输出流、FilterOutputStream过滤输出流
OutputStream方法:void write(int b)、void write(byte[] buffer)、void write(byte[] buffer,int offset,int length)、void close()、void flush()
import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.Scanner; //ByteArrayInputStream使用 public class Demo10_3 { public static void main(String[] args) { byte[] b = new byte[1024]; Scanner sc1 = new Scanner(System.in); System.out.println("请输入用户名和密码:"); String str1 = sc1.next(); String str2 = sc1.next(); b = (str1 + ";" + str2).getBytes(); ByteArrayInputStream ba2 = new ByteArrayInputStream(b); try { int n = ba2.read(b); String str3 = new String(b, 0, n); System.out.println(str3.toUpperCase()); } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } finally { try { ba2.close(); } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } } } }
2.2字符流
以字符为单位存取数据,字符是指char类型的数据,由两个字节构成,占16位。Reader是所有输入流的父类,Writer是所有输出流的父类。
字符输入流:BufferReader缓存输入流,CharArrayReader字符数组输入流,StringReader字符串输入流,FileReader文件输入流
字符输入流常用方法:int read()、int read(char[] buffer)、int read(char[] buffer,int offset,int length)、void close()
字符输出流:BufferWriter缓存输入流,CharArrayWriter字符数组输入流,StringWriter字符串输入流,FileWriter文件输入流
字符输出流常用方法:void write(String str)、void write(char[] buffer)、void write(char[] buffer,int offset,int length)、void write(int b),void close()、void flush()
3.处理流(包括字节处理流和字符处理流)
3.1认识处理流
字符流和字节流的一种,用于处理简单的字符流和字节流不能实现的功能。
3.2特定数据类型访问处理流(只支持字节流)
输入流:DataInputStream(InputStream in)
输入流方法:boolean readBoolen()、byte readByte()、char readChar()、double readDouble()、float readFloat()、int readInt()、long readLong()、String readUTF()
输出流:DataOutputStream(OutputStream in)
输出流方法:boolean writeBoolen()、byte writeByte()、char writeChar()、double writeDouble()、float writeFloat()、int writeInt()、long writeLong()、String writeUTF()
3.3对象序列化处理流(显式声明、系统默认声明)
将对象转换为字节序列的过程。(1.对象需要永久保存在文件中;2.即时通信中)
字节输入流:ObjectInputStream();字节输出流:ObjectOutputStream();
import java.io.Serializable; //实现Serializable接口 class Student implements Serializable{ private static final long serialVersionUID = 1L;//显式声明版本号 String name; int age; String major; public Student(String name, int age, String major) { this.name = name; this.age = age; this.major = major; } public String toString() { return name + ":" + age + ":" + major; } } import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; //使用对象序列化处理流 public class Demo10_4 { public static void main(String[] args) { Student stu = new Student("张三", 18, "电子科学与技术"); FileOutputStream fo1 = null; ObjectOutputStream oo1 = null; //向文件写入数据 try { fo1 = new FileOutputStream("D:/test.txt"); oo1 = new ObjectOutputStream(fo1); oo1.writeObject(stu); } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } finally { try { fo1.close(); oo1.close(); } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } } //读取文件数据 FileInputStream fi1 = null; ObjectInputStream oi1 = null; try { fi1 = new FileInputStream("D:/test.txt"); oi1 = new ObjectInputStream(fi1); stu = (Student)oi1.readObject();//这里一直提示出错,没有解决 System.out.println(stu); } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } finally { try { fi1.close(); oi1.close(); } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } } } }
3.4缓冲处理流(字节缓冲处理流:设置标记、跳过指定数据、重新读取、字符缓冲处理流:readline)
在缓冲的字节或字符中读取数据,以提高读写的效率。
字节缓冲处理流:
BufferedInputStream,方法(void mark(intreadlimit),void reset(),booleanmarkSupported(),long skip(long n))
BufferedOutputStream
字符缓冲处理流:
BufferedReader与BufferedWriter()