问题:Java面试知识点:File、IO流
答案:
1.File
listFiles方法注意事项:
• 当调用者不存在时,返回null
• 当调用者是一个文件时,返回null
• 当调用者是一个空文件夹时,返回一体度为0的数组
• 当调用者是一个有内容的文件夹时,将里面所有文件和文件夹的路径放在File数组中返回
• 当调用者是一个有隐藏文件的文件夹时,将里面所有文件和文件夹的路径放在File数组中返回,包含隐藏内容
• 当调用者是一个需要权限才能进入的文件夹时,返回null
代码如下:
package com.xy;
import java.io.File;
/**
* @ProjectName: day01
* @Package: com.xy
* @ClassName: test01
* @Author: 杨路恒
* @Description:
* @Date: 2021/8/23 0023 16:02
* @Version: 1.0
*/
public class test01 {
public static void main(String[] args) {
String path="D:\\Java\\2021Java传智播客\\09_第九章 IO流-V10.0\\01_File";
File file=new File(path);
System.out.println(file);
String path1="D:\\Java\\2021Java传智播客\\09_第九章 IO流-V10.0";
String path2="01_File";
File file1=new File(path1,path2);
System.out.println(file1);
File file2=new File("D:\\Java\\2021Java传智播客\\09_第九章 IO流-V10.0");
File file3=new File(file2,path2);
System.out.println(file3);
}
}
public class test02 {
public static void main(String[] args) throws IOException {
File file=new File("day07\\a.txt");
boolean newFile = file.createNewFile();
System.out.println(newFile);
File file1=new File("day07\\aaa");
boolean mkdir = file1.mkdir(); //创建一个单级文件夹,
// 不管调用者有没有后缀名,只能创建单击文件夹
System.out.println(mkdir);
boolean mkdirs = file1.mkdirs(); //创建一个多级文件夹
System.out.println(mkdirs);
}
}
public class test03 {
public static void main(String[] args) {
File file=new File("D:\\Java\\2021Java传智播客\\09_第九章 IO流-V10.0" +
"\\01_File\\a.txt");
boolean delete = file.delete();
System.out.println(delete); //如果删除的是文件,那么直接删除,如果删除的是文件夹,
// 那么能删除空文件夹,如果要删除一个有内容的文件夹,只能先进入到这个文件夹,
// 把里面的内容全部删除完毕,才能再次删除这个文件夹
//简单来说:只能删除文件和空文件夹
}
}
public class test04 {
public static void main(String[] args) {
File file=new File("D:\\Java\\2021Java传智播客\\09_第九章 IO流-V10.0" +
"\\01_File\\06-File的获取和判断方法.flv");
boolean b = file.isDirectory(); //判断是否为文件夹
System.out.println(b);
boolean b1 = file.isFile(); //判断是否为文件
System.out.println(b1);
boolean b2 = file.exists(); //判断文件是否存在
System.out.println(b2);
String name = file.getName(); //获取文件或目录的名称
System.out.println(name);
File file1=new File("D:\\");
File[] files = file1.listFiles();
for (File file2 : files) {
System.out.println(file2);
}
//进入文件夹,获取这个文件夹里面所有的文件和文件夹的File对象,并把这些File对象都放在一个数组
// 中返回,包括隐藏文件和隐藏文件夹都可以获取
//注意事项:
//1.当调用者是一个文件时
//2.当调用者是一个空文件夹时
//3.当调用者是一个有内容的文件夹时
//4.当调用者是一个有权限才能进入的文件夹时
}
}
public class test05 {
public static void main(String[] args) throws IOException {
File file=new File("day07\\aaa");
file.mkdirs();
File file1=new File(file,"a.txt");
boolean newFile = file1.createNewFile();
System.out.println(newFile);
}
}
public class test06 {
public static void main(String[] args) {
File file=new File("D:\\Java\\2021Java传智播客\\09_第九章 IO流-V10.0\\" +
"01_File\\a");
// deleteFile(file);
HashMap<String,Integer> hashMap=new HashMap<>();
count(new File("D:\\LeetCode"),hashMap);
String s="a.txt";
String[] split = s.split("\\.");
for (String s1 : split) {
System.out.println(s1);
}
System.out.println(hashMap);
}
public static void deleteFile(File file){
if (file==null){
return;
}
File[] files = file.listFiles();
for (File file1 : files) {
if (file1.isFile()){
file1.delete();
continue;
}
else {
deleteFile(file1);
}
}
file.delete();
}
public static void count(File file,HashMap<String,Integer> hashMap){
File[] files = file.listFiles();
if (files==null){
return;
}
for (File file1 : files) {
if (file1.isFile()){
System.out.println(file1.getName());
if (file1.getName().split("\\.").length<2){
continue;
}
String s = file1.getName().split("\\.")[1];
System.out.println(s);
hashMap.put(s,hashMap.getOrDefault(s,0)+1);
}
else {
count(file1,hashMap);
}
}
}
}
2.IO流
(1)字节流
代码如下:
package com.xy;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @ProjectName: day01
* @Package: com.xy
* @ClassName: test08字节流
* @Author: 杨路恒
* @Description:
* @Date: 2021/8/24 0024 10:51
* @Version: 1.0
*/
public class test08字节流 {
public static void main(String[] args) throws IOException {
//第二个参数就是续写开关,如果没有传递,默认就是false,
// 表示不打开续写功能,那么创建对象的这行代码会清空文件
// FileOutputStream fos=new FileOutputStream("day07\\b.txt",true);
//如果第二个参数为true,表示打开续写功能
//那么创建对象的这行代码不会清空文件
FileOutputStream fos = null;
try {
fos = new FileOutputStream("day07\\b.txt");
byte[] bytes = {6, 66, 66};
String s = "\r\n";
fos.write(bytes);
// fos.write(bytes,1,2);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public class test09字节流 {
public static void main(String[] args) throws IOException {
//如果文件存在,那么就不会报错。
//如果文件不存在,那么就直接报错。
FileInputStream fis=new FileInputStream("day07\\b.txt");
int read =0;
//一次读取一个字节,返回值就是本次读到的那个字节数据
//也就是字符在码表中对应的那个数字,
//如果我们想要看到的是字符数据,那么一定要强转成char
System.out.println(read);
System.out.println((char)read);
while ((read=fis.read())!=-1){
System.out.println((char)read);
//一次读取一个字节,返回值就是本次读到的那个字节数据
//也就是字符在码表中对应的那个数字,
//如果我们想要看到的是字符数据,那么一定要强转成char
}
fis.close();
}
}
public class test10字节流 {
public static void main(String[] args) throws IOException {
FileInputStream fis=null;
FileOutputStream fos=null;
try {
fis=new FileInputStream("C:\\Users\\Administrator\\Pictures" +
"\\Saved Pictures\\1.jpg");
fos=new FileOutputStream("day07\\1.jpg");
int read=0;
while ((read=fis.read())!=-1){
fos.write(read);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis!=null){
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.xy;
import java.io.*;
/**
* @ProjectName: day01
* @Package: com.xy
* @ClassName: test12字节缓冲流
* @Author: 杨路恒
* @Description:
* @Date: 2021/8/24 0024 15:12
* @Version: 1.0
*/
public class test12字节缓冲流 {
public static void main(String[] args) throws IOException {
BufferedInputStream bis=null; //在底层创建了一个默认长度为8192的字节数组。
BufferedOutputStream bos=null; //在底层也创建了一个默认长度为8192的字节数组。
long l = System.currentTimeMillis();
try {
bis=new BufferedInputStream(new FileInputStream("D:" +
"\\360安全浏览器下载\\" +
"CentOS-7-x86_64-DVD-1804.iso"));
bos=new BufferedOutputStream(new FileOutputStream("" +
"day07\\1.iso"));
System.out.println("开始拷贝数据");
int length=0;
while ((length=bis.read())!=-1){
bos.write(length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (bis!=null){
try {
//方法的底层会把字节流给关闭。
bis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long l1 = System.currentTimeMillis();
System.out.println("结束,时间"+(l1-l));
}
}
package com.xy;
import java.io.*;
/**
* @ProjectName: day01
* @Package: com.xy
* @ClassName: test12字节缓冲流
* @Author: 杨路恒
* @Description:
* @Date: 2021/8/24 0024 15:12
* @Version: 1.0
*/
public class test13字节缓冲流 {
public static void main(String[] args) throws IOException {
BufferedInputStream bis=null; //在底层创建了一个默认长度为8192的字节数组。
BufferedOutputStream bos=null; //在底层也创建了一个默认长度为8192的字节数组。
long l = System.currentTimeMillis();
try {
bis=new BufferedInputStream(new FileInputStream("D:" +
"\\360安全浏览器下载\\" +
"CentOS-7-x86_64-DVD-1804.iso"));
bos=new BufferedOutputStream(new FileOutputStream("" +
"day07\\1.iso"));
System.out.println("开始拷贝数据");
int length=0;
byte[] bytes=new byte[1024];
while ((length=bis.read(bytes))!=-1){
bos.write(bytes,0,length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (bis!=null){
try {
//方法的底层会把字节流给关闭。
bis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long l1 = System.currentTimeMillis();
System.out.println("结束,时间"+(l1-l));
}
}
2.字符流
代码如下:
package com.xy;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
/**
* @ProjectName: day01
* @Package: com.xy
* @ClassName: test14字符流
* @Author: 杨路恒
* @Description:
* @Date: 2021/8/24 0024 15:41
* @Version: 1.0
*/
public class test14字符流 {
public static void main(String[] args) throws UnsupportedEncodingException {
String s="手可摘星辰";
byte[] bytes = s.getBytes();
System.out.println(Arrays.toString(bytes));
for (byte aByte : bytes) {
System.out.println(aByte);
}
System.out.println("************");
byte[] bytes1 = s.getBytes("GBK");
System.out.println(Arrays.toString(bytes1));
for (byte b : bytes1) {
System.out.println(b);
}
byte[] b1={-26, -119, -117, -27, -113, -81, -26, -111, -104,
-26, -104, -97, -24, -66, -80};
byte[] b2={-54, -42, -65, -55, -43, -86, -48, -57, -77, -67};
String s1=new String(b1); //利用默认的UTF-8进行解码
System.out.println(s1);
String s2=new String(b2,"gbk"); //利用GBK进行解码
System.out.println(s2);
}
}
public class test15字符流 {
public static void main(String[] args) throws IOException {
//创建字符输出流的对象
// FileWriter fw=new FileWriter(new File("day07\\c.txt"));
FileWriter fw=new FileWriter("day07\\c.txt");
fw.write(6);
char[] bytes={6,66,66};
fw.write(bytes,0,2);
fw.write("手可摘星辰");
// fw.flush(); //刷新流。刷新完毕之后,还可以继续写数据
fw.close(); //关闭流。释放资源。一旦关闭,就不能写数据
}
}
public class test16字符流 {
public static void main(String[] args) throws IOException {
//创建字符输入流对象
// FileReader fr=new FileReader(new File("day07\\c.txt"));
FileReader fr= null;
try {
fr = new FileReader("day07\\c.txt");
int length=0;
char[] bytes=new char[1024];
while ((length=fr.read(bytes))!=-1){
System.out.println(length);
System.out.println(new String(bytes,0,length));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr!=null){
fr.close();
}
}
}
}
package com.xy;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
* @ProjectName: day01
* @Package: com.xy
* @ClassName: test18字符缓冲输入流
* @Author: 杨路恒
* @Description:
* @Date: 2021/8/24 0024 19:23
* @Version: 1.0
*/
public class test18字符缓冲输入流 {
public static void main(String[] args) throws IOException {
BufferedReader br= null;
try {
br = new BufferedReader(new FileReader("day07\\d.txt"));
int length=0;
char[] chars=new char[1024];
System.out.println(br.readLine()); //一读读一整行,在之前,如果读不到数据,返回-1
//但是readLine读不到数据返回null
while ((length=br.read(chars))!=-1){
System.out.println(new String(chars,0,length));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
br.close();
}
}
}
public class test19字符缓冲输出流 {
public static void main(String[] args) throws IOException {
BufferedWriter bw= null;
try {
bw = new BufferedWriter(new FileWriter("day07\\1.txt"));
bw.write(6);
char[] chars={6,66};
bw.newLine(); //跨平台的回车换行
bw.write(chars);
bw.write("手可摘星辰");
} catch (IOException e) {
e.printStackTrace();
} finally {
bw.close();
}
}
}
3.对象操作流
代码如下:
public class test21转换流 {
public static void main(String[] args) throws IOException {
InputStreamReader isr=new InputStreamReader(new FileInputStream("" +
"day07\\d.txt"));
int length=0;
while ((length=isr.read())!=-1){
System.out.println((char) length);
}
isr.close();
OutputStreamWriter osw=new OutputStreamWriter(
new FileOutputStream("day07\\2.txt"));
osw.write("手可摘星辰");
osw.close();
}
}
public class test22对象操作流 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
User user=new User("杨大大","666");
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(
"day07\\3.txt"));
//Exception in thread "main" java.io.NotSerializableException: com.xy.User
//对象操作流对象要序列化
oos.writeObject(user);
oos.close();
//Exception in thread "main" java.io.InvalidClassException:
// com.xy.User; local class incompatible:
//如果我们修改了类中的信息,那么虚拟机会再次计算出一个序列号,把文件中的对象读到内存,本地中
//的序列号和类中的序列号不一致了。
//解决
//我们手动给出,而且这个值不要变
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("day07\\3.txt"));
User o = (User)ois.readObject();
while (true){
try {
User o1 = (User)ois.readObject();
} catch (IOException e) {
break;
}
}
System.out.println(o);
ois.close();
System.out.println(o.getName());
}
}
package com.xy;
import java.io.Serializable;
/**
* @ProjectName: day01
* @Package: com.xy
* @ClassName: User
* @Author: 杨路恒
* @Description:
* @Date: 2021/8/25 0025 11:05
* @Version: 1.0
*/
//如果想要这个类的对象能被序列化,那么这个类必须要实现一个接口.Serializable
//Serializable接口的意义
//称之为是一个标记性接口,里面没有任何的抽象方法
//只要一个类实现了这个Serializable接口,那么表示这个类的对象可以被序列化。
public class User implements Serializable {
private String name;
private transient String password;
private static final long serialVersionUID=1L;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(String name, String password) {
this.name = name;
this.password = password;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
public User() {
}
}
public class test23Properties {
public static void main(String[] args) {
Properties properties=new Properties();
properties.put("杨大大","恒大大");
System.out.println(properties);
properties.remove("杨大大");
System.out.println(properties);
properties.put("杨大大","恒大大");
String s = properties.getProperty("杨大大");
System.out.println(s);
Set<Object> objects = properties.keySet();
for (Object object : objects) {
System.out.println(object);
}
}
}
public class test24Properties {
public static void main(String[] args) throws IOException {
Properties prop=new Properties();
FileReader fr=new FileReader("day07\\prop.properties");
prop.load(fr); //调用完load方法之后,文件中的键值对数据已经在集合中了
fr.close();
System.out.println(prop);
prop.setProperty("杨大大","666");
FileWriter fw=new FileWriter("day07\\prop1.properties");
prop.store(fw,"");
fw.close();
}
}