Java基础知识之IO流

b

c

d

e

  • 回车符\r和换行符\n
*   回车符:回到一行的开头(return)。
*   换行符:下一行(newline)。
  • 系统中的换行:
*   Windows系统里,每行结尾是 `回车+换行` ,即`\r\n`;
*   Unix系统里,每行结尾只有 `换行` ,即`\n`;
*   Mac系统里,每行结尾是 `回车` ,即`\r`。从 Mac OS X开始与Linux统一。

3.字节输入流[InputStream]

java.io.InputStream抽象类是表示字节输入流的所有类的超类,可以读取字节信息到内存中。它定义了字节输入流的基本共性功能方法。

  • public void close() :关闭此输入流并释放与此流相关联的任何系统资源。

  • public abstract int read(): 从输入流读取数据的下一个字节。

  • public int read(byte[] b): 从输入流中读取一些字节数,并将它们存储到字节数组 b中 。

小贴士:

close方法,当完成流的操作时,必须调用此方法,释放系统资源。

4. FileInputStream类

java.io.FileInputStream类是文件输入流,从文件中读取字节。

(1)构造方法
  • FileInputStream(File file): 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名。

  • FileInputStream(String name): 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。

当你创建一个流对象时,必须传入一个文件路径。该路径下,如果没有该文件,会抛出FileNotFoundException

  • 构造举例,代码如下:

public class FileInputStreamConstructor throws IOException{

public static void main(String[] args) {

// 使用File对象创建流对象

File file = new File(“a.txt”);

FileInputStream fos = new FileInputStream(file);

// 使用文件名称创建流对象

FileInputStream fos = new FileInputStream(“b.txt”);

}

}

(2)读取字节数据
  1. 读取字节read方法,每次可以读取一个字节的数据,提升为int类型,读取到文件末尾,返回-1,代码使用演示:

public class FISRead {

public static void main(String[] args) throws IOException{

// 使用文件名称创建流对象

FileInputStream fis = new FileInputStream(“read.txt”);

// 读取数据,返回一个字节

int read = fis.read();

System.out.println((char) read);

read = fis.read();

System.out.println((char) read);

read = fis.read();

System.out.println((char) read);

read = fis.read();

System.out.println((char) read);

read = fis.read();

System.out.println((char) read);

// 读取到末尾,返回-1

read = fis.read();

System.out.println( read);

// 关闭资源

fis.close();

}

}

输出结果:

a

b

c

d

e

-1

循环改进读取方式,代码使用演示:

public class mainIo1 {

public static void main(String[] args) throws IOException {

FileInputStream fis=new FileInputStream(“a.txt”);

int read=0;

while((read=fis.read())!=-1) {

System.out.println((char)read);

}

fis.close();

}

}

小贴士:

  1. 虽然读取了一个字节,但是会自动提升为int类型。

  2. 流操作完毕后,必须释放系统资源,调用close方法,千万记得。

  3. 使用字节数组读取read(byte[] b),每次读取b的长度个字节到数组中,返回读取到的有效字节个数,读取到末尾时,返回-1 ,代码使用演示:

public class FISRead {

public static void main(String[] args) throws IOException{

// 使用文件名称创建流对象.

FileInputStream fis = new FileInputStream(“read.txt”); // 文件中为abcde

// 定义变量,作为有效个数

int len ;

// 定义字节数组,作为装字节数据的容器

byte[] b = new byte[2];

// 循环读取

while (( len= fis.read(b))!=-1) {

// 每次读取后,把数组变成字符串打印

System.out.println(new String(b));

}

// 关闭资源

fis.close();

}

}

输出结果:

ab

cd

ed

错误数据d,是由于最后一次读取时,只读取一个字节e,数组中,上次读取的数据没有被完全替换,所以要通过len ,获取有效的字节,代码使用演示:

public class FISRead {

public static void main(String[] args) throws IOException{

// 使用文件名称创建流对象.

FileInputStream fis = new FileInputStream(“read.txt”); // 文件中为abcde

// 定义变量,作为有效个数

int len ;

// 定义字节数组,作为装字节数据的容器

byte[] b = new byte[2];

// 循环读取

while (( len= fis.read(b))!=-1) {

// 每次读取后,把数组的有效字节部分,变成字符串打印

System.out.println(new String(b,0,len));// len 每次读取的有效字节个数

}

// 关闭资源

fis.close();

}

}

输出结果:

ab

cd

e

小贴士:

使用数组读取,每次读取多个字节,减少了系统间的IO操作次数,从而提高了读写的效率,建议开发中使用。

5.字节流练习:图片复制

