1. 文件
什么是文件?
文件可认为是相关记录或放在一起的数据的集合。
Java程序如何访问文件?
通过Java API :java.io.File 类
File类访问文件属性
File类常用方法
2. 流
如何读写文件?
通过流来读写文件,流是指一连串流动的字符,是以先进先出方式发送信息的通道。
输入/输出流与数据源
Java流的分类
输入输出流是相对于计算机内存来说的。
字节流是 8 位通用字节流,字符流是 16 位 Unicode 字符流。
InputStream类常用方法
int read( )
int read(byte[] b)
int read(byte[] b,int off,int len)
void close( )
int available()
子类FileInputStream常用的构造方法
FileInputStream(File file)
FileInputStream(String name)
使用FileInputStream 读文本文件
File file = new File("/Users/allen/Desktop/page.html"); FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); int data; while((data = fileInputStream.read()) != -1){ System.out.print((char)data); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } }
OutputStream类常用方法
void write(int c)
void write(byte[] buf)
void write(byte[] b,int off,int len)
void close( )
子类FileOutputStream常用的构造方法
FileOutputStream (File file)
FileOutputStream(String name)
FileOutputStream(String name,boolean append)
使用FileOutputStream 写文本文件
FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream("/Users/allen/Desktop/hello.txt"); String str = "好好学习java"; byte[] words = str.getBytes(); fileOutputStream.write(words, 0, words.length); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } }
Reader类常用方法
int read( )
int read(byte[] c)
read(char[] c,int off,int len)
void close( )
子类BufferedReader常用的构造方法
BufferedReader(Reader in)
子类BufferedReader特有的方法
readLine()
使用FileReader读取文件(因为文件字符编码格式不同,很有可能出现乱码)
FileReader fileReader = null; try { fileReader = new FileReader("/Users/allen/Desktop/hello.txt"); StringBuffer stringBuffer = new StringBuffer(); int len = -1; char[] ch = new char[1024]; while((len = fileReader.read(ch)) != -1){ stringBuffer.append(ch); } System.out.println(stringBuffer); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { fileReader.close(); } catch (IOException e) { e.printStackTrace(); } }
使用InputStreamReader读取文件(可以指定字符编码,解决乱码问题)
FileInputStream fileInputStream = null; InputStreamReader inputStreamReader = null; try { fileInputStream = new FileInputStream("/Users/allen/Desktop/hello.txt"); //可以指定编码格式,防止出现乱码 inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); StringBuffer stringBuffer = new StringBuffer(); int len = -1; char[] ch = new char[1024]; while((len = inputStreamReader.read(ch)) != -1){ stringBuffer.append(ch); } System.out.println(stringBuffer); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { inputStreamReader.close(); fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } }
使用 BufferedReader 读文本文件
FileInputStream fileInputStream = null; InputStreamReader inputStreamReader = null; BufferedReader bufferedReader = null; try { fileInputStream = new FileInputStream("/Users/allen/Desktop/hello.txt"); inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); bufferedReader = new BufferedReader(inputStreamReader); String line = null; while((line = bufferedReader.readLine()) != null){ System.out.println(line); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { bufferedReader.close(); inputStreamReader.close(); fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } }
Writer类常用方法
write(String str)
write(String str,int off,int len)
void close()
void flush()
子类BufferedWriter常用的构造方法
BufferedReader(Writer out)
使用FileWriter写文件
Writer writer = null; try { //可以选择true,这样不会覆盖掉原来的内容,只是追加 writer = new FileWriter("/Users/allen/Desktop/hello.txt", true); String info = "这是我要往里面写入的一段话"; writer.write(info); writer.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { writer.close(); } catch (IOException e) { e.printStackTrace(); } }
使用 BufferedWriter 写文件
Writer writer = null; BufferedWriter bufferedWriter = null; try { writer = new FileWriter("/Users/allen/Desktop/hello.txt", true); bufferedWriter = new BufferedWriter(writer); bufferedWriter.write("你好!"); bufferedWriter.newLine();//换行 bufferedWriter.write("我是带缓冲区的输出流"); bufferedWriter.newLine();//换行 bufferedWriter.write("输出完毕!!"); bufferedWriter.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { bufferedWriter.close(); writer.close(); } catch (IOException e) { e.printStackTrace(); } }
读写二进制文件
DataInputStream类
FileInputStream的子类
与FileInputStream类结合使用读取二进制文件
DataOutputStream类
FileOutputStream的子类
与FileOutputStream类结合使用写二进制文件
使用 DataInputStream和DataOutputStream 读写二进制文件
FileInputStream fileInputStream = null; DataInputStream dataInputStream = null; FileOutputStream fileOutputStream = null; DataOutputStream dataOutputStream = null; try { fileInputStream = new FileInputStream("/Users/allen/Desktop/1537193274-diLswpUDbH.jpg"); dataInputStream = new DataInputStream(fileInputStream); fileOutputStream = new FileOutputStream("/Users/allen/Desktop/new/111.jpg"); dataOutputStream = new DataOutputStream(fileOutputStream); int data; while((data = dataInputStream.read()) != -1){ dataOutputStream.write(data); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ try { fileInputStream.close(); dataInputStream.close(); fileOutputStream.close(); dataOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } }
3. 总结
4. 序列化与反序列化
序列化与反序列化的过程
序列化是将对象的状态写入到特定的流中的过程
反序列化则是从特定的流中获取数据重新构建对象的过程
序列化与反序列化之前先将类实现Serializable接口,让类具有序列化的能力。
public class People implements Serializable { private String name; private Integer age; }
如果有个别隐私敏感字段,可以在字段前面加上transient屏蔽字段,这样字段就不会序列化。
public class People implements Serializable { private String name; private transient Integer age; }
使用ObjectOutputStream序列化
People people = new People("张三", 18); FileOutputStream fileOutputStream = null; ObjectOutputStream objectOutputStream = null; try { fileOutputStream = new FileOutputStream("/Users/allen/Desktop/people.txt"); objectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(people); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ try { fileOutputStream.close(); objectOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } }
使用ObjectInputStream反序列化
FileInputStream fileInputStream = null; ObjectInputStream objectInputStream = null; try { fileInputStream = new FileInputStream("/Users/allen/Desktop/people.txt"); objectInputStream = new ObjectInputStream(fileInputStream); People people = (People) objectInputStream.readObject(); System.out.println(people); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { try { fileInputStream.close(); objectInputStream.close(); } catch (IOException e) { e.printStackTrace(); } }