慕课网_文件传输基础——Java IO流

第1章 文件的编码

1-1 文件的编码 (15:07)

第2章 File 类的使用

2-1 File 类常用 API 介绍 (10:50)

import java.io.File;
import java.io.IOException;

public class FileDemo {
	public static void main(String[] args) {
		File file = new File("E:\helloworld\java");
		System.out.println(file.exists());// 判断文件/文件夹是否存在

		if (!file.exists()) {
			file.mkdirs();// 创建目录
		} else {
			file.delete();
		}

		// 是否是一个目录
		System.out.println(file.isDirectory());

		// 是否是一个文件
		System.out.println(file.isFile());

		File file2 = new File("E:\helloworld\1.txt");
		if (file2.exists()) {
			try {
				file2.createNewFile();// 创建文件
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			file2.delete();
		}

		System.out.println(file);// file.toString()的内容
		System.out.println(file.getAbsolutePath());
		System.out.println(file.getName());
		System.out.println(file.getParent());
	}
}

2-2 遍历目录 (10:26)

import java.io.File;
import java.io.IOException;

//列出File的一些常用操作比如过滤、遍历等操作

class FileUtils1 {
	/*
	 * 列出制定目录下(包括其子目录)的所有文件
	 */
	public static void listDirectory(File dir) throws IOException {
		if (!dir.exists()) {
			throw new IllegalArgumentException("目录:" + dir + "不存在");
		}
		if (!dir.isDirectory()) {
			throw new IllegalArgumentException(dir + "不是目录");
		}
		String[] filenames = dir.list();// 返回的是字符串数组 直接子的名称,不包含子目录下的内容
		for (String string : filenames) {
			System.out.println(dir + "\" + string);
		}
	}
}

class FileUtils2 {
	/*
	 * 列出制定目录下(包括其子目录)的所有文件
	 */
	public static void listDirectory(File dir) throws IOException {
		if (!dir.exists()) {
			throw new IllegalArgumentException("目录:" + dir + "不存在");
		}
		if (!dir.isDirectory()) {
			throw new IllegalArgumentException(dir + "不是目录");
		}
		// 如果要遍历子目录下的内容就需要构造成File对象做递归操作,File提供了直接返回File对象的API
		File[] files = dir.listFiles();// 返回的是直接子目录(文件)的对象
		if (files != null && files.length > 0) {
			for (File file : files) {
				if (file.isDirectory()) {
					listDirectory(file);
				} else {
					System.out.println(file);
				}
			}
		}
	}
}

public class FileUtilTest {
	public static void main(String[] args) throws IOException {
		FileUtils2.listDirectory(new File("E:\kms"));
	}
}

第3章 RandomAccessFile类的使用

3-1 RandomAccessFile基本操作 (17:16)

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;

/*
 * java.io.File类用于表示文件(目录)
 * File类只用于表示文件(目录)的信息(名称、大小等),不能用于文件内容的访问
 * 
 * RandomAccessFile java提供的对文件内容的访问,既可以读文件,也可以写文件。
 * RandomAccessFile支持随机访问文件,可以访问文件的任意位置。
 * 
 * (1)java文件模型
 * 在硬盘上的文件是byte byte byte存储的,是数据的集合
 * 
 * (2)打开文件
 * 有两种模式"rw"(读写)"r"(只读)
 * RandomAccessFile raf=new RandomAccessFile(file,"rw");
 * 文件指针,打开文件时指针在开头pointer=0;
 * 
 * (3)写文件
 * raf.write(int);只写一个字节(后8位),同时指针指向下一个位置,准备再次写入
 * 
 * (4)读文件
 * int b=raf.read();读一个字节
 * 
 * (5)文件读写完成以后一定要关闭(Oracle官方说明)
 */

public class RafDemo {
	public static void main(String[] args) throws IOException {
		File demo = new File("demo");
		if (!demo.exists()) {
			demo.mkdirs();
		}

		File file = new File(demo, "raf.dat");
		if (!file.exists()) {
			file.createNewFile();
		}

		RandomAccessFile raf = new RandomAccessFile(file, "rw");

		// 指针的位置
		System.out.println(raf.getFilePointer());
		raf.write('A');// 只写了一个字节
		
		System.out.println(raf.getFilePointer());
		raf.write('B');

		int i = 0x7fffffff;
		// 用write方法每次只能写一个字节,如果要把i写进去就得写4次
		raf.write(i >>> 24);// 高8位
		raf.write(i >>> 16);
		raf.write(i >>> 8);
		raf.write(i);
		System.out.println(raf.getFilePointer());

		// 可以直接写一个int
		raf.writeInt(i);

		String s = "中";
		byte[] gbk = s.getBytes("gbk");
		raf.write(gbk);
		System.out.println(raf.length());

		// 读文件,必须吧指针移到头部
		raf.seek(0);
		// 一次性读取,把文件中的内容都读到字节数组中
		byte[] buf = new byte[(int) raf.length()];
		raf.read(buf);
		System.out.println(Arrays.toString(buf));

		String s1 = new String(buf, "gbk");
		System.out.println(s1);

		for (byte b : buf) {
			System.out.println(Integer.toHexString(b & 0xff) + " ");
		}

		raf.close();
	}
}

第4章 字节流

4-1 字节流之文件输入流FileInputStream-1 (15:09)

import java.io.FileInputStream;
import java.io.IOException;

/*
 * IO流(输入流、输出流)
 * 字节流、字符流
 * 1.字节流
 * 1)InputStream、OutputStream
 * InputStream抽象了应用程序读取数据的方式
 * OutputStream抽象了应用程序写出数据的方式
 * 2)EOF=end 读到-1就读到结尾
 * 3)输入流基本方法
 * int b=in.read();
 * in.read(byte[] buf);
 * in.read(byte[] buf,int start,int size);
 * 4)输出流基本方法
 * out.write(int b);写出一个byte到流,b的低8位
 * out.write(byte[] buf);将buf字节数组都写入到流
 * out.write(byte[] buf,int start,int size);
 * 5)FileInputStream具体实现了在文件上读取数据
 */

public class IOUtil {
	/*
	 * 读取指定文件内容,按照16进制输出到控制台 并且每输出10个byte换行
	 */
	public static void main(String[] args) {
		try {
			IOUtil.printHex("E:\helloworld\1.txt");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void printHex(String fileName) throws IOException {
		FileInputStream in = new FileInputStream(fileName);
		int b;
		int i = 1;
		while ((b = in.read()) != -1) {
			if (b <= 0xf) {
				System.out.print("0");
			}
			System.out.print(Integer.toHexString(b) + " ");
			if (i++ % 10 == 0) {
				System.out.println();
			}
		}
		in.close();
	}
}

4-2 字节流之文件输入流FileInputStream-2 (08:40)

4-3 字节流之文件输出流FileOutputStream (13:24)

4-4 字节流之数据输入输出流 (11:08)

4-5 字节缓冲流 (17:54)

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * BufferedInputStream&BufferedOutputStream
 * 这两个流类位IO提供了带缓冲区的操作,一般打开文件进行写入
 * 或读取操作时,都会加上缓冲,这种流模式提供了IO的性能
 * 从应用程序中把输入放入文件,相当于将一缸水导入到另一个缸水中
 * FileOutputStream->write()方法相当于一滴一滴地把水转移过去
 * DataOutputStream->writeXxx()方法会方便一些,相当于一瓢一瓢把水转移过去
 * BufferedOutputStream->write()方法更方便,相当于一瓢一瓢先放入桶中,再从桶中倒入到另一个缸中,性能提高
 */

public class IOUtil {
	public static void main(String[] args) {

		try {
			long start = System.currentTimeMillis();
			IOUtil.copyFileByBuffer(new File("E:\helloworld\1.txt"), new File("E:\helloworld\2.txt"));
			long end = System.currentTimeMillis();
			System.out.println(end - start);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void copyFileByBuffer(File srcFile, File destFile) throws IOException {
		if (!srcFile.exists()) {
			throw new IllegalArgumentException("文件:" + srcFile + "不存在");
		}
		if (!srcFile.isFile()) {
			throw new IllegalArgumentException(srcFile + "不是文件");
		}
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
		int c;
		while ((c = bis.read()) != -1) {
			bos.write(c);
			bos.flush();// 刷新缓冲区
		}
		bos.close();
		bis.close();
	}
}

第5章 字符流

5-1 字节字符转换流 (18:09)

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/*
 * 字符流
 * 1)编码问题
 * 2)认识文本和文本文件
 * java的文本(char)是16位无符号整数,是字符的Unicode编码(双字节编码)
 * 文件是byte byte byte...的数据序列
 * 文本文件是文本(char)序列按照某种编码方案(utf-8,utf-16be,gbk)序列化位byte的存储结构
 * 3)字符流(Reader Writer)
 * 字符的处理,一次处理一个字符
 * 字符的底层仍然是基本的字节序列
 * 字符流的基本实现
 * InputStreamReader完成byte流解析为char流,按照编码解析
 * OutputStreamWriter提供char流到byte流,按照编码处理
 */

public class IsrAndOswDemo {
	public static void main(String[] args) throws IOException {
		FileInputStream in = new FileInputStream("E:\helloworld\1.txt");
		InputStreamReader isr = new InputStreamReader(in, "utf-8");// 默认项目的编码

		FileOutputStream out = new FileOutputStream("E:\helloworld\2.txt");
		OutputStreamWriter osw = new OutputStreamWriter(out, "utf-8");

		char[] buffer = new char[8 * 1024];
		int c;
		/*
		 * 批量读取,放入buffer这个字符数组,从第0个位置开始放置,最多放入buffer.length个 返回的是读到的字符的个数
		 */
		while ((c = isr.read(buffer, 0, buffer.length)) != -1) {
			String s = new String(buffer, 0, c);
			System.out.print(s);
			osw.write(buffer, 0, c);
			osw.flush();
		}
		osw.close();
		isr.close();
	}
}

5-2 字符流之文件读写流 (05:56)

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FrAndFwDemo {
	public static void main(String[] args) throws IOException {
		FileReader fr = new FileReader("E:\helloworld\1.txt");
		FileWriter fw = new FileWriter("E:\helloworld\2.txt", true);// true追加
		char[] buffer = new char[2048];
		int c;

		while ((c = fr.read(buffer, 0, buffer.length)) != -1) {
			fw.write(buffer, 0, c);
			fw.flush();
		}

		fw.close();
		fr.close();
	}
}

5-3 字符流的过滤器 (10:25)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

/*
 * BufferedReader->readLine一次读一行
 * BufferedWriter/PrintWriter->写一行
 */

public class BrAndBwOrPwDemo {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("E:\helloworld\1.txt")));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("E:\helloworld\2.txt")));
		PrintWriter pw = new PrintWriter("E:\helloworld\3.txt");
		String line;