public class mainIo1 {

public static void main(String[] args) throws IOException {

FileInputStream fis = new FileInputStream(“D:\image\img1.png”);

FileOutputStream fos = new FileOutputStream(“D:\image\img2.png”);

byte[] b = new byte[1024];

int len = 0;

while ((len = fis.read(b)) != -1) {

fos.write(b, 0, len);

}

fos.close();

fis.close();

}

}

小贴士:

流的关闭原则:先开后关,后开先关。

三.字符流


当使用字节流读取文本文件时,可能会有一个小问题。就是遇到中文字符时,可能不会显示完整的字符,那是因为一个中文字符可能占用多个字节存储。所以Java提供一些字符流类,以字符为单位读写数据,专门用于处理文本文件。

1.字符输入流[Reader]

java.io.Reader抽象类是表示用于读取字符流的所有类的超类,可以读取字符信息到内存中。它定义了字符输入流的基本共性功能方法。

  • public void close():关闭此流并释放与此流相关联的任何系统资源。

  • public int read():从输入流读取一个字符。

  • public int read(char[] cbuf): 从输入流中读取一些字符,并将它们存储到字符数组 cbuf中 。

2.FileReader类

java.io.FileReader类是读取字符文件的便利类。构造时使用系统默认的字符编码和默认字节缓冲区。

小贴士:

  1. 字符编码:字节与字符的对应规则。Windows系统的中文编码默认是GBK编码表。
idea中UTF-8
  1. 字节缓冲区:一个字节数组,用来临时存储字节数据。
(1)构造方法
  • FileReader(File file):创建一个新的FileReader,给定要读取的File对象。

  • FileReader(String fileName):创建一个新的FileReader,给定要读取的文件的名称。

当年创建一个流对象时,必须传入一个文件路径。类似于FileInputStream.

  • 构造举例,代码如下:

public class mainIo1 {

public static void main(String[] args) throws FileNotFoundException {

// 使用File对象创建流对象

File file = new File(“a.txt”);

FileReader fr1 = new FileReader(file);

// 使用文件名称创建流对象

FileReader fr2 = new FileReader(“b.txt”);

}

}

(2)读取字符数据
  1. 读取字符read方法,每次可以读取一个字符的数据,提升为int类型,读取到文件末尾,返回-1,循环读取,代码如下:

public class mainIo1 {

public static void main(String[] args) throws IOException {

FileReader fr = new FileReader(“a.txt”);

int b;

while ((b = fr.read()) != -1) {

System.out.println((char) b);

}

fr.close();

}

}

小贴士:虽然读取了一个字符,但是会自动提升为int类型。

  1. 使用字符数组读取read(char[] cbuf),每次读取b的长度个字符到数组中,返回读取到的有效个字符个数,读取到末尾时,返回-1,代码如下:

public class mainIo1 {

public static void main(String[] args) throws IOException {

FileReader fr = new FileReader(“a.txt”);

char[] c = new char[2];

int len;

while ((len = fr.read©) != -1) {

System.out.println(new String©);

}

fr.close();

}

}

获取有效的字符改进,代码如下:

public class mainIo1 {

public static void main(String[] args) throws IOException {

FileReader fr = new FileReader(“a.txt”);

char[] c = new char[2];

int len;

while ((len = fr.read©) != -1) {

System.out.println(new String(c,0,len));

}

fr.close();

}

}

3.字符输出流[Writer]

java.io.Writer抽象类是表示用于写出字符流的所有类的超类,将指定的字符信息写出到目的地。它定义了字符输出流的基本共性功能方法。

  • void write(int c) 写入单个字符。

  • void write(char[] cbuf)写入字符数组。

  • abstract void write(char[] cbuf, int off, int len)写入字符数组的某一部分,off数组的开始索引,len写的字符个数。

  • void write(String str)写入字符串。

  • void write(String str, int off, int len) 写入字符串的某一部分,off字符串的开始索引,len写的字符个数。

  • void flush()刷新该流的缓冲。

  • void close() 关闭此流,但要先刷新它。

4.FileWriter类

java.io.FileWriter类是写出字符到文件的便利类。构造时使用系统默认的字符编码和默认字节缓冲区。

(1)构造方法
  • FileWriter(File file):创建一个新的FileWriter,给定要读取的File对象。

  • FileWriter(String fileName):创建一个新的FileWriter,给定要读取的文件名称。

当你创建一个流对象时,必须传入一个文件路径,类似于FileOutputStream

  • 构造举例,代码如下:

public class mainIo1 {

public static void main(String[] args) throws IOException {

File file=new File(“a.txt”);

FileWriter fw1=new FileWriter(file);

FileWriter fw2=new FileWriter(“b.txt”);

}

}

