目录
0.前述概要
1. File类
1.1 概述
1.2 File的重要方法
1.3 java.io
1.3.1 四种抽象类
1.3.2 流
1.3.3 其他常用 I/O 流
2. 字节输入流(InputSteam)
2.1 关系类图
2.2 应用实现
3. 字节输出流(OutputStream)
3.1 概述与类图
3.2 应用实现
4. 字符输出流 Reader & 字符输出流
5. 将字节流转换为字符流(InputStreamReader & OutputStreamWriter)
5.1 InputStreamReader
5.2 OnputStreamWriter
6. 缓冲区
6.1 缓冲区的作用
6.2 通过 BufferReader 实现整行读取数据
7. 利用URLConnection对象下载网络图片
0.前述概要
1. File类
1.1 概述
- File类 是java.io 包下 代表与平台无关的 文件和目录
- 程序中操作文件和目录,都可以通过File 类完成。File能新建、删除、重命名文件和目录
- File 类不能访问文件内容本身。需要使用输入/输出流访问文件内容本身
1.2 File的重要方法
public class FileDemo {public static void main(String[] args) {// 1 新建文件对象File file1 = new File("d:/vb.txt");File file2 = new File("d:\\vc.html");// 1.1 创建文件try{boolean f1 = file1.createNewFile();boolean f2 = file2.createNewFile();// 1.2 获取文件路径 、 获取文件名 、 文件是否存在System.out.println(file1.getPath()+ "["+file1.getName()+"], 创建成功?:"+ f1);// 1.3 文件是否存在System.out.println(file2.getPath()+ "["+file2.getName()+"], 已存在?:"+ file2.exists());// 1.4 是否是目录or目录 、 文件大小System.out.println(file2.getName()+"文件?:" + file1.isFile() + " , 大小: " + file1.length());System.out.println(file2.getName()+"目录?:" + file2.isDirectory() + " , 大小: " + file2.length());// 1.5 删除文件System.out.println(file1.getPath() + "删除成功?:"+ file1.delete() + ",是否存在:" + file1.exists());}catch(IOException e){e.printStackTrace();}// 2 创建目录对象File dir1 = new File("d:/game/游戏/王者荣耀/戈雅");File dir2 = new File("d:/貂蝉");File dir3 = new File("d:/game/游戏/王者荣耀/夏洛特");try{boolean d1 = dir1.mkdirs();boolean d2 = dir2.mkdir();boolean d3 = dir3.mkdirs();System.out.println(dir1.getPath() + ",创建成功:?" + d1 + ",存在:?" + dir1.exists());System.out.println(dir2.getPath() + ",创建成功:?" + d2 + ",存在:?" + dir2.exists());System.out.println(dir3.getPath() + ",创建成功:?" + d3 + ",存在:?" + dir3.exists());dir1.delete();dir2.delete();}catch (Exception e){e.printStackTrace();}}
}
1.3 java.io
- Java的 IO相关操作 通过 java.io包下的类或接口 来支持
- 按 出入的方向可分为: 输入与输出
- 按 内容类型可分为: 字节流与字符流
1.3.1 四种抽象类
- 字节输入流 - InputStream
- 字节输出流 - OutputStream
- 字符输出流 - Reader
- 字符输出流 - Writer
1.3.2 流
Java把传输的数据抽象成为流(Stream),简化了程序处理
1.3.3 其他常用 I/O 流
2. 字节输入流(InputSteam)
2.1 关系类图
2.2 应用实现
- 实例化 InputSream 对象
- 调用 read()方法 循环读取 字节数据 , 进行处理
- 调用 close() 方法 关闭InputStream对象
import java.io.*;// 实现以 字节流 方式读取 文件数据
public class InputStreamDemo {public static void main(String[] args) throws FileNotFoundException {// 1.1 实例化 File 对象File source1 = new File("d:/game/游戏/王者荣耀/夏洛特/xlt.txt");File source2 = new File("d:/game/游戏/王者荣耀/夏洛特/xlt.png");InputStream fis = null;try{// 1.2 实例化 InputStream 对象
// fis = new FileInputStream(source1);fis = new FileInputStream(source2);// 2.1 定义 读取单位块 数组
// byte[] bs = new byte[1024];byte[] bs = new byte[1024*1024];int len;// 2.2 调用 read()方法 循环读取 字节数据 进行处理while((len = fis.read(bs)) != -1){System.out.println(len); //16 //375477}}catch(FileNotFoundException e){e.printStackTrace();}catch(IOException e){e.printStackTrace();} finally{if(fis != null){try{// 3 调用 close() 方法, 关闭InputStream对象fis.close();}catch(IOException e){e.printStackTrace();}}}}
}
3. 字节输出流(OutputStream)
3.1 概述与类图
3.2 应用实现
- 实例化 OutputStream 对象
- 调用write() 循环写入 字节数据
- 调用close() 关闭 OutputStream 对象
import java.io.*;/** 需求:** 实现 以字节流的方式 读取文件数据* 再 以字节流的方式 输出到其他文件中** ==》 即实现文件内容的复制*/
public class OutputStreamDemo {public static void main(String[] args) throws FileNotFoundException {// 1.1 实例化 File 对象File source1 = new File("d:/game/游戏/王者荣耀/夏洛特/xlt.txt");File source2 = new File("d:/game/游戏/王者荣耀/夏洛特/xlt.jpg");// A.1 实例化 File 对象File target = new File("d:/copy_xlt.png");InputStream fis = null;OutputStream fos = null;try{// 1.2 实例化 InputStream 对象
// fis = new FileInputStream(source1);fis = new FileInputStream(source2);// A.2 实例化 OutputStream 对象fos = new FileOutputStream(target);// 2.1 定义 读取单位块 数组
// byte[] bs = new byte[1024];byte[] bs = new byte[1024*1024];int len;// 2.2 调用 read()方法 循环读取 字节数据 进行处理while((len = fis.read(bs)) != -1){System.out.println(len); //16 //375477// B. 调用write() 循环写入 字节数据fos.write(bs, 0, len);}}catch(FileNotFoundException e){e.printStackTrace();}catch(IOException e){e.printStackTrace();} finally{// 通过finally块 确保fos & fis对象执行close方法if(fos != null){try{// C. 调用close() 关闭 OutputStream 对象fos.close();}catch(IOException e){e.printStackTrace();}}if(fis != null){try{// 3 调用 close() 方法, 关闭InputStream对象fis.close();}catch(IOException e){e.printStackTrace();}}}}/*** java.io.FileNotFoundException: d:\game\游戏\王者荣耀\夏洛特\xlt.png (系统找不到指定的文件。)* at java.io.FileInputStream.open0(Native Method)* at java.io.FileInputStream.open(FileInputStream.java:195)* at java.io.FileInputStream.<init>(FileInputStream.java:138)* at OutputStreamDemo.main(OutputStreamDemo.java:25)*/
}
4. 字符输出流 Reader & 字符输出流
- Reader 是 所有字符输入流 的抽象父类
- Writer 是 所有字符输出流 的抽象父类
- FileReader 与 FileWriter 分别对应了 文本文件的 读取与写入
Code: 应用字符输入输出流实现文本 读取与写入
import java.io.*;/** 需求:* 应用字符输入输出流实现文本 读取与写入*/
public class ReaderAndWriterDemo {public void readTextFile(){Reader reader = null;try{// 实例化 file 对象File file = new File("d:/game/游戏/王者荣耀/夏洛特/xlt.txt");// 实例化 FileReader 对象reader = new FileReader(file);int ch = 0;while((ch = reader.read())!= -1){
// System.out.print(ch+" "); // UTF-8 编码集 打印的是ASCLL 码System.out.print((char)ch+ " "); // UTF-8 编码集 打印的是字符}}catch(IOException e){e.printStackTrace();}finally{try{if(reader != null){reader.close();}}catch(IOException e){e.printStackTrace();}}}public void writerTextFile(){Writer writer = null;try{// 实例化 File 对象File file = new File("d:/game/游戏/王者荣耀/夏洛特/xlt.txt");// 若这个文件不存在 ,则新建一个文件。if(!file.exists()){file.createNewFile();}// 实例化writer对象writer = new FileWriter(file);// write() 方法用于 ---> 覆盖已有的文件内容writer.write("起手技!");writer.write("玫瑰剑士即剑神!");// append() 方法用于 ---> 再已有文件末尾追加内容writer.append("Append被动:七星光芒剑");}catch(IOException e){e.printStackTrace();}finally{if(writer != null){try {writer.close();} catch (IOException e) {e.printStackTrace();}}}}public static void main(String[] args) {ReaderAndWriterDemo r = new ReaderAndWriterDemo();
// r.writerTextFile();/*** 起手技!玫瑰剑士即剑神!Append被动:七星光芒剑*/r.readTextFile();/*** 29611 29808 21073 22763 21363 21073 31070 65281 65 112 112 101 110 100 34987 21160 65306 19971 26143 20809 33426 21073** 玫 瑰 剑 士 即 剑 神 ! A p p e n d 被 动 : 七 星 光 芒 剑*/}
}import java.io.*;/** 需求:* 应用字符输入输出流实现文本 读取与写入*/
public class ReaderAndWriterDemo {public void readTextFile(){Reader reader = null;try{// 实例化 file 对象File file = new File("d:/game/游戏/王者荣耀/夏洛特/xlt.txt");// 实例化 FileReader 对象reader = new FileReader(file);int ch = 0;while((ch = reader.read())!= -1){
// System.out.print(ch+" "); // UTF-8 编码集 打印的是ASCLL 码System.out.print((char)ch+ " "); // UTF-8 编码集 打印的是字符}}catch(IOException e){e.printStackTrace();}finally{try{if(reader != null){reader.close();}}catch(IOException e){e.printStackTrace();}}}public void writerTextFile(){Writer writer = null;try{// 实例化 File 对象File file = new File("d:/game/游戏/王者荣耀/夏洛特/xlt.txt");// 若这个文件不存在 ,则新建一个文件。if(!file.exists()){file.createNewFile();}// 实例化writer对象writer = new FileWriter(file);// write() 方法用于 ---> 覆盖已有的文件内容writer.write("起手技!");writer.write("玫瑰剑士即剑神!");// append() 方法用于 ---> 再已有文件末尾追加内容writer.append("Append被动:七星光芒剑");}catch(IOException e){e.printStackTrace();}finally{if(writer != null){try {writer.close();} catch (IOException e) {e.printStackTrace();}}}}public static void main(String[] args) {ReaderAndWriterDemo r = new ReaderAndWriterDemo();
// r.writerTextFile();/*** 起手技!玫瑰剑士即剑神!Append被动:七星光芒剑*/r.readTextFile();/*** 29611 29808 21073 22763 21363 21073 31070 65281 65 112 112 101 110 100 34987 21160 65306 19971 26143 20809 33426 21073** 玫 瑰 剑 士 即 剑 神 ! A p p e n d 被 动 : 七 星 光 芒 剑*/}
}
5. 将字节流转换为字符流(InputStreamReader & OutputStreamWriter)
5.1 InputStreamReader
FileReader其实可以完全实现这样的功能
Code实现
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;public class InputStreamReaderDemo {public void changeReadDemo(){FileInputStream fis = null;InputStreamReader isr = null;try{// 实例化 File 对象File file = new File("d:/game/游戏/王者荣耀/夏洛特/xlt.txt");// 实例化 FileInputStream 对象fis = new FileInputStream(file);// 实例化 InputStreamReader 对象// 将 字节流 转换为 字符流isr = new InputStreamReader(fis,"UTF-8");while(isr.ready()){
// System.out.println(isr.read());System.out.print((char)isr.read());}}catch(IOException e){e.printStackTrace();}finally{if(isr != null){try{isr.close();}catch(IOException e){e.printStackTrace();}}if(fis != null){try{fis.close();}catch (IOException e){e.printStackTrace();}}}}public static void main(String[] args) {InputStreamReaderDemo reader = new InputStreamReaderDemo();reader.changeReadDemo();/*** 29611* 29808* 21073* 22763* 21363* 21073* 31070*//*** 玫瑰剑士即剑神!Append被动:七星光芒剑*/}
}
5.2 OnputStreamWriter
需求:
实现 将字节输出流转换成 字符输出流
/**需求* 实现 将字节输出流转换成 字符输出流*/
public class OutputStreamWriterDemo {public void changeWriter(){FileOutputStream fos = null;OutputStreamWriter osw = null;try{// 实例化 File 对象File file = new File("d:/game/游戏/王者荣耀/夏洛特/xlt.txt");if(!file.exists()){// 创建文件file.createNewFile();}fos = new FileOutputStream(file);//实现 将字节输出流转换成 字符输出流osw = new OutputStreamWriter(fos, "UTF-8");osw.append("追加:qlj这是xlt的核心前提");osw.write("写入:qlj这是xlt的核心前提");}catch (IOException e){e.printStackTrace();}finally{try{if(osw != null){osw.close();}if(fos != null){osw.close();}}catch (IOException e){e.printStackTrace();}}}public static void main(String[] args) {OutputStreamWriterDemo demo = new OutputStreamWriterDemo();demo.changeWriter();}/*** 追加:qlj这是xlt的核心前提写入:qlj这是xlt的核心前提*/
}
6. 缓冲区
6.1 缓冲区的作用
6.2 通过 BufferReader 实现整行读取数据
/** 需求: 实现BufferReaderDemo内容的整行读取* 通过 BufferReader 实现整行读取数据*/
import java.io.*;/** 需求: 实现BufferReaderDemo内容的整行读取* 通过 BufferReader 实现整行读取数据*/
public class BufferReaderDemo {public void readBuffer(){Reader reader = null;BufferedReader br = null;try{// 1. 实例化 File 对象File file = new File("D:\\s_java\\se\\6-4io\\ioProj\\src\\main\\java\\BufferReaderDemo.java");// 2. 实例化 FileReader 对象reader = new FileReader(file);// 3. 实例化 BufferedReader 对象br = new BufferedReader (reader);String line = null;while((line = br.readLine()) != null){System.out.println(line);}}catch (IOException e){e.printStackTrace();}finally{try{if(br != null){br.close();}if(reader != null){reader.close();}}catch (IOException e){e.printStackTrace();}}}public static void main(String[] args) {BufferReaderDemo demo = new BufferReaderDemo();demo.readBuffer();}/*** public class BufferReaderDemo {** public void readBuffer(){* Reader reader = null;* BufferedReader br = null;* ....*/
}
7. 利用URLConnection对象下载网络图片
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;/*** 利用URLConnection对象下载网络图片*/
public class URLConDownLoadImg {public static void main(String[] args) {// 声明一个输入流InputStream is = null;OutputStream os = null;try{URL url = new URL("https://img2.baidu.com/it/u=144558109,2991279374&fm=253&app=138&f=JPEG?w=1570&h=800");URLConnection con = url.openConnection();is = con.getInputStream();os = new FileOutputStream("D:\\game\\游戏\\王者荣耀\\夏洛特\\金色xlt.png");byte[] buffer = new byte[1024];int len = 0;while((len = is.read(buffer)) != -1){
// System.out.println(len);os.write(buffer, 0, len);}}catch (MalformedURLException e){e.printStackTrace();}catch (IOException e){e.printStackTrace();}finally{try{if(os != null){os.close();}if(is != null){is.close();}}catch (IOException e){e.printStackTrace();}}}/*** 1024* 1024* 1024* 1024* 1024* ...* 1024* 354*/
}