		while ((line = br.readLine()) != null) {
			System.out.println(line);// 一次读一行,并不能识别换行
			bw.write(line);
			// 单独写出换行操作
			bw.newLine();// 换行操作
			bw.flush();

			pw.println(line);
			pw.flush();
		}

		bw.close();
		pw.close();
		br.close();
	}
}

第6章 对象的序列化和反序列化

6-1 序列化基本操作 (10:30)

6-2 transient及ArrayList源码分析 (12:41)

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/*
 * 对象的序列化,反序列化
 * 1)对象序列化,就是将Object转换成byte序列,反之叫对象的反序列化
 * 
 * 2)序列化流(ObjectOutputStream),是过滤流-writeObject
 * 反序列化(ObjectInputStream)-readObject
 * 
 * 3)序列化接口(Serializable)
 * 对象必须实现序列化接口,才能进行序列化,否则将出现异常
 * 这个接口,没有任何方法,只是一个标准
 * 
 * 4)transient关键字
 * 	private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException
 *  private void readObject(java.io.ObjectInputStream s) throws java.io.IOException,ClassNotFoundException
 *  
 *  java语言的关键字,变量修饰符,如果用transient声明一个实例变量,当对象存储时,它的值不需要维持。换句话来说就是,用transient关键字标记的成员变量不参与序列化过程。
 */

public class ObjectSeriaDemo {
	public static void main(String[] args) throws Exception, IOException {
		// 1.序列化
		String file = "E:\helloworld\1.txt";
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
		Student student1 = new Student("10001", "张三", 20);
		System.out.println(student1);
		oos.writeObject(student1);
		oos.flush();
		oos.close();

		// 2.反序列化
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
		Student student2 = (Student) ois.readObject();
		System.out.println(student2);
		ois.close();
	}
}

class Student implements java.io.Serializable {// 需要序列化的类必须implements java.io.Serializable
	private String stuno;
	private String stuname;
	transient private int stuage;// 该元素不会进行jvm默认的序列化,也可以自己完成这个元素的序列化

	public String getStuno() {
		return stuno;
	}

	@Override
	public String toString() {
		return "Student [stuno=" + stuno + ", stuname=" + stuname + ", stuage=" + stuage + "]";
	}

	public Student(String stuno, String stuname, int stuage) {
		super();
		this.stuno = stuno;
		this.stuname = stuname;
		this.stuage = stuage;
	}

	public Student() {
		super();
	}

	public void setStuno(String stuno) {
		this.stuno = stuno;
	}

	public String getStuname() {
		return stuname;
	}

	public void setStuname(String stuname) {
		this.stuname = stuname;
	}

	public int getStuage() {
		return stuage;
	}

	public void setStuage(int stuage) {
		this.stuage = stuage;
	}

	private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
		s.defaultWriteObject();// 把jvm能默认序列化的元素进行序列化操作
		s.writeInt(stuage);// 自己完成stuage的序列化
	}

	private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
		s.defaultReadObject();// 把jvm能默认反序列化的元素进行反序列化操作
		this.stuage = s.readInt();// 自己完成stuage的反序列化操作
	}
}

6-3 序列化中子父类构造函数问题 (11:43)

原文地址:https://www.cnblogs.com/denggelin/p/7166702.html