Java文件流主要包括字节流和字符流,字符流本质上就是字节流+编码表,下面通过26个案例介绍了字符流和字节流对文件的操作,包括创建文件路径,文件的读取和写入,文件判断,文件删除,遍历文件,文件复制,文件的编码与解码,文件异常处理以及对象序列化和反序列化等。
目录
24-复制文件的异常处理(try---catch代替throws)
26-序列化中的serialVersionUID和transient
- import java.io.File;
-
- public class FileDemo01 {
- public static void main(String[] args) {
- //File封装路径
- //创建File实例的三种方法
- File file = new File("src\\com\\learn\\java.txt") ;
- System.out.println(file);
-
- File file1 = new File("src\\com\\learn","java.txt") ;
- System.out.println(file1);
-
- File file2 = new File("src\\com\\learn") ;
- File file3 = new File(file2,"java.txt") ;
- System.out.println(file3);
- }
- }
- import java.io.File;
- import java.io.IOException;
-
- public class FileDemo02 {
- public static void main(String[] args) throws IOException {
- //在目录下创建java.txt文件
- File file = new File("New\\src\\com\\learn\\java.txt") ;
- System.out.println(file.createNewFile());
- System.out.println("------------------");
-
- //在目录下创建目录study
- File file1 = new File("New\\src\\com\\learn\\study") ;
- System.out.println(file1.mkdir());
- System.out.println("------------------");
-
- //在目录下创建多级目录study1\Java
- File file2 = new File("New\\src\\com\\learn\\study1\\Java");
- System.out.println(file2.mkdirs());
- System.out.println("------------------");
-
- }
- }
- import java.io.File;
- import java.io.IOException;
-
- public class FileDemo03 {
- public static void main(String[] args) throws IOException {
- File file = new File("New\\src\\com\\learn\\java.txt");
- file.createNewFile() ;
-
- System.out.println(file.isFile()); //是否是文件
- System.out.println(file.isDirectory()); //是否是目录
- System.out.println(file.exists()); //是否存在
-
- System.out.println(file.getAbsolutePath()); //绝对路径
- System.out.println(file.getPath()); //抽象的路径
- System.out.println(file.getName()); //最后的文件或者目录名
-
- //返回文件和目录或者目录的字符串
- File file1 = new File("New\\src") ;
- String [] files = file1.list() ;
- for(String s : files){
- System.out.println(s);
- }
-
-
- //返回文件和目录的File对象
- File [] files1 = file1.listFiles() ;
- for(File file2 : files1){
- System.out.println(file2);
- }
-
- }
- }
-
- import java.io.File;
- import java.io.IOException;
-
- public class FileDemo04 {
- public static void main(String[] args) throws IOException {
- //创建java.txt文件并删除
- File file = new File("New\\src\\com\\learn\\java.txt") ;
- file.createNewFile() ;
- file.delete() ;
- // System.out.println("------------");
-
- //创建study目录并删除
- File file1 = new File("New\\src\\com\\learn\\study") ;
- file1.mkdir() ;
- file1.delete() ;
- // System.out.println("-------------");
-
- //先创建目录,再创建文件,然后先删除文件,后删除目录
- File file2 = new File("New\\src\\com\\learn\\study") ;
- file2.mkdir() ;
- File file3 = new File("New\\src\\com\\learn\\study","java.txt") ;
- file3.createNewFile() ;
- file3.delete() ;
- file2.delete() ;
-
- }
- }
- import java.util.Scanner;
-
- public class FileDemo05 {
- public static void main(String[] args) {
- //递归求阶乘
- Scanner input = new Scanner(System.in) ;
- long n = input.nextLong() ;
- System.out.println(f(n));
- }
- public static long f(long n){
- return n==1 ? 1 : n * f(n-1) ;
- }
- }
-
- import java.io.File;
-
- public class FileDemo06 {
- public static void main(String[] args) {
- //遍历文件,输出绝对路径
- File file = new File("New\\src") ;
- findPath(file) ;
- }
-
- private static void findPath(File file) {
- if(file != null) {
- for (File file1 : file.listFiles()) {
- if (file1.isDirectory()) {
- findPath(file1); //是目录,递归调用
- } else {
- //不是目录,打印绝对路径
- System.out.println(file1.getAbsolutePath());
- }
- }
- }
- }
- }
- import java.io.FileOutputStream;
- import java.io.IOException;
-
- public class FileDemo07 {
- public static void main(String[] args) throws IOException {
- //true表示追加,如果没有true就默认为覆盖
- FileOutputStream fos = new FileOutputStream("New\\src\\com\\learn\\java.txt",true) ;
- fos.write(97); //写单个
- fos.write("\r\n".getBytes());
- fos.write("abcde\r\n".getBytes()); //写一组
- fos.write("xyzw\r\n".getBytes(),1,3); //写指定长度
- fos.close();
-
- }
- }
-
- import java.io.FileInputStream;
- import java.io.IOException;
-
- public class FileDemo08 {
- public static void main(String[] args) throws IOException {
- FileInputStream fis = new FileInputStream("New\\src\\com\\learn\\java.txt") ;
- //读取一次数据
- int by = fis.read() ;
- System.out.println(by);
- System.out.println((char)by);
-
- //读取所有数据
- while((by=fis.read()) != -1){
- System.out.print((char)by);
- }
-
- fis.close();
- }
- }
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
-
- public class FileDemo09 {
- public static void main(String[] args) throws IOException {
- //字节流依次读取文件内容并写入新文件
- FileInputStream fis = new FileInputStream("New\\src\\com\\learn\\java.txt") ;
- FileOutputStream fos = new FileOutputStream("New\\src\\com\\learn\\python.txt") ;
-
- int by ;
- while((by=fis.read()) != -1){
- fos.write((char)by);
- }
- fis.close();
- fos.close();
- }
- }
-
- import java.io.FileInputStream;
- import java.io.IOException;
-
- public class FileDemo10 {
- public static void main(String[] args) throws IOException {
- FileInputStream fis = new FileInputStream("New\\src\\com\\learn\\java.txt") ;
- byte [] bytes = new byte[1024] ;
- int len ;
- while((len=fis.read(bytes)) != -1){
- System.out.println(new String(bytes,0,len));
- }
- fis.close();
-
- }
- }
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
-
- public class FileDemo11 {
- public static void main(String[] args) throws IOException {
- //字节流复制图片
- FileInputStream fis = new FileInputStream("New\\src\\com\\learn\\x.jpg") ;
- FileOutputStream fos = new FileOutputStream("New\\src\\com\\my\\x.jpg") ;
-
- byte [] bytes = new byte[1024] ;
- int len ;
- while((len=fis.read(bytes)) != -1){
- fos.write(bytes,0,len);
- }
- fis.close();
- fos.close();
- }
- }
- import java.io.BufferedInputStream;
- import java.io.FileInputStream;
- import java.io.IOException;
-
- public class FileDemo12 {
- public static void main(String[] args) throws IOException {
- //字节缓冲流
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream("New\\src\\com\\learn\\java.txt")) ;
- byte [] bytes = new byte[1024] ;
- int len ;
- while((len=bis.read(bytes)) != -1){
- System.out.println(new String(bytes,0,len));
- }
- }
- }
- import java.io.UnsupportedEncodingException;
- import java.util.Arrays;
-
- public class FileDemo13 {
- public static void main(String[] args) throws UnsupportedEncodingException {
- String s = "中国" ;
- //编码
- byte [] bytes = s.getBytes("GBK") ;
- byte [] bytes1 = s.getBytes("UTF-8") ;
- System.out.println(Arrays.toString(bytes));
- System.out.println(Arrays.toString(bytes1));
-
- //解码
- String s1 = new String(bytes,"GBK") ;
- String s2 = new String(bytes1, "UTF-8") ;
- System.out.println(s1);
- System.out.println(s2);
-
- }
- }
-
- import java.io.*;
-
- public class FileDemo14 {
- public static void main(String[] args) throws IOException {
- //以GBK编码字符流写入
- OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("New\\src\\com\\learn\\java.txt"),"GBK") ;
- osw.write("中国");
- osw.close();
- //以GBK编码字符流读取
- InputStreamReader osr = new InputStreamReader(new FileInputStream("New\\src\\com\\learn\\java.txt"),"GBK") ;
- int len ;
- while((len=osr.read()) != -1){
- System.out.print((char) len);
- }
-
- }
- }
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStreamWriter;
-
- public class FileDemo15 {
- public static void main(String[] args) throws IOException {
- //字符流写数据的5种方式
- OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("New\\src\\com\\learn\\java.txt")) ;
- //写单个字符
- // osw.write("a");
- // osw.flush();
- // osw.close();
- //写字符数组
- // char [] chars = {'a','b','c','e','d'} ;
- // osw.write(chars);
- // osw.flush();
- // osw.close();
- //写字符数组的一部分
- // char [] chars1 = {'a','c','d','b'} ;
- // osw.write(chars1,1,2);
- // osw.flush();
- // osw.close();
- //写字符串
- // osw.write("abcdefg");
- // osw.flush();
- // osw.close();
- //写字符串的一部分
- osw.write("abcdefg",1,4);
- osw.flush();
- osw.close();
-
- }
- }
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
-
- public class FileDemo16 {
- public static void main(String[] args) throws IOException {
- InputStreamReader isr = new InputStreamReader(new FileInputStream("New\\src\\com\\learn\\java.txt")) ;
- //一次读取一个字符
- int len ;
- while((len=isr.read()) != -1){
- System.out.print((char)len);
- }
- //一次读取一组字符
- char [] bytes = new char[1024] ;
- int by ;
- while((by=isr.read(bytes)) != -1){
- System.out.println(new String(bytes,0,by));
- }
-
- isr.close();
- }
- }
- import java.io.*;
-
- public class FileDemo17 {
- public static void main(String[] args) throws IOException {
- InputStreamReader isr = new InputStreamReader(new FileInputStream("New\\src\\com\\learn\\java.txt")) ;
- OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("New\\src\\com\\my\\copy.txt")) ;
- //
- // //方法1:单个字符依次读写
- // int len ;
- // while ((len=isr.read()) != -1){
- // osw.write(len);
- // }
- // osw.close();
- //方法2:一组字符的读写
- int len ;
- char [] chars = new char[1024] ;
- while ((len = isr.read(chars)) != -1){
- osw.write(chars,0,len);
- }
- osw.close();
- isr.close();
- }
- }
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
-
- public class FileDemo18 {
- public static void main(String[] args) throws IOException {
- FileReader fr = new FileReader("New\\src\\com\\learn\\java.txt") ;
- FileWriter fw = new FileWriter("New\\src\\com\\my\\copy.txt") ;
-
- //一次读写一个字符
- // int len ;
- // while((len=fr.read()) !=-1){
- // fw.write(len);
- // }
- // fw.close();
-
- //一次写一组数据
- int len ;
- char [] chars = new char[1024] ;
- while((len=fr.read(chars)) != -1){
- fw.write(chars,0,len);
- }
- fr.close();
- fw.close();
- }
- }
- import java.io.*;
-
- public class FileDemo19 {
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new FileReader("New\\src\\com\\learn\\java.txt")) ;
- BufferedWriter bw = new BufferedWriter(new FileWriter("New\\src\\com\\my\\copy.txt")) ;
-
- //一次读写一个字符
- // int len ;
- while((len=br.read()) != -1){
- bw.write(len);
- }
- br.close();
- bw.close();
- //一次写一组字符
- int len ;
- char [] chars = new char[1024] ;
- while((len=br.read(chars)) != -1){
- bw.write(chars,0,len);
- }
- bw.close();
- br.close();
- }
- }
- import java.io.*;
-
- public class FileDemo20 {
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new FileReader("New\\src\\com\\learn\\java.txt")) ;
- BufferedWriter bw = new BufferedWriter(new FileWriter("New\\src\\com\\my\\copy.txt")) ;
-
- String line ;
- while ((line=br.readLine()) != null){
- bw.write(line);
- bw.newLine();
- bw.flush();
- }
- bw.close();
- br.close();
-
- }
- }
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.ArrayList;
-
- public class FileDemo21 {
- public static void main(String[] args) throws IOException {
- ArrayList
arrayList = new ArrayList<>() ; - arrayList.add("Java") ;
- arrayList.add("python");
- arrayList.add("c");
-
- BufferedWriter bw = new BufferedWriter(new FileWriter("New\\src\\com\\learn\\java.txt"));
- for(String s : arrayList){
- bw.write(s);
- bw.newLine();
- bw.flush();
- }
- bw.close();
-
- }
- }
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
-
- public class FileDemo22 {
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new FileReader("New\\src\\com\\learn\\java.txt")) ;
- ArrayList
arrayList = new ArrayList<>() ; - String line ;
- while((line = br.readLine()) != null){
- arrayList.add(line);
- }
-
- for(String str : arrayList){
- System.out.println(str);
- }
- }
- }
-
- import java.io.*;
-
- public class FileDemo23 {
- /**
- * 有2个问题需要注意
- * 第一,该方法只能复制单级文件,即目录里不能含有目录,只能含有文件
- * 第二,对于源路径和目标路径一定要具体到文件名,否则会拒绝访问
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- File srcFile = new File("D:\\files") ;
- File destFile = new File("New\\src\\com\\my\\",srcFile.getName()) ;
-
- if(!destFile.exists()){
- destFile.mkdir() ;
- }
-
- for(File files : srcFile.listFiles()){
- String name = files.getName() ;
- File newFile = new File(destFile,name) ;
- copyFiles(files,newFile) ;
- }
- }
-
- private static void copyFiles(File srcFile, File newFile) throws IOException {
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile)) ;
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile)) ;
-
- byte [] bytes = new byte[1024] ;
- int len ;
- while((len=bis.read(bytes)) != -1){
- bos.write(bytes,0,len);
- }
- bis.close();
- bos.close();
-
- }
- }
- import java.io.*;
-
- public class FileDemo24 {
- public static void main(String[] args) throws IOException {
- File srcFile = new File("D:\\file") ;
- File destFile = new File("New\\src\\com\\my") ;
- copyFolder(srcFile,destFile) ;
- }
-
- private static void copyFolder(File srcFile, File destFile) throws IOException {
- //从源目录复制到目标目录
- if(srcFile.isDirectory()){
- File newFile = new File(destFile,srcFile.getName()) ;
- if(!newFile.exists()){
- newFile.mkdir() ;
- }
- for(File file : srcFile.listFiles()){
- copyFolder(file,newFile);
- }
-
- }else{ //是文件,直接复制
- File newFile = new File(destFile,srcFile.getName()) ;
- copyFile1(srcFile,newFile);
- }
- }
-
- private static void copyFile1(File srcFile, File newFile) throws IOException {
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile)) ;
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile)) ;
- int len ;
- byte [] bytes = new byte[1024] ;
- while((len=bis.read(bytes)) != -1){
- bos.write(bytes,0,len);
- }
- bis.close();
- bos.close();
-
- }
- }
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
-
- public class FileDemo25 {
- public static void main(String[] args) {
- method1() ;
- method2() ;
- }
-
- //方法1:直接进行try-catch处理异常
- private static void method1() {
- FileReader fr = null;
- FileWriter fw = null;
- try {
- fr = new FileReader("New\\src\\com\\learn\\java.txt");
- fw = new FileWriter("New\\src\\com\\my\\copy1.txt");
- int len ;
- char [] chars = new char[1024] ;
- while((len=fr.read(chars)) != -1){
- fw.write(chars,0,len);
- }
- }catch (IOException e){
- e.printStackTrace();
- }finally {
- try {
- if (fr != null) {
- fr.close();
- }
- }catch (IOException e){
- e.printStackTrace();
- }
- try {
- if (fw != null) {
- fw.close();
- }
- }catch (IOException e){
- e.printStackTrace();
- }
- }
- }
-
- //改进版本的异常处理
- private static void method2() {
- try(
- FileReader fr = new FileReader("New\\src\\com\\learn\\java.txt");
- FileWriter fw = new FileWriter("New\\src\\com\\my\\copy2.txt");) {
- int len ;
- char [] chars = new char[1024] ;
- while((len=fr.read(chars)) != -1){
- fw.write(chars,0,len);
- }
-
- }catch (IOException e){
- e.printStackTrace();
- }
- }
- }
- import java.io.Serializable;
-
- public class Student implements Serializable {
- private String name ;
- private int age ;
-
- public Student() {
- }
-
- public Student(String name, int age) {
- this.name = name;
- this.age = age;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
- }
- import java.io.*;
-
- public class FileDemo26 {
- public static void main(String[] args) throws IOException, ClassNotFoundException {
- //对象序列化流
- ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("New\\src\\com\\my\\c.txt")) ;
- Student student = new Student("张三", 18) ;
- oos.writeObject(student);
- oos.close();
-
- //对象反序列化流
- ObjectInputStream ois = new ObjectInputStream(new FileInputStream("New\\src\\com\\my\\c.txt")) ;
- Object object = ois.readObject() ;
- Student student1 = (Student) object ;
-
- System.out.println(student1.getName() + "," + student1.getAge());
- }
- }
- import java.io.Serializable;
-
- public class Student implements Serializable {
- //加入序列化后修改类文件,会报错,需要加一个版本号
- private static final long serialVersionUID = 42L ;
- private transient String name ; //给变量加transient关键字,可以不被序列化
- private int age ;
-
- public Student() {
- }
-
- public Student(String name, int age) {
- this.name = name;
- this.age = age;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- @Override
- public String toString() {
- return "Student{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
- }
- import java.io.*;
-
- public class FileDemo26 {
- public static void main(String[] args) throws IOException, ClassNotFoundException {
- //对象序列化流
- ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("New\\src\\com\\my\\c.txt")) ;
- Student student = new Student("张三", 18) ;
- oos.writeObject(student);
- oos.close();
-
- //对象反序列化流
- ObjectInputStream ois = new ObjectInputStream(new FileInputStream("New\\src\\com\\my\\c.txt")) ;
- Object object = ois.readObject() ;
- Student student1 = (Student) object ;
-
- System.out.println(student1.getName() + "," + student1.getAge());
- }
- }