-
相关概念
- 输入:往内存中写数据的操作;
- 输入设备:键盘【标准输入设备】、鼠标、话筒、触摸板等等;
- 输出:将数据从内存中写出的操作;
- 输出设备:显示器【标准输出设备】、音响、打印机、投影仪等等;
-
File类:
- 该类是文件或目录的抽象描述,该类的对象不能表示文件的内容;
- File类针对于文件或文件目录,只能进行新建、删除、重命名、上层目录等等的操作。如果涉 及到访问文件的内容,File类是无能为力的,只能使用IO流提供的相应的输入输出流来实现;
- 常把File类的对象作为形参传递给相应的输入输出流的构造器中;
- 不同操作系统中的文件路径的表示:
①绝对路径:
本地文件协议:file://
Windows:file:[///]C:\a.txt
Linux/Unix:/home/user/a.txt
②相对路径:从当前路径开始的路径(非根路径)如:./hadoop/ect/core-site.xml - Flie类常用方法:
- createNewFile():创建一个新文件;
- createTempFile(String prefix,String suffix):创建一个临时文件;
- mkdir():创建空的单级目录;
- mkdirs():创建多级目录;
- delete():删除文件;
- exit():判断一个文件是否存在;
- getAbsolutePath():返回文件的绝对路径;
- getName():返回文件名;
- getParent():返回路径名;
- isDirectory():判断是否是目录;
- isFile():判断是否是文件;
- length():放回文件的长度;
- list():放回列出目录下的文件名的String数组;
- listFile():返回带路径的文件名的File数组;
- renameTo(File des):文件重命名;
-
输入输出流
-
IO流的划分:
①按照流的流向的不同:输入流 、输出流 (站位于程序的角度);
② 按照流中的数据单位的不同:字节流 、字符流 (纯文本文件使用字符流 ,除此之外使 用字 节流);
③ 按照流的角色的不同:节点流 、处理流 (流直接作用于文件上是节点流(4个),除 此之 外都是处理流); -
输入输出流分类图
-
第一波流(重点)
顶层抽象类 节点流(文件流) 缓冲流(处理流) InputStream FileInputStream(int read(byte[] b)) BufferedInputStream( int read(byte[] b)) OutputStream FileOutputStream(oid write(b,0,len)) BufferedOutputStream(void write(b,0,len)) Reader FileReader(int read(char[] c)) BufferedReader(int read(char[] c))或String readLine() Writer FileWriter(void write(c,0,len)) BufferedWriter((void write(c,0,len)或void write(String str)) 注: ①从硬盘中读入一个文件,要求此文件一定得存在。若不存在,报FileNotFoundException的异常; ②从程序中输出一个文件到硬盘,此文件可以不存在。若不存在,就创建一个实现输出。若存在,则将已存在 的文件覆盖; ③真正开发时,就使用缓冲流来代替节点流 ④主要最后要关闭相应的流。先关闭输出流,再关闭输入流。将此操作放入finally
- 第一波流实例:
********FileReader******** public void testFileReader() { FileReader fs = null; try { //1.造文件 File file = new File("hello.txt"); //2.造流 fs = new FileReader(file); //3.读 //方式1: // int data; // while ((data = fs.read()) != -1) { // System.out.print((char) data); // } //方式2: char[] data = new char[5]; int len; while ((len = fs.read(data)) != -1) { for (int i = 0; i < len; i++) System.out.print(data[i]); } } catch (IOException e) { e.printStackTrace(); } finally { //4.关流 try { if (fs != null) fs.close(); } catch (IOException e) { e.printStackTrace(); } } } ```
-
*********FileInputStream*********
public void testFileInputStream() {
//1.造文件,造流
FileInputStream fis = null;
try {
fis = new FileInputStream(new File("hello.txt"));
//3.读入数据
byte[] data = new byte[5];
int len;
while ((len = fis.read(data)) != -1) {
for (int i = 0; i < len; i++) {
System.out.print((char) data[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//3.关流
try {
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
********FileWriter********
public void testFileWriter() {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(new File("hello.txt"));
fw = new FileWriter(new File("hello1.txt"));
char[] data = new char[5];
int len;
while ((len = fr.read(data)) != -1)
fw.write(data, 0, len);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fr != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
********FileOutputStream********
public void testFileOutputStream() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(new File("hello.txt"));
fos = new FileOutputStream(new File("hello3.txt"));
byte[] data = new byte[5];
int len;
while ((len = fis.read(data)) != -1)
fos.write(data, 0, len);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
********BufferedReader、BufferedWriter********
//复制文本文件
public void testBufferedReaderWriter() {
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(new File("hello.txt")));
bw = new BufferedWriter(new FileWriter(new File("hello.txt2")));
char[] data = new char[5];
int len;
while ((len = br.read(data)) != -1) {
bw.write(data, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
********BufferedInputStream、BufferedOutputStream********
//复制图片、视频、、、
public void testBufferedInputOutputStream() {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(new File("C:\\1.avi")));
bos = new BufferedOutputStream(new FileOutputStream(new File("C:1(副本).avi")));
byte[] data = new byte[1024];
int len;
while ((len = bis.read(data)) != -1) {
bos.write(data, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bis != null)
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
********readLine********
public void testReadLine() {
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(new File("hello.txt")));
bw = new BufferedWriter(new FileWriter(new File("hello.txt2")));
String line;
while ((line=br.readLine()) != null){
bw.write(line+"\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}