I/O流

IO

1.1 File类

File在java中表示(路径的)文件或者目录。

 

1.1.1 File常用属性和方法

public static void main(String[] args) {

 

// 给定路径创建File对象

// File file = new File("D:"+File.separator+"javatest"+File.separator+"a.txt");

File file = new File("d:\\javatest\\b.mp3");

System.out.println(file);

 

// 文件基本属性

System.out.println(file.canExecute());

System.out.println(file.canRead());

System.out.println(file.canWrite());

 

// 文件的创建、删除

if(!file.exists()) {

 

boolean r;

try {

r = file.createNewFile();

if(r) {

System.out.println("文件创建成功");

}

} catch (IOException e) {

e.printStackTrace();

}

}

 

// 删除文件

file.delete();

}

 

创建文件时会抛出检查时异常IOException

1.1.2 File的路径相关

public static void main(String[] args) {

 

 File file = new File("d:\\javatest\\a");

// File file = new File("a.txt");

 

// 获取file的绝对路径

System.out.println(file.getAbsolutePath());

// 获取file的创建时的路径字符串

System.out.println(file.getPath());

// 获取文件或者目录的名字

System.out.println(file.getName());

// 获取文件或者目录的父目录

System.out.println(file.getParent());

 

}

注意:如果file是相对路径,相对路径的当前路径是工程目录(java17)

 

1.1.3 目录的创建

public static void main(String[] args) {

 

 File file = new File("d:\\javatest\\c\\d\\e");

 

 if(!file.exists()) {

 boolean r;

 

try {

// 一次只能创建一个目录

// r = file.mkdir();

r = file.mkdirs();

if(r) {

System.out.println("目录创建成功");

}

} catch (Exception e) {

e.printStackTrace();

}

 

 }

}

 

1.1.4 目录的遍历

list():返回一个file表示的目录中的子目录或者文件,字符串数组类型

listFiles():返回一个file表示的目录中的子目录或者文件File数组类型

public static void main(String[] args) {

 

// 需求:遍历d:\javatest目录

// list()

File file =  new File("d:\\javatest");

 

 

/*

String[] list = file.list();

 

for (String str : list) {

System.out.print(str);

File f = new File(file.getPath()+"\\"+str);

if(f.isDirectory()) {

System.out.println(" 目录");

}else {

System.out.println(" 文件");

}

}*/

 

 

// listFiles();

File[] listFiles = file.listFiles();

for (File f : listFiles) {

System.out.print(f.getName());

if(f.isDirectory()) {

System.out.println(" 目录");

}else {

System.out.println(" 文件");

}

}

}

 

├  └

 

1.2 IO

1.2.1 

流(stream):流是一连串流动的数据(字节、字符),先进先出的方式发送的信息的通道中。

 

 

1.2.2 输入流和输出流

输入流

数据从源数据源程序的过程称为输入流可以理解为从源数据源读取数据到程序的过程

 

 

输出

数据从程序流到目的地的过程称为输出流。可以理解为把数据程序写入目的地的过程

 

 

数据源一般指提供数据的原始媒介,一般常见有文件、数据库、云端其他硬件等能提供数据的媒介。

 

1.2.3 的分类

按照流向分为输入流和输出流

按照处理单元分为字节流和字符流

按照功能分为节点流转换流。

 

 

 

1.3 InputStream/OutputStream

InputStream 是所有字节输入流抽象父类提供了

read 读取一个字节

read(byte[] buf) 读取一定量的字节到缓冲区数组 buf中

 

OutputStream 是所有字节输出流的抽象父类,提供了

write() 写入一个字节

write(byte[] buf) 写入一定量的字节到输出流

 

FileInputStream 文件字节输入流专门用于从文件中读取字节到程序内存中。

FileOutputStream 文件字节输出流,专门用于从内存中写入字节到文件中。

 

需求:文件读取一个字节

