我是靠谱客的博主 踏实香菇,这篇文章主要介绍java基础_笔记3(io流,文件)1.1  Io流 1.2   File,现在分享给大家,希望可以做个参考。

1.1  Io流

流的分类

 往内存中去的叫做输入流INPUT

往磁盘中去的叫做输出流ouput

一次读取一个字节的叫字节流

一次读取一个字符的叫字符流

四大家族

InputStream 字节输入流

OuputStream字节输出流

Reader         字符输入流

Writer            字符输出流

以上的类都是抽象类

所有的流开启之后都是可关闭的,存在一个close方法。流相当于一个管道,内存和硬盘之间的通道,用完之后需要关闭

 需要掌握的流

 文件专属:
java.io.FileInputstream java.io.FileOutputstream java.io.FileReader
java.io.FileWriter
转换流:(将字节流转换成字符流)java.io.InputStreamReader java.io.OutputStreamWriter
缓冲流专属:
java.io.BufferedReader
java.io.BufferedWriter
java.io.BufferedInputstream
java.io.Bufferedoutputstream
数据流专属:
java.io.DataInputStream
java.io.DataOutputstream
标准输出流:
java.io.Printwriter
Java.io.PrintStream
对象专属流:
java.io.ObjectInputstream
java.io.ObjectOutputstream

FileInputStream和FileOutPutStream流

 ps:路径使用的是相对路径,流默认的路径是该项目的路径

file.read();方法:如果不输入任何数据,会默认读取一个字节,相当于一个指针,每次都读取一个字节并返回一个int类型的ASCLL。如果没有数据就返回-1.

因此使用byte数组可以一次读取多个字节。提高效率。

file.read(bytes)!=-1代表着一直读取到空为止

file1.available()方法返回文件中剩余的字节数

在输出文件的时候用字符串类型转型

new String(bytes1,偏移量,数目)可以使用这两个参数控制输出

最后一个数目可以使用file.read(bytes)的返回值,返回值刚好就是读取的数目

FileOutputStream file4=new FileOutputStream("base_java/test.txt",true);创建的时候后面那个true表示开启追加功能,不设置默认清空文档重写

然后使用上述查询出来的byte数组使用write直接写入即可。最后关闭两个流即可

如果使用不容的文件路径,就实现了文件的复制。

如果使用的输出流文件不存在的时候会直接创建文件

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
FileInputStream file=new FileInputStream("base_java/test.txt"); //创建文件对象 long begin=System.currentTimeMillis(); // 计算开始时间 byte[] bytes1=new byte[4]; // 创建一个四个字节的byte数组 // 用文件对象调用read方法,使用循环读取文件数据,返回值为-1的时候表明读取完毕 while (file.read(bytes1)!=-1){ System.out.println(new String(bytes1)); } // 创建一个新的对象 FileInputStream file1=new FileInputStream("base_java/test.txt"); // 在创建数组对象的时候调用文件acailable方法,返回剩下的字节数目,这种方法不适合大量数据 byte[] bytes=new byte[file1.available()]; int count=file1.read(bytes); FileOutputStream file4=new FileOutputStream("base_java/test.txt",true); file4.write(bytes1); file4.close(); System.out.println(new String(bytes)); // 关闭文件流 file.close(); file1.close(); long end=System.currentTimeMillis(); System.out.println(end-begin);

简单的应用文件输出流,利用字符串的getBytes()方法将字符串转换

复制代码
1
2
3
4
5
6
public static void main(String[] args) throws IOException { FileOutputStream file=new FileOutputStream("base_java/test.txt",true); byte[] bytes="aaa".getBytes(); file.write(bytes); file.close(); }

FileWriter和FileReader 

