IO流
一、File类
1.File的含义和位置
File类对象代表一个文件或者一个文件夹;File类存放在io包下。
2.获取File类的三种方式
public static void test1()
{
File file1 = new File("first.txt");
System.out.println(file1);
File file2 = new File("D:" + File.separator + "myproject" + File.separator + "my-javabase-demo" + File.separator
+ "javase-demo" + File.separator + " first.txt");
System.out.println(file2);
File file3 = new File("D:\\myproject\\my-javabase-demo", "javase-demo");
System.out.println(file3);
File file4 = new File(file3, "first.txt");
System.out.println(file4);
}
2.1.File类路径注意事项:
相对路径:相较于某个路径下,指明的路径。 idea @test的单元测试指在同一个module下,main方法里面指同一个工程下; 绝对路径:包含盘符在内的文件或文件目录的路径
3.File常见功能
目录结构
3.1.File类的获取功能
3.1.1.常见的File类获取功能
public static void test2()
{
File file1 = new File("first.txt");
File file2 = new File("D:\\myproject\\my-javabase-demo\\javase-demo\\second.txt");
System.out.println(file1.getAbsolutePath());
System.out.println(file1.getPath());
System.out.println(file1.getParent());
System.out.println(file1.getName());
System.out.println(file1.length());
System.out.println(file1.lastModified());
System.out.println("----------");
System.out.println(file2.getAbsolutePath());
System.out.println(file2.getPath());
System.out.println(file2.getParent());
System.out.println(file2.getName());
System.out.println(file2.length());
System.out.println(file2.lastModified());
}
3.1.2.File对象获取对应的文件夹列表
public static void test3()
{
File file = new File("D:\\myproject\\my-javabase-demo\\javase-demo\\di");
for (String s : Objects.requireNonNull(file.list()))
{
System.out.println(s);
}
for (File listFile : Objects.requireNonNull(file.listFiles()))
{
System.out.println(listFile.getAbsolutePath());
}
}
3.2.File类的重命名功能
public static void test4()
{
File file1 = new File("first.txt");
System.out.println(file1.renameTo(new File("1.txt")));
File file2 = new File("D:\\myproject\\my-javabase-demo\\javase-demo\\second.txt");
System.out.println(file2.renameTo(new File("D:\\myproject\\my-javabase-demo\\javase-demo\\three.txt")));
}
3.3.File类的判断功能
public static void test5()
{
File file1 = new File("first.txt");
File file2 = new File("D:\\myproject\\my-javabase-demo\\javase-demo\\three.txt");
System.out.println(file2.isDirectory());
System.out.println(file2.isFile());
System.out.println(file2.exists());
System.out.println(file2.canRead());
System.out.println(file2.canWrite());
System.out.println(file2.isHidden());
}
3.4.File类的创建功能
public static void test6()
{
File file1 = new File("di.txt");
File file2 = new File("D:\\myproject\\my-javabase-demo\\javase-demo\\four.txt");
File file3 = new File("di");
File file4 = new File("D:\\myproject\\my-javabase-demo\\javase-demo\\four");
try
{
System.out.println(file1.createNewFile());
System.out.println(file2.createNewFile());
System.out.println(file3.mkdir());
System.out.println(file4.mkdir());
}
catch (IOException e)
{
e.printStackTrace();
}
}
3.5.File类的删除功能
public static void test7()
{
File file1 = new File("first.txt");
System.out.println(file1.delete());
File file2 = new File("D:\\myproject\\my-javabase-demo\\javase-demo\\first.txt");
System.out.println(file2.delete());
File file3 = new File("D:\\myproject\\my-javabase-demo\\javase-demo\\di");
System.out.println(file3.delete());
if (file3.isDirectory())
{
for (File file : file3.listFiles())
{
file.delete();
}
}
System.out.println(file3.delete());
}
4.File类注意事项
当硬盘有一个真实的文件或目录存在时,创建File对象,各个属相会显示赋值; 当硬盘中没有真实的文件或者文件夹对应时,创建对象时,除了指定的目录和路径外,其他的属性取成员变量的默认值。
二、IO流的原理及流的分类
1.Java IO原理
- I/O是Input/Output的缩写, I/O技术是非常实用的技术,用于处理设备之间的数据传输。如读/写文件,网络通讯等。
- Java程序中,对于数据的输入/输出操作以“流(stream)” 的方式进行。
- java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过标准的方法输入或输出数据。
2.流的分类
2.1.根据流向分类
- 输入input:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中。
- 输出output:将程序(内存)数据输出到磁盘、光盘等存储设备中。
2.2.根据数据单位分类
2.3.根据流的角色分类
- 节点流 (直接从数据源或目的地读写数据),直接作用在源文件上面的。
- 处理流 (不直接连接到数据源或目的地,而是“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供更为强大的读写功能。作用在已有的流之上)
3.字符流
3.1.Reader 读入文件数据
public static void test1()
{
File file = new File("javase-demo\\three.txt");
FileReader fileReader = null;
try
{
fileReader = new FileReader(file);
int data = fileReader.read();
while (data != -1)
{
System.out.println((char) data);
data = fileReader.read();
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (fileReader != null)
{
fileReader.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public static void test2()
{
FileReader fileReader = null;
try
{
File file = new File("javase-demo\\three.txt");
fileReader = new FileReader(file);
char[] cArr = new char[5];
int len;
while ((len = fileReader.read(cArr)) != -1)
{
System.out.print(new String(cArr, 0, len));
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (fileReader != null)
{
try
{
fileReader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
3.2.Writer 写入数据到文件
public static void test3()
{
FileWriter fileWriter = null;
try
{
File file = new File("javase-demo\\one.txt");
fileWriter = new FileWriter(file, true);
fileWriter.write("小白,你好\n");
fileWriter.write("小白,加油\n");
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (fileWriter != null)
{
fileWriter.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public static void test4()
{
FileReader fileReader = null;
FileWriter fileWriter = null;
try
{
File file1 = new File("javase-demo\\one.txt");
File file2 = new File("javase-demo\\two.txt");
fileReader = new FileReader(file1);
fileWriter = new FileWriter(file2);
char[] cArr = new char[5];
int len;
while ((len = fileReader.read(cArr)) != -1)
{
fileWriter.write(cArr, 0, len);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (fileReader != null)
{
fileReader.close();
}
if (fileWriter != null)
{
fileWriter.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
3.3.注意事项
- 字符流无法处理图片之类格式的文件
- 对于文本文件(.txt,.java,.c,.cpp …),使用字符流处理
- 对于非文本文件(.jpg,.mp3,.doc,.ppt,.xls),使用字节流处理
- 使用字节流处理文本文件,如果遇到中文等,可能出现乱码。
- 如果是单纯的复制文件,则字节流和字符流都可以使用。
4.字节流
4.1.使用字节流复制图片文件
public static void test2()
{
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try
{
File fileScr = new File("javase-demo\\img.png");
File fileDest = new File("javase-demo\\img1.png");
inputStream = new FileInputStream(fileScr);
outputStream = new FileOutputStream(fileDest);
int data;
while ((data = inputStream.read()) != -1)
{
outputStream.write(data);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (inputStream != null)
inputStream.close();
if (outputStream != null)
outputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
4.2.文件目录如下
5.缓冲流
5.1.BufferedInputStream && BufferedOutputStream
public static void test1()
{
long start = System.currentTimeMillis();
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try
{
File file1 = new File("javase-demo\\img.png");
File file2 = new File("D:\\img.png");
inputStream = new FileInputStream(file1);
outputStream = new FileOutputStream(file2);
bufferedInputStream = new BufferedInputStream(inputStream);
bufferedOutputStream = new BufferedOutputStream(outputStream);
byte[] bytes = new byte[1024];
int len;
while ((len = bufferedInputStream.read(bytes)) != -1)
{
bufferedOutputStream.write(bytes, 0, len);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (bufferedInputStream != null)
bufferedInputStream.close();
if (bufferedOutputStream != null)
bufferedOutputStream.close();
if (inputStream != null)
inputStream.close();
if (outputStream != null)
outputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
5.2.BufferedReader&& BufferedWriter
public static void test2()
{
long start = System.currentTimeMillis();
BufferedWriter bufferedWriter = null;
BufferedReader bufferedReader = null;
try
{
File file1 = new File("D:\\myproject\\my-javabase-demo\\javase-demo\\text.txt");
File file2 = new File("D:\\text.txt");
FileReader fileReader = new FileReader(file1);
FileWriter fileWriter = new FileWriter(file2);
bufferedReader = new BufferedReader(fileReader);
bufferedWriter = new BufferedWriter(fileWriter);
String data;
while((data = bufferedReader.readLine()) != null){
bufferedWriter.write(data);
bufferedWriter.newLine();
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (bufferedReader != null)
bufferedReader.close();
if (bufferedWriter != null)
bufferedWriter.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
6.转换流(转换流提供了在字节流和字符流之间的转换)
7.其他的流的使用
7.1.标准的输入输出流
public static void test1()
{
BufferedReader bufferedReader = null;
try
{
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
bufferedReader = new BufferedReader(inputStreamReader);
String data;
while (true)
{
System.out.println("输入字符串:");
data = bufferedReader.readLine();
if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data))
{
System.out.println("程序结束");
break;
}
String upperCase = data.toUpperCase();
System.out.println(upperCase);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (bufferedReader != null)
{
bufferedReader.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
8.对象流
8.1.对象流的使用
- ObjectInputStream && OjbectOutputSteam
用于存储和读取基本数据类型数据或对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。 - 序列化:用ObjectOutputStream类保存基本类型数据或对象的机制
- 反序列化:用ObjectInputStream类读取基本类型数据或对象的机制
- ObjectInputStream || OjbectOutputSteam 不能序列化static和transient修饰的成员变量
8.2.对象的序列化
对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点(序列化)。当其它程序获取了这种二进制流,就可以恢复成原来的Java对象(反序列化)。
8.3.使用序列化跟反序列化读取和写入操作
public static void test1()
{
ObjectOutputStream outputStream = null;
try
{
outputStream = new ObjectOutputStream(new FileOutputStream("javase-demo\\object.txt"));
outputStream.writeObject(new String("你好吗,小朋友"));
outputStream.flush();
outputStream.writeObject(new Employee("小白", 1));
outputStream.flush();
outputStream.writeObject(new Employee("小红", 23, 1001, new Account(5000)));
outputStream.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (outputStream != null)
{
outputStream.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public static void test2()
{
ObjectInputStream objectInputStream = null;
try
{
objectInputStream = new ObjectInputStream(new FileInputStream("javase-demo\\object.txt"));
Object obj = objectInputStream.readObject();
String str = (String) obj;
Employee e1 = (Employee) objectInputStream.readObject();
Employee e2 = (Employee) objectInputStream.readObject();
System.out.println(str);
System.out.println(e1);
System.out.println(e2);
}
catch (IOException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
finally
{
try
{
if (objectInputStream != null)
{
objectInputStream.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public class Employee implements Serializable
{
public static final long serialVersionUID = 475461233534532L;
private String name;
private int age;
private int id;
private Account acct;
public Employee(String name, int age, int id)
{
this.name = name;
this.age = age;
this.id = id;
}
public Employee(String name, int age, int id, Account acct)
{
this.name = name;
this.age = age;
this.id = id;
this.acct = acct;
}
@Override
public String toString()
{
return "Employee{" + "name='" + name + '\'' + ", age=" + age + ", id=" + id + ", acct=" + acct + '}';
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
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;
}
public Employee(String name, int age)
{
this.name = name;
this.age = age;
}
public Employee()
{
}
}
public class Account implements Serializable
{
public static final long serialVersionUID = 4713454534532L;
private double balance;
@Override
public String toString()
{
return "Account{" + "balance=" + balance + '}';
}
public double getBalance()
{
return balance;
}
public void setBalance(double balance)
{
this.balance = balance;
}
public Account(double balance)
{
this.balance = balance;
}
}
9.RandomAccessFile
9.1.RandomAccessFile概述
- RandomAccessFile直接继承于java.lang.Object类,实现了DataInput和DataOutput接口
- RandomAccessFile既可以作为一个输入流,又可以作为一个输出流
- 如果RandomAccessFile作为输出流时,写出到的文件如果不存在,则在执行过程中自动创建。
- 可以通过相关的操作,实现RandomAccessFile“插入”数据的效果
9.2.不同构造器
- public RandomAccessFile(File file, String mode)
- public RandomAccessFile(String name, String mode)
创建 RandomAccessFile 类实例需要指定一个 mode 参数,该参数指定 RandomAccessFile 的访问模式: - r: 以只读方式打开
- rw:打开以便读取和写入
- rwd:打开以便读取和写入;同步文件内容的更新
- rws:打开以便读取和写入;同步文件内容和元数据的更新
- 如果模式为只读r。则不会创建文件,而是会去读取一个已经存在的文件,
如果读取的文件不存在则会出现异常。 如果模式为rw读写。如果文件不存在则会去创建文件,如果存在则不会创建。
|