public static void main(String[] args) {

 

// 需求:读取一个文件中的一个字节

File file = new File("d:\\javatest\\a.txt");

 

// 1】创建管道

FileInputStream in = null;

 

try {

in = new FileInputStream(file);

 

// 2】从管道读取一个字节

/*

int t;

t = in.read();

t = in.read();

t = in.read();

t = in.read();

*/

// System.out.println(t);

 

// 循环读取一个字节

int t;

StringBuilder sb = new StringBuilder();

while( (t=in.read()) != -1 ) {

sb.append((char)t);

}

 

System.out.println(sb.toString());

 

 

 

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch(IOException e) {

e.printStackTrace();

}

 

// 3】关闭流管道

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

 

一次读取多个字节

public static void main(String[] args) {

 

// 需求:一次读取多个字节

File file = new File("d:\\javatest\\a.txt");

 

// 1】创建管道

FileInputStream in = null;

 

try {

in = new FileInputStream(file);

 

// 2】从管道读取多个字节到缓冲区

/*

byte[] buf = new byte[5];

int len;

len = in.read(buf);

len = in.read(buf);

len = in.read(buf);

len = in.read(buf);

 

for(byte b:buf) {

System.out.print((char)b+"\t");

}

System.out.println(len);

*/

 

// 通过循环读取文件

byte[] buf = new byte[5];

int len;

StringBuilder sb = new StringBuilder();

while( (len=in.read(buf)) != -1 ) {

// 读取的内容是原始二进制流,需要根据编码的字符集解码成对于字符

String str = new String(buf,0,len);

sb.append(str);

}

System.out.println(sb.toString());

 

 

 

 

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch(IOException e) {

e.printStackTrace();

}

 

// 3】关闭流管道

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

 

需求:按照指定编码写入文件

public static void main(String[] args) {

 

 

File file = new File("d:\\javatest\\c.txt");

 

FileOutputStream out = null;

 

try {

// 1】创建输出流管道

out = new FileOutputStream(file);

 

// 2】写入数据到管道中

// 一次写入一个字节

/*

out.write(97);

out.write(98);

out.write(99);

*/

 

// 一次写入多个字节

String str = "hello world";

// gbk

/*

byte[] buf = str.getBytes();

out.write(buf);

*/

 

byte[] buf = str.getBytes("UTF-8");

out.write(buf);

 

System.out.println("写入完成!");

 

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

 

// 3】关闭流

try {

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

注意:

[1]字符串写入文件时一定会存在编码问题

[2]使用utf8编码写入文件时,如果不含中文时,win系统会对文件的编码造成误判。

[3] 通过字节流写入文件时,管道写入一个字节,该字节立即写入文件中。

 

总结

InputStream/OutputStream 用于字节的读写。主要用于读取二进制文件(图片、音频、视频),也可以读取文件性文件。

 

需求:d:\\javatest\\logo.png 复制到工程目录中并显示复制进度。

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

 

 

File oriFile = new File("d:\\javatest\\logo.jpg");

File toFile = new File("logo.jpg");

 

long totalLen = oriFile.length(); // 文件大小

long cpyedLen = 0; // 已复制完成的大小

float progress = 0.0f;

 

FileInputStream in = new FileInputStream(oriFile);

FileOutputStream out = new FileOutputStream(toFile);

 

// 一次读取1kb

byte[] buf = new byte[512];

int len;

while( (len=in.read(buf)) != -1) {

out.write(buf, 0, len);

cpyedLen += len;

progress = cpyedLen*1.0f/totalLen;

System.out.println(progress);

 

 

}

 

in.close();

out.close();

 

System.out.println("复制完成!");

 

}

 

思考:用字节流来读取文本性文件时,按字节读容易造成乱码。此时,能不能按字符来读取。

 

1.4 Reader/Writer

Reader 是字符输入流的抽象父类,提供了

read 一次读取一个字符

read(char[] cbuf) 一次读取多个字符到字符缓冲区cbuf返回长度表示读取的字符个数

 

Writer 是字符输出流的抽象父类,提供了

write

write(char[] cbuf)

write(string)

 

FileReader 文件字符输入流,专门用于读取默认字符编码文本性文件。

 

FileWriter 文件字符输出流,专门用于写入默认字符编码的文本性文件。为了提高效率,FileWriter内部存在一个字节缓冲区,用于对待写入的字符进行统一编码字节缓冲区,一定要在关闭流之前,调用flush方法刷新缓冲区。

 

需求:一次读取一个字符/多个字符到cbuf

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

 

File file = new File("d:\\javatest\\d.txt");

 

FileReader reader = new FileReader(file);

 

// 1】一次读取一个字符

/*

int c;

c = reader.read();

c = reader.read();

c = reader.read();

c = reader.read();

c = reader.read();

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

*/

 

// 2】一次读取多个字符到cbuf

/*

char[] cbuf = new char[2];

int len;

len = reader.read(cbuf);

len = reader.read(cbuf);

len = reader.read(cbuf);

len = reader.read(cbuf);

System.out.println(Arrays.toString(cbuf));

System.out.println(len);

*/

 

char[] cbuf = new char[2];

int len;

StringBuilder sb = new StringBuilder();

while( (len=reader.read(cbuf)) != -1 ) {

sb.append(cbuf,0,len);

}

 

System.out.println(sb);

}

 

需求:写入字符到文件中

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

 

 

File file = new File("d:\\javatest\\f.txt");

 

FileWriter writer = new FileWriter(file);

 

// 1】一次写入一个字符

/*writer.write('');

writer.write('');*/

 

// 2】一次写入多个字符

/*char[] cbuf = {'h','e','l','l','o','',''};

writer.write(cbuf);*/

 

// 3】一次写入一个字符串

String str = "hello你好";

writer.write(str);

 

 

// 刷新字节缓冲区

writer.flush();

 

// 关闭流通道

writer.close();

 

System.out.println("写入完成");

}

 

思考:如何字符串utf8或者其他编码写入文件?

 

1.5 转换

 

InputStreamReader 继承Reader,是字节流通向字符流的桥梁可以把字节流按照指定编码 解码 成字符流。

 

OutputStreamWriter 继承Writer,是字符流通向字节流的桥梁,可以把字符流按照指定的编码 编码 成字节流。

1.5.1 转换流工作原理

 

 

需求:写入utf8文件

/**

 * 把一个字符串以utf8编码写入文件

 */

public class Test01 {

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

 

 

String str = "hello中国";

File file = new File("d:\\javatest\\g.txt");

 

// 1】创建管道

FileOutputStream out = new FileOutputStream(file);

OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");

 

// 2】写入管道

writer.write(str);

 

// 3】刷新缓冲区

writer.flush();

 

// 4】关闭管道

out.close();

writer.close();

 

System.out.println("写入完成");

}

}

 

需求:读取utf8文件

/**

 * 读取utf8编码的文本文件

 */

public class Test01 {

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

 

File file = new File("d:\\javatest\\g.txt");

 

// 1】建立管道

FileInputStream in = new FileInputStream(file);

InputStreamReader reader = new InputStreamReader(in, "UTF-8");

 

char[] cbuf = new char[2];

int len;

 

StringBuilder sb = new StringBuilder();

while( (len=reader.read(cbuf))!=-1 ) {

sb.append(cbuf, 0, len);

}

System.out.println(sb.toString());

 

}

}

 

注意:

[1]win平台默认的utf8编码的文本性文件带有BOM,java转换流写入的utf8文件不带BOM。所以java读取手动创建的utf8文件会出现一点乱码(?hello中国,?bom导致的)

[2] 一句话:用字符集编码,一定用字符集解码!!

 

思考:

FileReader = InputStreamReader + GBK

package cn.sxt07.outputstreamwriter;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStreamReader;

 

/**

 * 读取一个gbk编码的文本性文件

 */

public class Test02 {

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

 

 

File file = new File("d:\\javatest\\f.txt");

 

// 1】建立管道

/*

 * FileInputStream in = new FileInputStream(file);

 * InputStreamReader reader =  new InputStreamReader(in, "GBK");

 */

 

FileReader reader = new FileReader(file);

 

char[] cbuf = new char[2];

int len;

 

StringBuilder sb = new StringBuilder();

while( (len=reader.read(cbuf))!=-1 ) {

sb.append(cbuf, 0, len);

}

 

reader.close();

 

System.out.println(sb.toString());

}

}

 

 

1.6 BufferedReader/BufferedWriter

BufferedReader 继承Reader提供

read

read(char[] cbuf)

readLine() 用于读取一行文本,实现对文本的高效读取。

BufferedReader 初始化时需要一个reader本质BufferedReaderreader的基础上增加readLine()的功能。

 

BufferedWriter继承Writer提供

write

write(char[] cbuf)

write(string)

newline() 写入一个行分隔符。

 

需求:读取一首诗

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

 

// 按行读取gbk文本性文件

 

File file = new File("d:\\javatest\\i.txt");

 

// 1】创建管道

FileReader reader = new FileReader(file);

BufferedReader br = new BufferedReader(reader);

 

// 2】读取一行

/*

String line =  br.readLine();

line =  br.readLine();

line =  br.readLine();

line =  br.readLine();

*/

 

String line;

while( (line=br.readLine()) != null) {

System.out.println(line);

}

}

 

