问题: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;//一次读取一个字节,返回值就是本次读到的那个字节数据//也就是字符在码表中对应的那个数字,//如果我们想要看到的是字符数据,那么一定要强转成charSystem.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读不到数据返回nullwhile ((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;}@Overridepublic 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();}
}

Java面试知识点:File、IO流相关推荐

  1. Java面试知识点:红黑树、Stream流

    问题:Java面试知识点:红黑树.Stream流 答案: 1.红黑树 红黑树   • 平衡二叉B树   • 每一个节点可以是红或者黑   • 红黑树不是高度平衡的,它的平衡是通过"自己的红黑 ...

  2. 2019年秋招 Java 面试知识点梳理(高频问题)

    Java 面试知识点梳理 基础一 JVM-内存区域分配 HotSpot 对象创建 JVM-类加载机制 JVM-内存分配(堆上的内存分配) JVM-GC回收机制 JVM-垃圾收集器 JVM指令重排序 重 ...

  3. Java面试知识点:网络编程

    问题:Java面试知识点:网络编程 答案: 1.InetAddress 代码如下: package com.xy;import java.net.InetAddress; import java.ne ...

  4. Java 面试知识点解析——网络协议篇

    Java 面试知识点解析--网络协议篇 前言: 在遨游了一番 Java Web 的世界之后,发现了自己的一些缺失,所以就着一篇深度好文:知名互联网公司校招 Java 开发岗面试知识点解析 ,来好好的对 ...

  5. 打怪升级之小白的大数据之旅(二十五)<Java面向对象进阶之IO流三 其他常见流>

    打怪升级之小白的大数据之旅(二十五) Java面向对象进阶之IO流三 其他常见流 上次回顾 上一章,我们学习了常用的字节流与字符流,本章,我会将其他的一些常见的流进行分享,IO流很多,我介绍不完,就挑 ...

  6. Java 面试知识点解析(七)——Web篇

    前言: 在遨游了一番 Java Web 的世界之后,发现了自己的一些缺失,所以就着一篇深度好文:知名互联网公司校招 Java 开发岗面试知识点解析 ,来好好的对 Java 知识点进行复习和学习一番,大 ...

  7. Java 面试知识点解析(六)——数据库篇

    前言: 在遨游了一番 Java Web 的世界之后,发现了自己的一些缺失,所以就着一篇深度好文:知名互联网公司校招 Java 开发岗面试知识点解析 ,来好好的对 Java 知识点进行复习和学习一番,大 ...

  8. Java 面试知识点解析(五)——网络协议篇

    前言: 在遨游了一番 Java Web 的世界之后,发现了自己的一些缺失,所以就着一篇深度好文:知名互联网公司校招 Java 开发岗面试知识点解析 ,来好好的对 Java 知识点进行复习和学习一番,大 ...

  9. Java 面试知识点解析(二)——高并发编程篇

    前言: 在遨游了一番 Java Web 的世界之后,发现了自己的一些缺失,所以就着一篇深度好文:知名互联网公司校招 Java 开发岗面试知识点解析 ,来好好的对 Java 知识点进行复习和学习一番,大 ...

最新文章

  1. 思科快速转发机制(CEF-Cisco Express Forwarding)
  2. java复制一个对象_Java中对象的复制
  3. Mac上的Spotlight搜索都能干什么?
  4. 如何解决Mac启动盘已满的问题?MacBooster帮你清理20种垃圾文件!
  5. 【已解决】java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
  6. 用Python寻找最优投资组合
  7. 企业局域网无线组网方案
  8. 利用Cookie进行自动登录
  9. Cobbler自动化批量安装Linux操作系统
  10. Android实现用户圆形头像+背景模糊
  11. SybilFuse:Combining Local Attributes with Global Structure to Perform Robust Sybil Detect(论文笔记)
  12. 关于导入.a文件后报错Undefined symbols for architecture arm64:
  13. 三星emmc芯片手册_eMMC详细介绍
  14. SaaS企业如何降低客户流失率?
  15. Android Studio 基础 之 简单获取手机端所有图片
  16. OpenCV 4.2.0 + opencv_contrib安装 (ubuntu16.04)
  17. Dux主题 V6.4 新增百度收录
  18. win10显示我的电脑图标
  19. 三丰三坐标编程基本步骤_女式基本西装领的纸样设计步骤三
  20. 后端 绘制有向无环图(DAG图)

热门文章

  1. 每日一题(41)—— 数组和链表的区别
  2. 全国计算机等级考试暨南大学,9月暨南大学计算机等级考试报名时间通知
  3. android导出apk文件_Android测试工具入门介绍(三)
  4. matalotlib(2)
  5. Netty之自定义RPC
  6. LeetCode 2141. 同时运行 N 台电脑的最长时间(二分查找)
  7. 阿里云 超级码力在线编程大赛初赛 第4场 题目3. from start to end
  8. LeetCode 624. 数组列表中的最大距离
  9. LeetCode 1325. 删除给定值的叶子节点(递归)
  10. LeetCode 1377. T 秒后青蛙的位置(BFS)