JAVA IO 快速入门
一、输入流区别
字节输入流读取的是字节,字符输入流底层每次读取的是两个字节然后通过解码转换成字符。
二、输出流区别
字节输出流是直接将字节写入到硬盘文件,字符输出流是先将字符写到内存缓冲区,内存缓冲区会对照对应的码表将字符解码成字节,然后调用flush或者close将这些字节写入到硬盘文件。
三、应用区别
字节流可以用于文本、视频、音频、图片各种类型文件的读写操作,字符流一般只适用于读写文本文件最好是中文文本,如果用字符流读写非文本文件容易出现丢数据的问题,比如用字符流来读写图片,读的时候每次是需要拿两个字节去找码表对应的字符,如果没有找到就会标记为未知字符,然后写的时候会将未知字符舍弃,这样就会导致图片文件损坏。
四、处理流(用来包装节点流)
是对一个已存在的流的连接和封装,通过所封装的流的功能调用实现数据读写。如BufferedReader.处理流的构造方法总是要带一个其他的流对象做参数。一个流对象经过其他流的多次包装,称为流的链接。
package com.example.demo;
import java.io.File;
import java.io.IOException;
public class test {
public static void main(String[]args) throws IOException {
//第一种写法;
String fileName = "news.txt";
String parentPath ="d:\\";
File file = new File(parentPath,fileName);
try{
file.createNewFile();
System.out.println("创建成功");
}catch(Exception e){
e.printStackTrace();
}
//第二种写法
File files = new File("d:\\","news1.txt");
try {
files.createNewFile();
System.out.println("创建成功");
}catch (Exception e){e.printStackTrace();}
//第三种写法
File filess = new File("D:\\news2.txt");
try {
filess.createNewFile();
System.out.println("成功");
}catch (Exception e){
e.printStackTrace();
}
}
}
FileInputStream 操作(访问文件)源
package com.example.demo;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class test {
public static void main(String[]args) throws IOException {
String filePath = "D:\\hello.txt";
int readData = 0;
FileInputStream fileInputStream = null;
try {
//创建 FileInputStream 对象,用于读取文件
fileInputStream = new FileInputStream(filePath);
//如果返回-1,表示读取完毕
while ((readData = fileInputStream.read()) != -1){
//转成char显示
System.out.print((char)readData);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭文件流,释放资源
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void readFile02(){
String filePath = "D:\\hello.txt";
int readlen = 0;
//字节数组,一次读取8个字节
byte[] buf = new byte[8];
FileInputStream fileInputStream = null;
try {
//创建 FileInputStream 对象,用于读取文件
fileInputStream = new FileInputStream(filePath);
//如果返回-1,表示读取完毕
//如果读取正常,返回实际读取的字节数
while ((readlen = fileInputStream.read(buf)) != -1){
//转成字符串
System.out.print(new String(buf,0,readlen));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭文件流,释放资源
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
File file = new File("D:\\news3.txt");
file.createNewFile();
System.out.println("文件名称="+file.getName());
System.out.println("文件父级目录 ="+file.getParent());
System.out.println("文件大小 ="+file.length());
System.out.println("是不是一个文件 ="+file.isFile());
System.out.println("是不是一个目录 ="+file.isDirectory());
FileOutputStream 操作(写)
@org.junit.jupiter.api.Test
public void Test2(){
String filePath = "D:\\hello.txt";
FileOutputStream writer = null;
try {
//得到对象
writer = new FileOutputStream(filePath,true);
//写入字节
String tr = "Hello wukelan";
//getbytes 可以把字符串转换成字节数组
// writer.write(tr.getBytes());
//第二种写法
writer.write(tr.getBytes(),0,tr.length());
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭文件流,释放资源
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
字符流 FileReader 操作(读)
@org.junit.jupiter.api.Test
public void Test1(){
String filePath = "D:\\hello.txt";
int readData = 0;
//字符流要用字符数组
char[]arsr = new char[100];
FileReader fileInputStream = null;
try {
//创建 FileInputStream 对象,用于读取文件
fileInputStream = new FileReader(filePath);
//如果返回-1,表示读取完毕
while ((readData = fileInputStream.read(arsr))!=-1){
//转成char显示
System.out.print(new String(arsr,0,readData));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭文件流,释放资源
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
字符流 FileWrite 操作(写)
public class TestThree {
public static void main(String[] args) throws IOException {
String file ="D:\\hello.txt";
FileWriter writer = new FileWriter(file);
writer.write("13");
writer.close();
}
}
字符处理流 BuffReader 操作(读)
@Test
public void red() throws IOException{
String filePaths = "d:\\hello.txt";
String line;
BufferedReader reader = new BufferedReader(new FileReader(filePaths));
//bufferedReader.readLine()按行读取
while ((line = reader.readLine()) != null){
System.out.println(line);
}
reader.close();
}}
字符处理流BuffWriter 操作(写)
public class TestThree {
public static void main(String[] args) throws IOException {
String filePath = "d:\\hello.txt";
//如果想要追加,则在创建节点流处设置为true
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath,true));
bufferedWriter.write("hello");
bufferedWriter.newLine();//换行
bufferedWriter.write("hi");
bufferedWriter.close();
}
}
BufferedInputStream、BufferedOutputStream 文件拷贝
package com.example.demo;
import java.io.*;
public class TestFive {
public static void main(String[] args) {
String files = "d:\\hello.txt";
String filed = "d:\\hellow.txt";
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1、创建源文件、目标文件
File srcFile = new File(files);
File destFile = new File(filed);
srcFile.createNewFile();
destFile.createNewFile();
//2、创建输入节点流、输出节点流
FileInputStream fis = new FileInputStream(files);
FileOutputStream write = new FileOutputStream(files);
FileOutputStream fos = new FileOutputStream(filed);
String tr = "Hello wukelan";
write.write(tr.getBytes());
//3、创建输入缓冲流、输出缓冲流
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//4、将文件读入到内存然后写出到目的地址
byte[] buffer = new byte[10];
int len;
while ((len=bis.read(buffer))!=-1){
bos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//5、关闭流(关闭流的顺序是先关闭外层再关闭内层流,但是关闭外层流的同时内层流会自动关闭,所以只需要关闭外层流即可)
// bos.close();
// bis.close();
if (bos!=null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis!=null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// fos.close();
// fis.close();
}
}
}
JAVA中的对象流
一.对象流的基本介绍
对象流:
对象流是一种高级流,可以方便我们将java中的任何对象进行读写操作。
java.io.objectoutputstream
对象输出流,可以将对象转换为一组字节写出。
java.io.objectinputstream
对象输入流,可以读取一组字节转换为原对象,还原为原对象的条件是读取这个字节应该是对象输出流将一个对象转换的字节。
二.对象输出流(写)
1.ObjectOutputStream 提供了writeObject(object obj);oos将对象转换为字节后 将这组字节交给fos写入文本等于写到硬盘, 这个过程称之为“数据持久化”; oos将给定的对象pdd转为一组字节,这个过程称之为“对象序列化”;简单来讲“对象序列化”就是将对象写入文本中,称之为对象"序列化".
2.serializable接口:
objectOutputstream在对象进行序列化时有个要求:需要序列化对象所属的类。必须是实现serializable接口,实现该接口不需要重写任何方法,目的是可序化的一种标识。
3.transient属性:
把不需要序列化的属性忽略掉,这样可以达到对象文件的“减肥目的”.
一.自定义一个Student类实现serializable接口,
package com.example.demo.target;
import java.io.Serializable;
public class Student implements Serializable {
/**
* serialVersionUID作用:序列化时是为了保持版本
* 的兼容性,意思是在版本升级时反序列化仍保持对象的唯一性
*
* 他又以下两种方式:
* 一个是默认1L,比如:
* private static final long serialVersionUID = 1L;
* 另一个是来自一个64位的哈希段
* private static final long serialVersionUID = 336644671910956990L;
*/
private int id;
private String name;
private String school;
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 String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", school='" + school + '\'' +
'}';
}
}
二.将对象写入“hello.dat”文本中。
package com.example.demo.target;
import java.io.*;
public class Sender {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Student student = new Student();
student.setId(15);
student.setName("王菲");
student.setSchool("清华大学");
System.out.println(student.toString());
//文件输出流
FileOutputStream fos=new FileOutputStream("hello.txt");
//对象输出流
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(student);
System.out.println("写入完毕!!");
oos.close();
}
}
三.对象输入流(读)
1.读取一组字节转换为原对象,称为对象的“反序列化”.
2.ObjectInputStream提供了一个方法是readObject()返回一个object对象.
package com.example.demo.target;
import java.io.*;
public class Recived {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//文件输入流
FileInputStream fis=new FileInputStream("hello.txt");
//对象输入流
ObjectInputStream ois=new ObjectInputStream(fis);
Student stu=(Student) ois.readObject();//相等于object obj=ois.readObject();
System.out.println(stu);
ois.close();
}
}
// 文件删除,删除空目录
String path = "d:\\test";
File file = new File(path);
if(file.exists()) {
file.delete();
System.out.println("文件夹删除成功");
}else {
System.out.println("文件夹不存在");
}
//创建单级目录
String paths = "d:\\test";
File twofile = new File(paths);
if(twofile.exists()) {
System.out.println("文件夹已存在");
}else {
if(twofile.mkdir()) {
System.out.println("文件夹创建成功");
}else {
System.out.println("文件夹创建失败");
}
}
//创建多级目录
String threepath = "f:\\test01\\me";
File three = new File(path);
if(three.exists()) {
System.out.println("文件夹已存在");
}else {
if(three.mkdirs()) {
System.out.println("文件夹创建成功");
}else {
System.out.println("文件夹创建失败");
}
}