需求:gbk编码写入一首诗到文件

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

 

File file = new File("d:\\javatest\\j.txt");

 

// 1】创建gbk管道

FileWriter writer = new FileWriter(file);

BufferedWriter bw = new BufferedWriter(writer);

 

// 2】写入一行

bw.write("窗前明月光,");

bw.newLine();

 

bw.write("疑似地上霜。");

 

// for win

// bw.write("\r\n");

 

// for unix/linux/mac

// bw.write("\n");

 

bw.write("举头望明月,");

bw.newLine();

 

// 3flush

bw.flush();

 

// 4】关闭管道

bw.close();

writer.close();

}

 

需求:utf8编码高效写入文件

/**

 * utf8写入一首诗

 * @author Administrator

 *

 */

public class Test02 {

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

 

File file = new File("d:\\javatest\\j-utf8.txt");

 

// 1】创建utf8管道

FileOutputStream out = new FileOutputStream(file);

OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");

BufferedWriter bw = new BufferedWriter(writer);

 

// 2】写入一行

bw.write("窗前明月光,");

bw.newLine();

 

bw.write("疑似地上霜。");

 

// for win

bw.write("\r\n");

 

// for unix/linux/mac

// bw.write("\n");

 

bw.write("举头望明月,");

bw.newLine();

 

// 3flush

bw.flush();

 

// 4】关闭管道

bw.close();

writer.close();

}

}

posted @ 2019-05-06 21:05  御云凌月  阅读(113)  评论(0编辑  收藏  举报