文本字符输入和输出流,只能对文本读取或者修改 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void main(String[] args) throws IOException { // 创建对象 FileReader fileReader=new FileReader("base_java/test.txt"); // 创建char数组 char[] chars=new char[4]; int count=0; // 循环输出 while ((count = fileReader.read(chars))!=-1){ System.out.print(new String(chars,0,count)); } fileReader.close(); }

如果是输出流则需要刷新一个方法,可以定义数组存放,也可以直接存放 

复制代码
1
2
3
4
5
6
7
8
public static void main(String[] args) throws IOException { FileWriter fileWriter=new FileWriter("base_java/test1.txt",true); char[] chars = {'我','d','s'}; fileWriter.write(chars); fileWriter.write("cccccc"); fileWriter.flush(); fileWriter.close(); }

BufferedReader和BufferedWriter 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public static void main(String[] args) throws IOException { // 字节流 FileInputStream fileInputStream=new FileInputStream("base_java/test.txt"); // 字符流 FileReader fileReader=new FileReader("base_java/test.txt"); //将字节流转换为字符流的转换流 InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream); //缓冲包装流,构造方法只接受字符流,可以接受inputStreamReader和fileReader BufferedReader bufferedReader=new BufferedReader(fileReader); String line=null; // 调用读取每行方法输出 while ((line=bufferedReader.readLine())!=null){ System.out.println(line); } bufferedReader.close(); }
复制代码
1
2
3
4
5
6
7
8
9
public static void main(String[] args) throws IOException { // 缓冲流同样都是只能接受字符流,同样可以使用转换流 BufferedWriter out=new BufferedWriter(new FileWriter("base_java/test.txt",true)); out.write("n"); out.write("BufferedWriter"); out.flush(); out.close(); }

dataoutputstream和dataintputstream

  只能用这个数据输出流写的文件才可以用输入流读取,并且读的时候必须和写的顺序一致才可以取出数据,并且只接受基本数据类型。

利用标准输出流创建日志类

 创建一个类,new标准输出流的对象,利用system.setout改变输出语句的输出位置,达到日志记录的效果

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Logger { public static void log(String msg){ PrintStream out= null; try { out = new PrintStream(new FileOutputStream("base_java/log.txt"),true); } catch (FileNotFoundException e) { e.printStackTrace(); } System.setOut(out); Date notime=new Date(); SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS"); String date=format.format(notime); System.out.println(date+":"+msg); } }

 1.2   File

file对象可以是一个目录也可以是一个文件,是一个路径名的抽象表现形式 、

常用方法

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public static void main(String[] args) throws IOException { File file=new File("base_java/filea"); // 判断是否存在,如果不存在就新建文件形式file.createNewFile(); // 判断是否存在,如果不存在就新建目录形式file.mkdir(); if (!file.exists()){ // file.createNewFile(); file.mkdir(); } String parentpath=file.getParent(); System.out.println("父路径"+parentpath); String absolutionpath=file.getAbsolutePath(); System.out.println("获取绝对路径:"+absolutionpath); System.out.println("判断是否是目录"+file.isDirectory()); System.out.println("判断是否是文件"+file.isFile()); long time=file.lastModified();//获取最后修改时间,是从1970到最后一次修改的总毫秒数 Date date=new Date(time); SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); String nowtime =format.format(date); System.out.println("最后一次修改时间"+nowtime); System.out.println("获取文件大小"+file.length()); File file1=new File("base_java/file/aa/vv"); // 判断是否存在,如果不存在就新建多级目录形式形式file.createNewFile(); if (!file1.exists()){ // file.createNewFile(); file1.mkdirs(); } }

 文件的拷贝

从一个地方拷贝到另一个地方(较复杂)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public static void main(String[] args) throws IOException { File srcFile=new File("C:\Users\86176\Desktop\019301700020柳俊\Android"); File destFile=new File("C:\test"); cope(srcFile,destFile); } public static void cope(File srcFile, File destFile) throws IOException { if (srcFile.isFile()){ FileInputStream in=new FileInputStream(srcFile); FileOutputStream out=new FileOutputStream((destFile.getAbsolutePath().endsWith("\") ? destFile.getAbsolutePath():destFile.getAbsolutePath()+"\")+srcFile.getAbsolutePath().substring(3)); byte[] bytes=new byte[1024*1024]; int count=0; while ((count=in.read(bytes))!=-1){ out.write(bytes,0,count); } out.flush(); if (out!=null){out.close();} return; } File[] files=srcFile.listFiles(); for (File file:files){ if (file.isDirectory()){ String srcdir=file.getAbsolutePath(); String destdir=(destFile.getAbsolutePath().endsWith("\")?destFile.getAbsolutePath():destFile.getAbsolutePath()+"\")+srcdir.substring(3); File newfile1=new File(destdir); if(!newfile1.exists()){ newfile1.mkdirs(); } } cope(file,destFile); } }

 序列化与反序列化

 //序列化:将对象从内存中存放到磁盘里

//反序列化:将磁盘里存放的对象读取到内存中

//序列化需要类本身实现Serializable接口

//这个接口起到一个标识符的作用,让jvm给该类自动生成一个序列化版本号

java中对类的区分:

首先对类名区分,如果不一样肯定不是一个类

其次就是使用序列号来区分类,每个类都有单独的序列号,

类实现了Serializable接口之后会自动生成一个版本号,这样就会自动区分类

但是这样有一个坏处,一但修改之后重新编译,jvm就会认为该类是一个全新的类

在读取文件中的java对象的时候就会出现序列号不一致的错误

所以必须要指定序列号

将类保存到文件

复制代码
1
2
3
4
5
6
7
public static void main(String[] args) throws IOException { student student=new student("zhangsan",22); ObjectOutputStream obj=new ObjectOutputStream(new FileOutputStream("base_java/student")); obj.writeObject(student); obj.flush(); obj.close(); }

 将文件中的类输出

复制代码
1
2
3
4
5
6
public static void main(String[] args) throws IOException, ClassNotFoundException { ObjectInputStream obj=new ObjectInputStream(new FileInputStream("base_java/student")); Object object=obj.readObject(); System.out.println(object); obj.close(); }

这种方式不可以一次一次的加对象,如果需要存放多个对象需要使用list

复制代码
1
2
3
4
5
6
7
8
9
List<student> list=new ArrayList<>(); list.add(new student("587587",23)); list.add(new student("587",22)); list.add(new student("5758",42)); list.add(new student("4575",43)); ObjectOutputStream obj=new ObjectOutputStream(new FileOutputStream("base_java/student")); obj.writeObject(list); obj.flush(); obj.close();

io和Properties读取文件

设计理念:以后经常改变的数据,可以单独写一个文件,使用程序动态读取。将来只需要修改这个文件的内容,java代码不用改动,不需要重新编译。 

properties集合有一个方法可以调用一个文件字符流来读取文件中的数据到改map集合

然后使用自带的get方法可以取出数据 

复制代码
1
2
3
4
5
6
public static void main(String[] args) throws IOException { FileReader fileReader=new FileReader("base_java/student"); Properties properties=new Properties(); properties.load(fileReader); System.out.println(properties.getProperty("username")); }

 student文件

复制代码
1
2
username="zhangsan" password="1234"

一般配置文件就以properties结尾

最后

以上就是踏实香菇最近收集整理的关于java基础_笔记3(io流,文件)1.1  Io流 1.2   File的全部内容,更多相关java基础_笔记3(io流,文件)1.1 内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部