2020最新版 Java基础-IO框架 输入流:将<存储设备>中的内容读入到<内存>中。 输出流:将<内存>中的内容写入到<存储设备>中。 节点流:具有实际传输数据的读写功能。 过滤流:在节点流的基础之上增强功能。
所有在java.io中的类都将相对路径名解释为以用户工作目录开始,通过下面代码来获取这个信息
System
.out
.println(System
.getProperty("user.dir"));
字节流:以字节为单位的流 字节流的父类 InputStream:字节输入流 OutputStream:字节输出流
通过FileInputStream读取文件
static void readone() throws IOException
{
FileInputStream file
= new FileInputStream("src\\main\\resources\\application.xml");
System
.out
.println("file = " + file
);
int date
= 0 ;
while ((date
=file
.read())!=-1){
System
.out
.print( (char)date
);
}
file
.close();
}
FileInputStream自定义缓冲数组
static void readmore() throws IOException
{
FileInputStream file
= new FileInputStream("src\\main\\resources\\asd.txt");
System
.out
.println("file = " + file
);
byte[] buf
= new byte[1024] ;
int count
= 0 ;
while ((count
=file
.read(buf
))!=-1){
System
.out
.print(new String(buf
,0,count
));
}
file
.close();
}
使用BufferedInputStream读取数据
static void readbuffer() throws IOException
{
FileInputStream file
= new FileInputStream("src\\main\\resources\\asd.txt");
BufferedInputStream bufferedInputStream
= new BufferedInputStream(file
);
int date
= 0 ;
while ((date
=bufferedInputStream
.read())!=-1){
System
.out
.print( (char)date
);
}
bufferedInputStream
.close();
}
数据写入
static void wirte() throws IOException
{
FileOutputStream fileOutputStream
= new FileOutputStream("src\\\\main\\\\resources\\\\asd.txt",true);
String world
= "hello world" ;
fileOutputStream
.write(world
.getBytes());
fileOutputStream
.close();
}
文件复制
static void filecopy() throws IOException
{
FileInputStream fileInputStream
= new FileInputStream("C:\\Users\\xiaocai\\Pictures\\sadw20200427121533.png");
FileOutputStream fileOutputStream
= new FileOutputStream("src\\main\\resources\\daw.png");
byte[] bytes
= new byte[1024];
int count
= 0 ;
while ((count
= fileInputStream
.read(bytes
))!=-1){
fileOutputStream
.write(bytes
,0,count
);
}
fileInputStream
.close();
fileOutputStream
.close();
}
序列化
public class Student implements Serializable {
private static final long serialVersionUID
= -2961257442669125983L
;
String name
;
transient int age
;
}
static void outobject() throws IOException
{
FileOutputStream fileOutputStream
= new FileOutputStream("src\\main\\resources\\aaa.bin");
ObjectOutputStream objectOutputStream
= new ObjectOutputStream(fileOutputStream
);
Student zy
= new Student("zy", 20);
objectOutputStream
.writeObject(zy
);
objectOutputStream
.close();
}
反序列化
static void Inobject() throws IOException
, ClassNotFoundException
{
FileInputStream fileInputStream
= new FileInputStream("src\\main\\resources\\aaa.bin");
ObjectInputStream objectInputStream
= new ObjectInputStream(fileInputStream
);
Object o
= objectInputStream
.readObject();
System
.out
.println("o = " + o
);
objectInputStream
.close();
}
文件操作
File file
= new File("qqqq.txt");
System
.out
.println(file
.toString());
if (!file
.exists()){
boolean newFile
= file
.createNewFile();
System
.out
.println("newFile = " + newFile
);
}
boolean delete
= file
.delete();
System
.out
.println("delete = " + delete
);
File dir
= new File("aaa\\bbb\\ccc");
if (!dir
.exists()){
dir
.mkdirs() ;
System
.out
.println("create sucess");
}
boolean deletedir
= dir
.delete();
System
.out
.println("deletedir = " + deletedir
);
File dirlist
= new File("C:\\");
String
[] list
= dirlist
.list();
for (String name
: list
) {
System
.out
.println("name = " + name
);
}
File filefilter
= new File("C:\\Users\\xiaocai\\Pictures");
File
[] files
= filefilter
.listFiles(new FileFilter() {
public boolean accept(File pathname
) {
if (pathname
.getName().endsWith(".jpg"))
return true;
else return false;
}
});
for (File name
: files
) {
System
.out
.println(name
);
}
static void listDir(File dir
){
File
[] files
= dir
.listFiles();
if (files
!=null
&& files
.length
>0){
for (File f
: files
) {
if (f
.isDirectory()) listDir(f
);
else System
.out
.println(f
);
}
}
}
static void deleteDir(File dir
){
File
[] files
= dir
.listFiles();
if (files
!=null
&& files
.length
>0){
for (File f
: files
) {
if (f
.isDirectory()) deleteDir(f
);
else System
.out
.println(f
.delete());
}
}
dir
.delete() ;
}