(2)基本写出数据
  1. 写出字符write(int b)方法,每次可以写出一个字符数据,代码使用演示:

public class FWWrite {

public static void main(String[] args) throws IOException {

// 使用文件名称创建流对象

FileWriter fw = new FileWriter(“fw.txt”);

// 写出数据

fw.write(97); // 写出第1个字符

fw.write(‘b’); // 写出第2个字符

fw.write(‘C’); // 写出第3个字符

fw.write(30000); // 写出第4个字符,中文编码表中30000对应一个汉字。

/*

【注意】关闭资源时,与FileOutputStream不同。

如果不关闭,数据只是保存到缓冲区,并未保存到文件。

*/

// fw.close();

}

}

输出结果:

abC田

小贴士:

  1. 虽然参数为int类型四个字节,但是只会保留一个字符的信息写出。
  1. 未调用close方法,数据只是保存到了缓冲区,并未写出到文件。
(3)关闭和刷新

因为内置缓冲区的原因,如果不关闭输出流,无法写出字符到文件中。但是关闭的流对象,是无法继续写出数据的。如果我们既想写出数据,又想继续使用流,就需要flush 方法了。

  • flush :刷新缓冲区,流对象可以继续使用。

  • close:先刷新缓冲区,然后通知系统释放资源。流对象不可以再被使用了。

代码如下:

public class FWWrite {

public static void main(String[] args) throws IOException {

// 使用文件名称创建流对象

FileWriter fw = new FileWriter(“fw.txt”);

// 写出数据,通过flush

fw.write(‘刷’); // 写出第1个字符

fw.flush();

fw.write(‘新’); // 继续写出第2个字符,写出成功

fw.flush();

// 写出数据,通过close

fw.write(‘关’); // 写出第1个字符

fw.close();

fw.write(‘闭’); // 继续写出第2个字符,【报错】java.io.IOException: Stream closed

fw.close();

}

}

小贴士:即便是flush方法写出了数据,操作的最后还是要调用close方法,释放系统资源。

(4)写出其它数据
  1. 写出字符数组write(char[] cbuf)write(char[] cbuf, int off, int len) ,每次可以写出字符数组中的数据,用法类似FileOutputStream,代码使用演示:

public class mainIo1 {

public static void main(String[] args) throws IOException {

FileWriter fw=new FileWriter(“a.txt”);

char[] c=“小皮皮美滋滋”.toCharArray();

fw.write©;

fw.write(c,2,2);

fw.close();

}

}

  1. 写出字符串write(String str)write(String str, int off, int len) ,每次可以写出字符串中的数据,更为方便,代码使用演示:

public class mainIo1 {

public static void main(String[] args) throws IOException {

FileWriter fw=new FileWriter(“a.txt”);

String s=“小皮皮美滋滋”;

fw.write(s);

fw.write(s,2,2);

fw.close();

}

}

  1. 续写和换行:操作类似于FileOutputStream。

public class mainIo1 {

public static void main(String[] args) throws IOException {

FileWriter fw=new FileWriter(“a.txt”,true);

String s=“小皮皮美滋滋”;

fw.write(s);

fw.write(“\r\n”);

fw.write(s,2,2);

fw.close();

}

}

小贴士:字符流,只能操作文本文件,不能操作图片,视频等非文本文件。

当我们单纯读或者写文本文件时 使用字符流 其他情况使用字节流

四.IO异常的处理


1.JDK7前处理

之前的入门练习,我们一直把异常抛出,而实际开发中并不能这样处理,建议使用try...catch...finally 代码块,处理异常部分,代码使用演示:

public class HandleException1 {

public static void main(String[] args) {

// 声明变量

FileWriter fw = null;

try {

//创建流对象

fw = new FileWriter(“fw.txt”);

// 写出数据

fw.write(“黑马程序员”); //黑马程序员

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (fw != null) {

fw.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

2.JDK7的处理

还可以使用JDK7优化后的try-with-resource 语句,该语句确保了每个资源在语句结束时关闭。所谓的资源(resource)是指在程序完成后,必须关闭的对象。

格式:

try (创建流对象语句,如果多个,使用’;'隔开) {

// 读写数据

} catch (IOException e) {

e.printStackTrace();

}

代码使用演示:

public class mainIo1 {

public static void main(String[] args) {

try (FileWriter fw = new FileWriter(“a.txt”)) {

fw.write(“小皮皮”);

} catch (IOException e) {

System.out.println(e);

}

}

}

3.JDK9的改进(扩展知识点了解内容)

JDK9中try-with-resource 的改进,对于引入对象的方式,支持的更加简洁。被引入的对象,同样可以自动关闭,无需手动close,我们来了解一下格式。

改进前格式:

// 被final修饰的对象

final Resource resource1 = new Resource(“resource1”);

// 普通对象

Resource resource2 = new Resource(“resource2”);

// 引入方式:创建新的变量保存

try (Resource r1 = resource1;

Resource r2 = resource2) {

// 使用对象

}

改进后格式:

// 被final修饰的对象

final Resource resource1 = new Resource(“resource1”);

// 普通对象

Resource resource2 = new Resource(“resource2”);

// 引入方式:直接引入

try (resource1; resource2) {

// 使用对象

}

改进后,代码使用演示:

public class TryDemo {

public static void main(String[] args) throws IOException {

// 创建流对象

final FileReader fr = new FileReader(“in.txt”);

FileWriter fw = new FileWriter(“out.txt”);

// 引入到try中

try (fr; fw) {

// 定义变量

int b;

// 读取数据

while ((b = fr.read())!=-1) {

// 写出数据

fw.write(b);

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

五.属性集


1.构造方法

  • public Properties() :创建一个空的属性列表。

2.基本的存储方法

  • public Object setProperty(String key, String value) : 保存一对属性。

  • public String getProperty(String key) :使用此属性列表中指定的键搜索属性值。

  • public Set<String> stringPropertyNames() :所有键的名称的集合。

public class mainIo1 {

public static void main(String[] args) throws IOException {

Properties p = new Properties();

p.setProperty(“xpp”, “111”);

p.setProperty(“mzz”, “222”);

;

p.setProperty(“lbj”, “333”);

System.out.println§;

System.out.println(p.getProperty(“xpp”));

System.out.println(p.getProperty(“mzz”));

System.out.println(p.getProperty(“lbj”));

System.out.println(p.getProperty(“ppp”));

Set s = p.stringPropertyNames();

for (String s1 : s) {

System.out.println(s1 + “-----” + p.getProperty(s1));

}

}

}

总结

机会是留给有准备的人,大家在求职之前应该要明确自己的态度,熟悉求职流程,做好充分的准备,把一些可预见的事情做好。

对于应届毕业生来说,校招更适合你们,因为绝大部分都不会有工作经验,企业也不会有工作经验的需求。同时,你也不需要伪造高大上的实战经验,以此让自己的简历能够脱颖而出,反倒会让面试官有所怀疑。

你在大学时期应该明确自己的发展方向,如果你在大一就确定你以后想成为Java工程师,那就不要花太多的时间去学习其他的技术语言,高数之类的,不如好好想着如何夯实Java基础。下图涵盖了应届生乃至转行过来的小白要学习的Java内容:

请转发本文支持一下

了解详情https://docs.qq.com/doc/DSmxTbFJ1cmN1R2dB
2.基本的存储方法

  • public Object setProperty(String key, String value) : 保存一对属性。

  • public String getProperty(String key) :使用此属性列表中指定的键搜索属性值。

  • public Set<String> stringPropertyNames() :所有键的名称的集合。

public class mainIo1 {

public static void main(String[] args) throws IOException {

Properties p = new Properties();

p.setProperty(“xpp”, “111”);

p.setProperty(“mzz”, “222”);

;

p.setProperty(“lbj”, “333”);

System.out.println§;

System.out.println(p.getProperty(“xpp”));

System.out.println(p.getProperty(“mzz”));

System.out.println(p.getProperty(“lbj”));

System.out.println(p.getProperty(“ppp”));

Set s = p.stringPropertyNames();

for (String s1 : s) {

System.out.println(s1 + “-----” + p.getProperty(s1));

}

}

}

总结

机会是留给有准备的人,大家在求职之前应该要明确自己的态度,熟悉求职流程,做好充分的准备,把一些可预见的事情做好。

对于应届毕业生来说,校招更适合你们,因为绝大部分都不会有工作经验,企业也不会有工作经验的需求。同时,你也不需要伪造高大上的实战经验,以此让自己的简历能够脱颖而出,反倒会让面试官有所怀疑。

你在大学时期应该明确自己的发展方向,如果你在大一就确定你以后想成为Java工程师,那就不要花太多的时间去学习其他的技术语言,高数之类的,不如好好想着如何夯实Java基础。下图涵盖了应届生乃至转行过来的小白要学习的Java内容:

请转发本文支持一下

[外链图片转存中…(img-3y8ZstoI-1724088077941)]

[外链图片转存中…(img-SMB6SBwZ-1724088077942)]

了解详情https://docs.qq.com/doc/DSmxTbFJ1cmN1R2dB

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值