活動地址:CSDN21天學習挑戰賽
🍁博客主頁:
👉@不會壓彎的小飛俠
?歡迎關注:
👉點贊
👍收藏
?留言
?
?系列專欄:
👉21天挑戰回顧java
?知足上進,不負野心。
🔥歡迎大佬指正,一起學習!一起加油!
方法名 | 說明 |
---|---|
File(String pathname) | 通過將給定的路徑名字符串轉換為抽象路徑名來創建新的File實例 |
File(String parent, String child) | 從父路徑名字符串和子路徑名字符串創建新的File實例 |
File(File parent, String child) | 從父抽象路徑名和子路徑名字符串創建新的File實例 |
案例分析:
package com.file;
import java.io.File;
public class Test1 {public static void main(String[] args) {File f1=new File("F://Test//java.txt");System.out.println(f1); //F:\Test\java.txtFile f2=new File("F://Test","java.txt");System.out.println(f2); //F:\Test\java.txtFile f3=new File("F://Test");File f4=new File(f3,"java.txt");System.out.println(f4); //F:\Test\java.txt}
}
方法名 | 說明 |
---|---|
public boolean createNewFile() | 當具有該名稱的文件不存在時,創建一個由該抽象路徑名命名的新空文件 |
public boolean mkdir() | 創建由此抽象路徑名命名的目錄 |
public boolean mkdirs() | 創建由此抽象路徑名命名的目錄,包括任何必需但不存在的父目錄 |
java的輸入輸出。案例分析:
package com.file;
import java.io.File;
import java.io.IOException;
public class Test2 {public static void main(String[] args) throws IOException {//在F盤的Test目錄下創建一個test.txt文件File f1=new File("F://Test//test.txt");System.out.println(f1.createNewFile()); //true//在F盤的Test目錄下創建一個Spring目錄File f2=new File("F://Test//Spring");System.out.println(f2.mkdir()); //true//在F盤的Test目錄下創建多個目錄File f3=new File("F://Test//MyBatis//SpringMVC");System.out.println(f3.mkdirs()); //true}
}
方法名 | 說明 |
---|---|
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對象數組 |
案例分析:
package com.file;import java.io.File;public class Test3 {public static void main(String[] args) {File f=new File("F:\\Test\\java.txt");System.out.println(f.isDirectory()); //falseSystem.out.println(f.isFile()); //trueSystem.out.println(f.exists()); //trueSystem.out.println(f.getAbsoluteFile()); //F:\Test\java.txtSystem.out.println(f.getPath()); //F:\Test\java.txtSystem.out.println(f.getName()); //java.txtFile f1=new File("F:\\Test");String[] list = f1.list();for (String s : list) {System.out.println(s); /*
a
b
java.txt
MyBatis
Spring
test.txt
TestOne
小飛俠
小馬哥*/}File[] files = f1.listFiles();for (File file : files) {System.out.println(file);
/*
F:\Test\a
F:\Test\b
F:\Test\java.txt
F:\Test\MyBatis
F:\Test\Spring
F:\Test\test.txt
F:\Test\TestOne
F:\Test\小飛俠
F:\Test\小馬哥 */}}
}
方法名 | 說明 |
---|---|
public boolean delete() | 刪除由此抽象路徑名表示的文件或目錄 |
E:\\study\\text.txt
study\\text.txt
案例分析:
package com.file;import java.io.File;
import java.io.IOException;public class Test4 {public static void main(String[] args) throws IOException {//在當前模塊下創建one.txt文件File f1=new File("E:\\JavaSE\\Test\\File\\one.txt"); //trueSystem.out.println(f1.createNewFile());//刪除當前模塊下創建one.txt文件System.out.println(f1.delete()); true//在當前模塊下創建one目錄File f2=new File("E:\\JavaSE\\Test\\File\\one");System.out.println(f2.mkdir()); true//刪除當前模塊下創建one目錄System.out.println(f2.delete()); true}
}
IO:輸入/輸出(Input/Output)
IO流就是用來處理設備間數據傳輸問題的
IO流分類:
Java數據輸入輸出的方法?按照數據類型來分
一般來說,IO流的分類是按照數據類型來分的
案例分析:
package com.outPutstream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Test1 {public static void main(String[] args) throws IOException {//創建字節輸出流對象FileOutputStream f = new FileOutputStream("E:\\JavaSE\\Test\\File\\outputStream.txt");//將字節寫入此文件輸出流f.write(97); //a//釋放資源f.close();}
}
方法名 | 說明 |
---|---|
void write(int b) | 將指定的字節寫入此文件輸出流,一次寫一個字節數據 |
void write(byte[] b) | 將b.length字節從指定的字節數組寫入此文件輸出流,一次寫一個字節數組數據 |
void write(byte[] b, int off, int len) | 將len字節從指定的字節數組開始,從偏移量off開始寫入此文件輸出流,一次寫一個字節數組的部分數據 |
案例分析:
package com.outPutstream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test2 {public static void main(String[] args) throws IOException {//創建字節輸出流對象FileOutputStream f = new FileOutputStream("E:\\JavaSE\\Test\\File\\outputStream.txt");//將字節寫入此文件輸出流/* f.write(97);f.write(98);f.write(99);f.write(100); */ //abcd//將指定的字節數組寫入此文件輸出流byte[] bytes={97,98,99,100};//f.write(bytes); //abcdf.write(bytes,1,2); //bc//釋放資源f.close();}
}
案例分析:
package com.outPutstream;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;public class Test3 {public static void main(String[] args) throws IOException {FileOutputStream f = new FileOutputStream("E:\\JavaSE\\Test\\File\\outputStream.txt",true);for (int i = 0; i <5 ; i++) {f.write("hello".getBytes());f.write("\r\n".getBytes());}f.close();}
}
//執行三次的結果
/*
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello*/
案例分析:
package com.FileInputStream;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;public class Test1 {public static void main(String[] args) throws IOException {//創建字節輸入流對象FileInputStream f = new FileInputStream("E:\\JavaSE\\Test\\File\\inputStream.txt");//讀取數據/*int by = f.read();System.out.println(by); //97System.out.println((char)by); //a*///一次只能讀取一個字節int by = f.read();while (by != -1){System.out.print((char) by); //abcby=f.read();}//釋放資源f.close();}
}
如果輸入輸出所用時間比處理時間,案例:
package com.FileInputStream;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;public class Test2 {public static void main(String[] args) throws IOException {FileInputStream f1 = new FileInputStream("E:\\JavaSE\\Test\\File\\inputStream.txt");FileOutputStream f2 = new FileOutputStream("E:\\JavaSE\\Test\\File\\outputStream.txt");int by = f1.read();while (by !=-1){f2.write((char)by);by=f1.read();}}
}
案例:
package com.FileInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test3 {public static void main(String[] args) throws IOException {FileInputStream f1 = new FileInputStream("E:\\JavaSE\\Test\\File\\inputStream.txt");byte[] bytes=new byte[1024];int by;by = f1.read(bytes);while (by !=-1){System.out.println(new String(bytes,0,by));by=f1.read(bytes);}}
}
/*
Java
JavaWeb
Python
Spring
Mybatis*/
案例:
package com.BufferStream;import java.io.*;public class Test1 {public static void main(String[] args) throws IOException {BufferedOutputStream b1 = new BufferedOutputStream(new FileOutputStream("E:\\JavaSE\\Test\\File\\buffer.txt"));b1.write("Hello".getBytes());b1.write("\r\t".getBytes());b1.write("World".getBytes());b1.close();BufferedInputStream b2 = new BufferedInputStream(new FileInputStream("E:\\JavaSE\\Test\\File\\buffer.txt"));int by = b2.read();while (by !=-1){System.out.print((char)by);by=b2.read();}b2.close();}
}
案例分析:
package com.zifu;import java.io.UnsupportedEncodingException;
import java.util.Arrays;public class Test1 {public static void main(String[] args) throws UnsupportedEncodingException {/*String s1="小馬哥";byte[] bytes = s1.getBytes();System.out.println(Arrays.toString(bytes)); //[-27, -80, -113, -23, -87, -84, -27, -109, -91]//String s=new String(bytes); //小馬哥String s=new String(bytes,"UTF-8"); //小馬哥System.out.println(s);*/String s2="小飛俠";byte[] bytes =s2.getBytes("GBK");System.out.println(Arrays.toString(bytes)); //[-48, -95, -73, -55, -49, -64]String s=new String(bytes,"GBK");System.out.println(s); //小飛俠}
}
案例分析:
package com.zifu;import java.io.*;public class Test2 {public static void main(String[] args) throws Exception {OutputStreamWriter o = new OutputStreamWriter(new FileOutputStream("E:\\JavaSE\\Test\\File\\java.txt"),"GBK");o.write("小飛俠");o.close();InputStreamReader i = new InputStreamReader(new FileInputStream("E:\\JavaSE\\Test\\File\\java.txt"),"GBK");int by = i.read();while (by !=-1){System.out.print((char)by); //小飛俠by=i.read();}}
}
方法名 | 說明 |
---|---|
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) | 寫一個字符串的一部分 |
案例分析:
package com.zifu;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;public class Test3 {public static void main(String[] args) throws Exception {OutputStreamWriter o = new OutputStreamWriter(new FileOutputStream("E:\\JavaSE\\Test\\File\\java.txt"));//寫一個字符o.write("97"); //97o.write("a"); //a//刷新流o.flush();//寫一個字符數組char[] chars={'a','b','c','d','e'}; //abcde//o.write(chars);//寫一個字符數組的一部分o.write(chars,0,chars.length); //abcdeo.write(chars,1,3); //bcd//寫一個字符串//o.write("xyz"); //xyz//寫一個字符串的一部分o.write("asdfghj",1,5); //sdfgho.close();}
}
方法名 | 說明 |
---|---|
int read() | 一次讀一個字符數據 |
int read(char[] cbuf) | 一次讀一個字符數組數據 |
處理大量輸入輸出數據的計算機,案例分析:
package com.zifu;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;public class Test4 {public static void main(String[] args) throws IOException {InputStreamReader i = new InputStreamReader(new FileInputStream("E:\\JavaSE\\Test\\File\\java.txt"));//一次讀一個字符數據int by = i.read();while (by !=-1){System.out.print((char)by); //aabcdebcdsdfghby=i.read();}//一次讀一個字符數組char[] chars=new char[1024];int c = i.read(chars);while (c!=-1){System.out.println(new String(chars,0,c));c=i.read(chars);
/*
aab
cde
bcds
dfgh */}}
}
案例分析:
package com.zifu;import java.io.*;public class Test5 {public static void main(String[] args) throws IOException {/*BufferedWriter w = new BufferedWriter(new FileWriter("E:\\JavaSE\\Test\\File\\java.txt"));w.write("Hello");w.write("\r\n");w.write("World");*/BufferedReader r = new BufferedReader(new FileReader("E:\\JavaSE\\Test\\File\\java.txt"));int by = r.read();while (by!=-1){System.out.print((char)by);
/*
Hello
World */by=r.read();}//w.close();r.close();}
}
案例分析:
package com.zifu;import java.io.*;public class Test6 {public static void main(String[] args) throws IOException {BufferedWriter w = new BufferedWriter(new FileWriter("E:\\JavaSE\\Test\\File\\java.txt"));for (int i = 0; i <5 ; i++) {w.write("Java"+i);
/*
Java0
Java1
Java2
Java3
Java4*///換行w.newLine();w.flush();}w.close();BufferedReader r = new BufferedReader(new FileReader("E:\\JavaSE\\Test\\File\\java.txt"));/*String s = r.readLine();while (s!=null){System.out.println(s);s=r.readLine();}*/String s;while ((s=r.readLine())!=null){System.out.println(s);}}
}
/*
Java0
Java1
Java2
Java3
Java4*/
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态