目录
File(String parent, String child)
File(File parent, String child)
public boolean createNewFile()
File类
1.File类概述以及构造方法
概述
-
它是文件和目录路径名的抽象表示
-
文件和目录是可以通过File封装成对象的
-
对于File而言,其封装的并不是一个真正存在的文件,仅仅是一个路径名而已。它可以是存在的,也可以是不存在的。将来是要通过具体的操作把这个路径的内容转换为具体存在的
构造方法
方法名 | 说明 |
---|---|
File(String pathname) | 通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例 |
File(String parent, String child) | 从父路径名字符串和子路径名字符串创建新的 File实例 |
File(File parent, String child) | 从父抽象路径名和子路径名字符串创建新的 File实例 |
实例
File(String pathname)
File f1 = new File("E:\\itcast\\java.txt");
File(String parent, String child)
File f2 = new File("E:\\itcast","java.txt");
File(File parent, String child)
File f3 = new File("E:\\itcast");
File f4 = new File(f3,"java.txt");
2.File类创建功能
方法名 | 说明 |
---|---|
public boolean createNewFile() | 当具有该名称的文件不存在时,创建一个由该抽象路径名命名的新空文件 |
public boolean mkdir() | 创建由此抽象路径名命名的目录 |
public boolean mkdirs() | 创建由此抽象路径名命名的目录,包括任何必需但不存在的父目录 |
public boolean createNewFile()
File f1 = new File("E:\\itcast\\java.txt");
f1.createNewFile();
public boolean mkdir()
File f2 = new File("E:\\itcast\\JavaSE");
f2.mkdir();
public boolean mkdirs()
File f3 = new File("E:\\itcast\\JavaWEB\\HTML");
System.out.println(f3.mkdirs());
3.File类判断和获取功能
-
判断功能
方法名 说明 public boolean isDirectory() 测试此抽象路径名表示的File是否为目录 public boolean isFile() 测试此抽象路径名表示的File是否为文件 public boolean exists() 测试此抽象路径名表示的File是否存在 -
获取功能
方法名 说明 public String getAbsolutePath() 返回此抽象路径名的绝对路径名字符串 public String getPath() 将此抽象路径名转换为路径名字符串 public String getName() 返回由此抽象路径名表示的文件或目录的名称 public String[] list() 返回此抽象路径名表示的目录中的文件和目录的名称字符串数组 public File[] listFiles() 返回此抽象路径名表示的目录中的文件和目录的File对象数组
File f = new File("myFile\\java.txt");
System.out.println(f.isDirectory());
System.out.println(f.isFile());
System.out.println(f.exists());
File f = new File("myFile\\java.txt");
System.out.println(f.getAbsolutePath());
System.out.println(f.getPath());
System.out.println(f.getName());
System.out.println("--------");
File f2 = new File("E:\\itcast");
String[] strArray = f2.list();
for(String str : strArray) {
System.out.println(str);
}
System.out.println("--------");
File[] fileArray = f2.listFiles();
for(File file : fileArray) {
// System.out.println(file);
// System.out.println(file.getName());
if(file.isFile()) {
System.out.println(file.getName());
}
}
4.File类删除功能
方法名 | 说明 |
---|---|
public boolean delete() | 删除由此抽象路径名表示的文件或目录 |
File f3 = new File("myFile\\itcast");
f3.mkdir();
f3.delete();
5.递归遍历目录
//根据给定的路径创建一个File对象
// File srcFile = new File("E:\\itcast");
File srcFile = new File("E:\\itheima");
//调用方法
getAllFilePath(srcFile);
}
//定义一个方法,用于获取给定目录下的所有内容,参数为第1步创建的File对象
public static void getAllFilePath(File srcFile) {
//获取给定的File目录下所有的文件或者目录的File数组
File[] fileArray = srcFile.listFiles();
//遍历该File数组,得到每一个File对象
if(fileArray != null) {
for(File file : fileArray) {
//判断该File对象是否是目录
if(file.isDirectory()) {
//是:递归调用
getAllFilePath(file);
} else {
//不是:获取绝对路径输出在控制台
System.out.println(file.getAbsolutePath());
}
}
}
IO流
1.概述和分类
概述
-
IO:输入/输出(Input/Output)
-
流:是一种抽象概念,是对数据传输的总称。也就是说数据在设备间的传输称为流,流的本质是数据传输
-
IO流就是用来处理设备间数据传输问题的。常见的应用:文件复制;文件上传;文件下载
分类
-
按照数据的流向
-
输入流:读数据
-
输出流:写数据
-
-
按照数据类型来分
-
字节流(如果操作的是图片、视频、音频等二进制文件。优先使用字节流 。 如果不确定文件类型,优先使用字节流。字节流是万能的流 )
-
字节输入流
-
字节输出流
-
-
字符流(如果操作的是纯文本文件,优先使用字符流 )
-
字符输入流
-
字符输出流
-
-
2.字节流
1.字节流写数据
方法名 | 说明 |
---|---|
void write(int b) | 将指定的字节写入此文件输出流 一次写一个字节数据 |
void write(byte[] b) | 将 b.length字节从指定的字节数组写入此文件输出流 一次写一个字节数组数据 |
void write(byte[] b, int off, int len) | 将 len字节从指定的字节数组开始,从偏移量off开始写入此文件输出流 一次写一个字节数组的部分数据 |
FileOutputStream fos = new FileOutputStream("myByteStream\\fos.txt");
fos.write(97); //void write(int b)
byte[] bys = "abcde".getBytes();// void write(byte[] b)
fos.write(bys);
fos.write(bys,1,3);//void write(byte[] b, int off, int len)
//释放资源
fos.close();
字节流写数据的两个小问题
-
字节流写数据如何实现换行
-
windows:\r\n
-
linux:\n
-
mac:\r
-
-
字节流写数据如何实现追加写入
-
public FileOutputStream(String name,boolean append)
-
创建文件输出流以指定的名称写入文件。如果第二个参数为true ,则字节将写入文件的末尾而不是开头
-
//创建字节输出流对象
// FileOutputStream fos = new FileOutputStream("myByteStream\\fos.txt");
FileOutputStream fos = new FileOutputStream("myByteStream\\fos.txt",true);
//写数据
for (int i = 0; i < 10; i++) {
fos.write("hello".getBytes());
fos.write("\r\n".getBytes());
}
//释放资源
fos.close();
2.字节流读数据
FileInputStream fis = new FileInputStream("myByteStream\\fos.txt");
//一次读一个字节
int by;
while ((by=fis.read())!=-1) {
System.out.print((char)by);
}
//一次读一个字节数组
byte[] bys = new byte[1024]; //1024及其整数倍
int len;
while ((len=fis.read(bys))!=-1) {
System.out.print(new String(bys,0,len));
}
//释放资源
fis.close();
3.字节缓冲流
方法名 | 说明 |
---|---|
BufferedOutputStream(OutputStream out) | 创建字节缓冲输出流对象 |
BufferedInputStream(InputStream in) | 创建字节缓冲输入流对象 |
//字节缓冲输出流:BufferedOutputStream(OutputStream out)
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("myByteStream\\bos.txt"));
bos.write("hello\r\n".getBytes());
bos.write("world\r\n".getBytes());
//释放资源
bos.close();
//字节缓冲输入流:BufferedInputStream(InputStream in)
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("myByteStream\\bos.txt"));
//一次读取一个字节数据
// int by;
// while ((by=bis.read())!=-1) {
// System.out.print((char)by);
// }
//一次读取一个字节数组数据
byte[] bys = new byte[1024];
int len;
while ((len=bis.read(bys))!=-1) {
System.out.print(new String(bys,0,len));
}
//释放资源
bis.close();
4.字节流和字节缓冲流复制视频
//字节流复制视频
FileInputStream fis = new FileInputStream("E:\\itcast\\字节流复制图片.avi");
FileOutputStream fos = new FileOutputStream("myByteStream\\字节流复制图片.avi");
//基本字节流一次读写一个字节
int by;
while ((by=fis.read())!=-1) {
fos.write(by);
}
fos.close();
fis.close();
//基本字节流一次读写一个字节数组
byte[] bys = new byte[1024];
int len;
while ((len=fis.read(bys))!=-1) {
fos.write(bys,0,len);
}
fos.close();
fis.close();
//字节缓冲流复制视频
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\itcast\\字节流复制图片.avi"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("myByteStream\\字节流复制图片.avi"));
//字节缓冲流一次读写一个字节数组
byte[] bys = new byte[1024];
int len;
while ((len=bis.read(bys))!=-1) {
bos.write(bys,0,len);
}
bos.close();
bis.close();
//字节缓冲流一次读写一个字节
int by;
while ((by=bis.read())!=-1) {
bos.write(by);
}
bos.close();
bis.close();
3.字符流
1.为什么会出现字符流
由于字节流操作中文不是特别的方便,所以Java就提供字符流
字符流 = 字节流 + 编码表
2.编码表
ASCII字符集、GBXXX字符集、UTF-8编码
3.字符串中的编码解码问题
方法名 | 说明 |
---|---|
byte[] getBytes() | 使用平台的默认字符集将该 String编码为一系列字节 |
byte[] getBytes(String charsetName) | 使用指定的字符集将该 String编码为一系列字节 |
String(byte[] bytes) | 使用平台的默认字符集解码指定的字节数组来创建字符串 |
String(byte[] bytes, String charsetName) | 通过指定的字符集解码指定的字节数组来创建字符串 |
String s = "中国";
byte[] bys = s.getBytes();//byte[] getBytes()
byte[] bys = s.getBytes("UTF-8"); //byte[] getBytes(String charsetName)
String ss = new String(bys);//String(byte[] bytes)
String ss = new String(bys,"UTF-8");//String(byte[] bytes, String charsetName)
4.字符流中的编码解码问题
方法名 | 说明 |
---|---|
InputStreamReader(InputStream in) | 使用默认字符编码创建InputStreamReader对象 |
InputStreamReader(InputStream in,String chatset) | 使用指定的字符编码创建InputStreamReader对象 |
OutputStreamWriter(OutputStream out) | 使用默认字符编码创建OutputStreamWriter对象 |
OutputStreamWriter(OutputStream out,String charset) | 使用指定的字符编码创建OutputStreamWriter对象 |
//OutputStreamWriter(OutputStream out)使用默认字符编码创建OutputStreamWriter对象
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("myCharStream\\osw.txt"));
//OutputStreamWriter(OutputStream out,String charset)使用指定的字符编码创建OutputStreamWriter对象
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("myCharStream\\osw.txt"),"GBK");
osw.write("中国");
osw.close();
//InputStreamReader(InputStream in)使用默认字符编码创建InputStreamReader对象
InputStreamReader isr = new InputStreamReader(new FileInputStream("myCharStream\\osw.txt"));
//InputStreamReader(InputStream in,String chatset)使用指定的字符编码创建InputStreamReader对象
InputStreamReader isr = new InputStreamReader(new FileInputStream("myCharStream\\osw.txt"),"GBK");
//一次读取一个字符数据
int ch;
while ((ch=isr.read())!=-1) {
System.out.print((char)ch);
}
isr.close();
5.字符流写数据
方法名 | 说明 |
---|---|
void write(int c) | 写一个字符 |
void write(char[] cbuf) | 写入一个字符数组 |
void write(char[] cbuf, int off, int len) | 写入字符数组的一部分 |
void write(String str) | 写一个字符串 |
void write(String str, int off, int len) | 写一个字符串的一部分 |
flush() | 刷新流,之后还可以继续写数据 |
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("myCharStream\\osw.txt"));
//void write(int c):写一个字符
osw.write(97);
//void writ(char[] cbuf):写入一个字符数组
char[] chs = {'a', 'b', 'c', 'd', 'e'};
osw.write(chs);
//void write(char[] cbuf, int off, int len):写入字符数组的一部分
osw.write(chs, 0, chs.length);
osw.write(chs, 1, 3);
//void write(String str):写一个字符串
osw.write("abcde");
//void write(String str, int off, int len):写一个字符串的一部分
osw.write("abcde", 1, 3);
//释放资源
osw.close();
6.字符流读数据
方法名 | 说明 |
---|---|
int read() | 一次读一个字符数据 |
int read(char[] cbuf) | 一次读一个字符数组数据 |
InputStreamReader isr = new InputStreamReader(new FileInputStream("myCharStream\\ConversionStreamDemo.java"));
//int read():一次读一个字符数据
int ch;
while ((ch=isr.read())!=-1) {
System.out.print((char)ch);
}
//int read(char[] cbuf):一次读一个字符数组数据
char[] chs = new char[1024];
int len;
while ((len = isr.read(chs)) != -1) {
System.out.print(new String(chs, 0, len));
}
//释放资源
isr.close();
7.字符缓冲流
方法名 | 说明 |
---|---|
BufferedWriter(Writer out) | 创建字符缓冲输出流对象 |
BufferedReader(Reader in) | 创建字符缓冲输入流对象 |
//BufferedWriter(Writer out)
BufferedWriter bw = new BufferedWriter(new FileWriter("myCharStream\\bw.txt"));
bw.write("hello\r\n");
bw.write("world\r\n");
bw.close();
//BufferedReader(Reader in)
BufferedReader br = new BufferedReader(new FileReader("myCharStream\\bw.txt"));
//一次读取一个字符数据
// int ch;
// while ((ch=br.read())!=-1) {
// System.out.print((char)ch);
// }
//一次读取一个字符数组数据
char[] chs = new char[1024];
int len;
while ((len=br.read(chs))!=-1) {
System.out.print(new String(chs,0,len));
}
br.close();
8.字符缓冲流特有功能
方法介绍
BufferedWriter:
方法名 | 说明 |
---|---|
void newLine() | 写一行行分隔符,行分隔符字符串由系统属性定义 |
BufferedReader:
方法名 | 说明 |
---|---|
String readLine() | 读一行文字。 结果包含行的内容的字符串,不包括任何行终止字符如果流的结尾已经到达,则为null |
//创建字符缓冲输出流
BufferedWriter bw = new BufferedWriter(new FileWriter("myCharStream\\bw.txt"));
//写数据
for (int i = 0; i < 10; i++) {
bw.write("hello" + i);
//bw.write("\r\n");
bw.newLine();
bw.flush();
}
//释放资源
bw.close();
//创建字符缓冲输入流
BufferedReader br = new BufferedReader(new FileReader("myCharStream\\bw.txt"));
String line;
while ((line=br.readLine())!=null) {
System.out.println(line);
}
br.close();
IO小结图片
文件与集合相互转换
1.文件到集合
//创建ArrayList集合
ArrayList<String> array = new ArrayList<String>();
//往集合中存储字符串元素
array.add("hello");
array.add("world");
array.add("java");
//创建字符缓冲输出流对象
BufferedWriter bw = new BufferedWriter(new FileWriter("myCharStream\\array.txt"));
//遍历集合,得到每一个字符串数据
for(String s : array) {
//调用字符缓冲输出流对象的方法写数据
bw.write(s);
bw.newLine();
bw.flush();
}
//释放资源
bw.close();
2.集合到文件
//创建字符缓冲输入流对象
BufferedReader br = new BufferedReader(new FileReader("myCharStream\\array.txt"));
//创建ArrayList集合对象
ArrayList<String> array = new ArrayList<String>();
//调用字符缓冲输入流对象的方法读数据
String line;
while ((line=br.readLine())!=null) {
//把读取到的字符串数据存储到集合中
array.add(line);
}
//释放资源
br.close();
//遍历集合
for(String s : array) {
System.out.println(s);
}
3.文件到集合(改)
//创建字符缓冲输入流对象
BufferedReader br = new BufferedReader(new FileReader("myCharStream\\students.txt"));
//创建ArrayList集合对象
ArrayList<Student> array = new ArrayList<Student>();
//调用字符缓冲输入流对象的方法读数据
String line;
while ((line = br.readLine()) != null) {
//把读取到的字符串数据用split()进行分割,得到一个字符串数组
String[] strArray = line.split(",");
//创建学生对象
Student s = new Student();
//把字符串数组中的每一个元素取出来对应的赋值给学生对象的成员变量值
//itheima001,林青霞,30,西安
s.setSid(strArray[0]);
s.setName(strArray[1]);
s.setAge(Integer.parseInt(strArray[2]));
s.setAddress(strArray[3]);
//把学生对象添加到集合
array.add(s);
}
//释放资源
br.close();
//遍历集合
for (Student s : array) {
System.out.println(s.getSid() + "," + s.getName() + "," + s.getAge() + "," + s.getAddress());
}
4.集合到文件(改)
//创建ArrayList集合
ArrayList<Student> array = new ArrayList<Student>();
//创建学生对象
Student s1 = new Student("itheima001", "林青霞", 30, "西安");
Student s2 = new Student("itheima002", "张曼玉", 35, "武汉");
Student s3 = new Student("itheima003", "王祖贤", 33, "郑州");
//把学生对象添加到集合中
array.add(s1);
array.add(s2);
array.add(s3);
//创建字符缓冲输出流对象
BufferedWriter bw = new BufferedWriter(new FileWriter("myCharStream\\students.txt"));
//遍历集合,得到每一个学生对象
for (Student s : array) {
//把学生对象的数据拼接成指定格式的字符串
StringBuilder sb = new StringBuilder();
sb.append(s.getSid()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddress());
//调用字符缓冲输出流对象的方法写数据
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
//释放资源
bw.close();
Properties类
1.Properties作为Map集合的使用
Properties prop = new Properties();
//存储元素
prop.put("itheima001", "林青霞");
prop.put("itheima002", "张曼玉");
prop.put("itheima003", "王祖贤");
//遍历集合
Set<Object> keySet = prop.keySet();
for (Object key : keySet) {
Object value = prop.get(key);
System.out.println(key + "," + value);
}
2.Properties作为Map集合的特有方法
方法名 | 说明 |
---|---|
Object setProperty(String key, String value) | 设置集合的键和值,都是String类型,底层调用 Hashtable方法 put |
String getProperty(String key) | 使用此属性列表中指定的键搜索属性 |
Set<String> stringPropertyNames() | 从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串 |
//创建集合对象
Properties prop = new Properties();
//Object setProperty(String key, String value):设置集合的键和值,都是String类型,底层调用Hashtable方法put
prop.setProperty("itheima001", "林青霞");
/*
Object setProperty(String key, String value) {
return put(key, value);
}
Object put(Object key, Object value) {
return map.put(key, value);
}
*/
prop.setProperty("itheima002", "张曼玉");
prop.setProperty("itheima003", "王祖贤");
//String getProperty(String key):使用此属性列表中指定的键搜索属性
// System.out.println(prop.getProperty("itheima001"));
// System.out.println(prop.getProperty("itheima0011"));
// System.out.println(prop);
//Set<String> stringPropertyNames():从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串
Set<String> names = prop.stringPropertyNames();
for (String key : names) {
// System.out.println(key);
String value = prop.getProperty(key);
System.out.println(key + "," + value);
}
3.Properties和IO流相结合的方法
方法名 | 说明 |
---|---|
void load(InputStream inStream) | 从输入字节流读取属性列表(键和元素对) |
void load(Reader reader) | 从输入字符流读取属性列表(键和元素对) |
void store(OutputStream out, String comments) | 将此属性列表(键和元素对)写入此 Properties表中,以适合于使用 load(InputStream)方法的格式写入输出字节流 |
void store(Writer writer, String comments) | 将此属性列表(键和元素对)写入此 Properties表中,以适合使用 load(Reader)方法的格式写入输出字符流 |
//把集合中的数据保存到文件
// myStore();
//把文件中的数据加载到集合
myLoad();
}
private static void myLoad() throws IOException {
Properties prop = new Properties();
//void load(Reader reader):
FileReader fr = new FileReader("myOtherStream\\fw.txt");
prop.load(fr);
fr.close();
System.out.println(prop);
}
private static void myStore() throws IOException {
Properties prop = new Properties();
prop.setProperty("itheima001","林青霞");
prop.setProperty("itheima002","张曼玉");
prop.setProperty("itheima003","王祖贤");
//void store(Writer writer, String comments):
FileWriter fw = new FileWriter("myOtherStream\\fw.txt");
prop.store(fw,null);
fw.close();