IO (文件读写)
Input输入,Output输出
信息需要永久保存(持久化),一般用文件的形式把信息保存到磁盘
程序运行运行需要一些基本配置信息,这些配置信息也是保存在磁盘的文件中
程序从磁盘上读取文件,就称为Imput,把文件写到磁盘,称为Output(参考位置是内存)
java.io包下类的分类
-
按输入和输出的方向划分:
-
输入
Input、Reader
-
输出
Output、Writer
-
-
按数据格式分:
- 字节流(二进制文件,如:exe、office、图片,音频、视频)
Stream - 字符流(文本文件,如:txt、程序源码文件,html)
Reader、Writer
- 字节流(二进制文件,如:exe、office、图片,音频、视频)
File
File类跟磁盘上文件或目录关联,读写文件时就用这个file对象
常用方法:
public static void main(String[] args) {
//E:/Desktop/图片/1.jpg 文件路径的另一种表示方法
//创建文件访问对象,参数文件的路径和名称
//文件路径可以用分隔符可以用 \\ 或 / ,如E:/Desktop/图片
//不管文件存不存在都会创建一个File对象
File file = new File("E:\\Desktop\\备用.txt");
//判断文件是否存在,存在返回true,不不存在返回false
System.out.println(file.exists());
//返回File对象文件名(不包含路径)
System.out.println(file.getName());
//返回文件对象关联的path是否是一个文件
System.out.println(file.isFile());
//返回文件对象关联的path是否是一个文件
File fileDir = new File("E://");
System.out.println(fileDir.isFile());//false
//判断文件对象路径关联的是否是一个目录
System.out.println(fileDir.isDirectory());//true
//返回文件最后一次修改的时间戳
long lastModifile = file.lastModified();
Date date = new Date(lastModifile);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));
//返回文件长度,单位:字节
System.out.println(file.length());
//文件重命名,参数是新文件名的路径和名称
file.renameTo(new File("E:\\Desktop\\文档.txt"));
File fileClass = new File("E:\\Desktop\\文档.txt");
//删除文件
fileClass.delete();
//列出目录下所有的文件和目录
File[] files = fileDir.listFiles();
for (File f: files) {
System.out.println(f.getName());
}
File fileNew = new File("E:\\Desktop\\新创建的目录");
//创建一个新的目录,如果不存在就创建,存在就不创建
fileNew.mkdir();
}
练习
实现dir命令功能类似功能,列出指定目录下的所有目录及子目录 使用递归方法
public class MyDir {
public static void main(String[] args) {
dir(new File("E:\\Desktop\\JavaEE笔记"),"");
}
private static void dir(File file,String tab){
tab += "\t";
File[] files = file.listFiles();
for (File f:files) {
if(f.isDirectory()){
System.out.println(tab + f.getName());
dir(f,tab);
}
}
}
}
FileInputStream
以二进制方式读取文件
read(byte[] bytes) 把文件内容读入到字节数组,读取到了内容返回读到的字节的长度,没有内容返回-1
public class FileInputStreamDemo {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("E:\\Desktop\\备用.txt");
byte[] bytes = new byte[32];
int count = fis.read(bytes);
while(count != -1){
System.out.println(new String(bytes));
bytes = new byte[32];
count = fis.read(bytes);
}
/*
for (byte b : bytes) {
System.out.println(b);
}*/
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileOutputStream
以二进制方式写文件
- write(byte[] bytes)
- flush() 强制把缓冲内容写硬盘
public class FileOutputStreamDemo {
public static void main(String[] args) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream("E:\\Desktop\\备用.txt");
String str = "hello hqyj";
fos.write(str.getBytes());//产符串转为字节数组作为write的参数
fos.flush();//刷新缓冲,强制写硬盘
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
练习:实现一个copy文件的功能
一个FilelnputStream对象读文件
把读到的字节数组写入到另一个FileOutputStream的对象
注意:写的时候用fos.write(bytes,0,count);,避免把数组中最后一次多余的字节写到了文件
public class MyCopy {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("E:\\Desktop\\图片\\1.jpg");
fos = new FileOutputStream("E:\\Desktop\\new.jpg");
byte[] bytes = new byte[1024];
int count = fis.read(bytes);
while (count != -1){
//第一个参数十字街数组,第二个参数是数组下标起始位置,第三个参数之这一次实际读到的字节数(读写字节长度)
//避免最后一次读时,数组内容未读满,后面的无效字节就不会写道文件中去
fos.write(bytes,0,count);
count = fis.read(bytes);//继续下一段字节
}
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis != null);
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
BufferedReader
带有缓存的读字符流的类,主要用于读文本类型的文件
- readLine() 读取一行内容,如果读到文件结尾,则返回null
public class BufferedReaderDemo {
public static void main(String[] args) {
FileReader reader = null;
BufferedReader br = null;
try {
reader = new FileReader("E:\\Desktop\\备用.txt");
br = new BufferedReader(reader);
//如果文件有内容,返回当前的内容,如果读到文件结束,返回null
String line = br.readLine();
while(line != null){
System.out.println(line);
line = br.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (br != null){
try{
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
BufferedWrite
带有缓存的写文本内容的类,用于写纯文本的文件
- write()写字符串
- newLine //回车换行
(如果文本文件中的每行的内容用逗号分隔,文件名命名为.csv,这种文件可以用xce直接打开显示为表格)
public class BufferedWriterDemo {
public static void main(String[] args) {
FileWriter writer = null;
BufferedWriter bw = null;
try {
writer = new FileWriter("E:\\Desktop\\备用.txt");
bw = new BufferedWriter(writer);
bw.write("张三,22,男");
bw.newLine();//回车换行
bw.write("李四,22,男");
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (writer != null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}