Java面试知识点:File、IO流
生活随笔
收集整理的這篇文章主要介紹了
Java面试知识点:File、IO流
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題: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流的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 9 操作系统第二章 进程管理 管程
- 下一篇: Java面试题:线程实现的两种方式及匿名