做一个官方网站多少钱,全国网络公司大全,wordpress 安装地址,网络推广专员要求问题#xff1a;Java面试知识点#xff1a;File、IO流 答案#xff1a;
1.File listFiles方法注意事项: • 当调用者不存在时#xff0c;返回null • 当调用者是一个文件时#xff0c;返回null • 当调用者是一个空文件夹时#xff0c;返回一体度为0的数组 • 当调用者是…问题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 pathD:\\Java\\2021Java传智播客\\09_第九章 IO流-V10.0\\01_File;File filenew File(path);System.out.println(file);String path1D:\\Java\\2021Java传智播客\\09_第九章 IO流-V10.0;String path201_File;File file1new File(path1,path2);System.out.println(file1);File file2new File(D:\\Java\\2021Java传智播客\\09_第九章 IO流-V10.0);File file3new File(file2,path2);System.out.println(file3);}
}public class test02 {public static void main(String[] args) throws IOException {File filenew File(day07\\a.txt);boolean newFile file.createNewFile();System.out.println(newFile);File file1new 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 filenew 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 filenew 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 file1new 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 filenew File(day07\\aaa);file.mkdirs();File file1new File(file,a.txt);boolean newFile file1.createNewFile();System.out.println(newFile);}
}public class test06 {public static void main(String[] args) {File filenew File(D:\\Java\\2021Java传智播客\\09_第九章 IO流-V10.0\\ 01_File\\a);
// deleteFile(file);HashMapString,Integer hashMapnew HashMap();count(new File(D:\\LeetCode),hashMap);String sa.txt;String[] split s.split(\\.);for (String s1 : split) {System.out.println(s1);}System.out.println(hashMap);}public static void deleteFile(File file){if (filenull){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,HashMapString,Integer hashMap){File[] files file.listFiles();if (filesnull){return;}for (File file1 : files) {if (file1.isFile()){System.out.println(file1.getName());if (file1.getName().split(\\.).length2){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 fosnew 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 fisnew FileInputStream(day07\\b.txt);int read 0;//一次读取一个字节返回值就是本次读到的那个字节数据//也就是字符在码表中对应的那个数字//如果我们想要看到的是字符数据那么一定要强转成charSystem.out.println(read);System.out.println((char)read);while ((readfis.read())!-1){System.out.println((char)read);//一次读取一个字节返回值就是本次读到的那个字节数据//也就是字符在码表中对应的那个数字//如果我们想要看到的是字符数据那么一定要强转成char}fis.close();}
} public class test10字节流 {public static void main(String[] args) throws IOException {FileInputStream fisnull;FileOutputStream fosnull;try {fisnew FileInputStream(C:\\Users\\Administrator\\Pictures \\Saved Pictures\\1.jpg);fosnew FileOutputStream(day07\\1.jpg);int read0;while ((readfis.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 bisnull; //在底层创建了一个默认长度为8192的字节数组。BufferedOutputStream bosnull; //在底层也创建了一个默认长度为8192的字节数组。long l System.currentTimeMillis();try {bisnew BufferedInputStream(new FileInputStream(D: \\360安全浏览器下载\\ CentOS-7-x86_64-DVD-1804.iso));bosnew BufferedOutputStream(new FileOutputStream( day07\\1.iso));System.out.println(开始拷贝数据);int length0;while ((lengthbis.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 bisnull; //在底层创建了一个默认长度为8192的字节数组。BufferedOutputStream bosnull; //在底层也创建了一个默认长度为8192的字节数组。long l System.currentTimeMillis();try {bisnew BufferedInputStream(new FileInputStream(D: \\360安全浏览器下载\\ CentOS-7-x86_64-DVD-1804.iso));bosnew BufferedOutputStream(new FileOutputStream( day07\\1.iso));System.out.println(开始拷贝数据);int length0;byte[] bytesnew byte[1024];while ((lengthbis.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 s1new String(b1); //利用默认的UTF-8进行解码System.out.println(s1);String s2new String(b2,gbk); //利用GBK进行解码System.out.println(s2);}
}public class test15字符流 {public static void main(String[] args) throws IOException {//创建字符输出流的对象
// FileWriter fwnew FileWriter(new File(day07\\c.txt));FileWriter fwnew 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 frnew FileReader(new File(day07\\c.txt));FileReader fr null;try {fr new FileReader(day07\\c.txt);int length0;char[] bytesnew char[1024];while ((lengthfr.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 length0;char[] charsnew char[1024];System.out.println(br.readLine()); //一读读一整行在之前如果读不到数据返回-1//但是readLine读不到数据返回nullwhile ((lengthbr.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 isrnew InputStreamReader(new FileInputStream( day07\\d.txt));int length0;while ((lengthisr.read())!-1){System.out.println((char) length);}isr.close();OutputStreamWriter oswnew OutputStreamWriter(new FileOutputStream(day07\\2.txt));osw.write(手可摘星辰);osw.close();}
}public class test22对象操作流 {public static void main(String[] args) throws IOException, ClassNotFoundException {User usernew User(杨大大,666);ObjectOutputStream oosnew 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 oisnew 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 serialVersionUID1L;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 propertiesnew Properties();properties.put(杨大大,恒大大);System.out.println(properties);properties.remove(杨大大);System.out.println(properties);properties.put(杨大大,恒大大);String s properties.getProperty(杨大大);System.out.println(s);SetObject objects properties.keySet();for (Object object : objects) {System.out.println(object);}}
}public class test24Properties {public static void main(String[] args) throws IOException {Properties propnew Properties();FileReader frnew FileReader(day07\\prop.properties);prop.load(fr); //调用完load方法之后文件中的键值对数据已经在集合中了fr.close();System.out.println(prop);prop.setProperty(杨大大,666);FileWriter fwnew FileWriter(day07\\prop1.properties);prop.store(fw,);fw.close();}
}