文章目錄
1. File 類
File類表示文件和目錄路徑名的抽象表示形式。可以實現文件的創建,刪除,重命名,得到路徑,創建時間等,是唯一與文件本身有關的操作類。
操作方法:
方法 | 作用 |
---|---|
public File(String pathname) | 構造file類實例,傳入路徑 |
exists() | 文件是否存在 |
createNewFile() | 創建新文件 |
delete() | 刪除文件。刪除文件夾得先把裏面的文件刪除 |
isDirectory() | 判斷是否是文件夾 |
isFile() | 判斷給定的路徑是否是文件 |
String[] list() | 列出文件夾中的文件的文件名,不會進入下一層文件夾 |
File[] listFiles() | 列出文件夾中的所有文件 |
mkdir() | 創建文件夾 |
renameTo(File dest) | 重命名,也可以用作移動文件 |
length() | 文件大小 |
getPath() | 路徑名字符串, 相對路徑 |
getAbsolutePath() | 絕對路徑 |
isHidden() | 是否隱藏 |
canRead() | 是否可讀 |
public static final String spearator; 路徑分隔符“\”
File f = new File(文件路徑)
文件路徑: "c:/test/v.txt" "c:\\test\\v.txt" "c:"+File.separator+"test"+File.separator+"v.txt"
FileFilter文件過濾器
public class MyFile {
public static void main(String[] args) {
String fileName = "D:\\school";
File files = new File(fileName);
File[] filesFilters = files.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.getName().endsWith(".pdf");
//返回文件的滿足條件
}
});
//filesFilters = files.listFiles((pathName)->pathName.getName().endsWith(".pdf")); //使用lamda表達式
for(File f:filesFilters){
System.out.println(f.toString());
}
}
}
2.字節流
IO流:輸入輸出流
流是一組有順序的,有起點和終點的字節集合,是對數據傳輸的總稱或抽象。即數據在兩設備間的傳輸稱爲流。 流的本質是數據傳輸。根據數據傳輸特性將流抽象爲各種類,方便更直觀的進行數據操作。
IO流的分類:
- 根據數據處理類型不同分爲:字符流和字節流
- 根據數據流向不同分爲:輸入流和輸出流
字節輸出流
OutputStream
類,是表示輸出字節流的所有類的超類。輸出流接受輸出字節並將這些字節發送到InputStream
類的某個接收器。如果要向文件中輸出,用FileOutputStream
。
字節輸入流
InputStream
類,字節輸入流的所有類的超類。文件:FileInputStream
public class ByteStreamDemo {
private static void out(){
//1.確定文件
File file = new File("D:\\school\\test.txt");
//2.創建文件輸出流對象
try {
OutputStream out = new FileOutputStream(file,true);//append爲true表示在文件尾部追加內容
//3.輸出到文件的內容
String info = "哈哈哈";
out.write(info.getBytes());
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void in(){
//1.確定文件
File file = new File("D:\\school\\test.txt");
//2.創建文件輸入流對象
try {
InputStream in = new FileInputStream(file);
byte[] bytes = new byte[1024];
StringBuilder buf = new StringBuilder();
int len = -1;//表示每次讀取的字節數
//把數據讀取到數組中,並返回讀取的字節數,當不等於-1時,表示讀取到數據,當等於-1時表示文件已經讀完
while ((len = in.read(bytes))!=-1){
buf.append(new String(bytes,0,len));//一定要從數組的起始位置,到真實讀取數據的結束位置。
//如果不這麼寫,最後一次讀取的數據,可能會保存了上一次的數據。
}
in.close();
System.out.println(buf);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
out();
in();
}
}
操作原理:每次只會操作一個字節(從文件中讀取或者寫入),默認每次執行寫入操作會直接把數據寫入文件。
3.字符流
Writer
寫入字符流的抽象類,子類必須實現的方法僅有write(char[],int,int)
、flush()
、close()
.
對文件的操作使用FileWriter
Reader
讀取字符流的抽象類,子類必須實現的方法僅有read(char[],int,int)
、close()
.
對文件的操作使用FileReader
public class CharStreamDemo {
private static void in() {
File file = new File("D:\\school\\test.txt");
try {
Reader in = new FileReader(file);
char[] cs = new char[1];
int len = -1;
StringBuilder buf = new StringBuilder();
while ((len=in.read(cs))!=-1){
buf.append(new String(cs,0,len));
}
in.close();
System.out.println(buf);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void out(){
File file = new File("D:\\school\\test.txt");
try {
Writer out = new FileWriter(file);
out.write("呵呵呵");
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
in();
}
}
操作原理:操作的單位是一個字符。文件字符操作會自動緩存,默認大小爲1024字節,在緩存滿了後,或者手動刷新緩存,或者關閉流時會把數據寫入文件。
**如何選擇字節流還是字符流:**操作非文本文件時,使用字節流。操作文本文件時,使用字符流。
文件複製:從一個輸入流中讀取數據,然後通過輸入流寫入目標位置。一邊讀一邊寫。
private static void copy(String src,String target){
File srcFile = new File(src);
File targetFile = new File(target);
try {
InputStream in = new FileInputStream(srcFile);
OutputStream out = new FileOutputStream(targetFile);
byte[] bytes = new byte[1024];
int len = -1;
while ((len=in.read(bytes))!=-1){
out.write(bytes,0,len);
}
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
4.字節字符流轉換
可以將一個字節流轉換爲字符流,也可以將一個字符流轉換爲字節流。
OutputStreamWriter
可以將輸出的字符流轉換爲字節流的輸出形式
InputStreamReader
將輸入的字節流轉換爲字符流輸入形式
public class ChangeStreamDemo {
private static void read(InputStream in){
Reader reader = new InputStreamReader(in, Charset.defaultCharset());
char[] cs = new char[1024];
int len = -1;
try {
while ((len = reader.read(cs))!=-1){
System.out.println(new String(cs,0,len));
}
reader.close();
}catch (IOException e){
e.printStackTrace();
}
}
private static void write(OutputStream out){
Writer writer = new OutputStreamWriter(out,Charset.defaultCharset());
try {
writer.write("xxxxxx");
writer.close();
}catch (IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) throws FileNotFoundException {
OutputStream out = new FileOutputStream("D:\\school\\test.txt",true);
write(out);
InputStream in = new FileInputStream("D:\\school\\test.txt");
read(in);
}
}
5.緩衝流
對文件或者其他目標進行頻繁的讀寫操作,效率低,性能差。
使用緩衝流的好處是能更高效地讀寫信息,其原理:將數據先緩衝起來,然後一起寫入或者讀取出來。
BufferedInputStream
: 爲另一個輸入流添加一些功能,在創建時,會創建一個內部緩衝區數組,用於緩衝數據。
BufferedOutputStream
: 通過設置這種輸出流,應用程序可以將各個字節寫入到底層輸出流中,而不必針對每次字節寫入調用底層系統。
BufferedReader
: 從字符輸入流中讀取文本,緩衝各個字符,從而實現字符,數組和行的高效讀取。
BufferedWriter
: 將文本寫入字符輸出流,緩衝各個字符,從而提供單個字符、數組和字符串的高效寫入。
public class BufferStreamDemo {
private static void byteWriter(){
File file = new File("D:\\school\\test.txt");
try {
OutputStream out = new FileOutputStream(file);
//創建字節緩衝流
BufferedOutputStream bos = new BufferedOutputStream(out);
String info = "buffer buffer";
bos.write(info.getBytes());
bos.close();
//out.close(); //此處可以不關閉,BufferedOutputStream在關閉的時候已經爲OutputStream關閉了
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
private static void byteReader(){
File file = new File("D:\\school\\test.txt");
try {
InputStream in = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(in);
byte[] bytes = new byte[1024];
int len = -1;
while ((len=bis.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
}
bis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
byteWriter();
byteReader();
}
}
BufferedOutputStream
內部默認的緩存大小是8kb,每次寫入時存儲到緩存中的byte數組中,當數組存滿時,會把數組的數據寫入文件中,並且緩存下標歸零。
public class BufferStreamDemo {
private static void charReader(){
File file = new File("D:\\school\\test.txt");
try {
Reader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader);
char[] cs = new char[1024];
int len = -1;
while ((len=br.read(cs))!=-1){
System.out.println(new String(cs,0,len));
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void charWriter(){
File file = new File("D:\\school\\test.txt");
try {
Writer writer = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(writer);
bw.write("rrrrrr");
bw.flush();
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
charWriter();
charReader();
}
}
字符緩衝流:
- 加入字符緩衝流可以增強讀取功能(readLine)
- 可以更高效的讀取數據,默認緩存是8k,但可以手動指定緩存大小,把數據讀取到緩存中,減少每次轉換過程,效率更高。
6.打印流
打印流的主要功能是用於輸出,分爲:
- 字節打印流
PrintStream
- 字符打印流
PrintWriter
打印流可以很方便的進行輸出
public class PrintStreamDemo {
private static void bytePrint(){
File file = new File("D:\\school\\test.txt");
try {
OutputStream out = new FileOutputStream(file);
//加緩存
BufferedOutputStream bos = new BufferedOutputStream(out);
//增強打印功能
PrintStream ps = new PrintStream(bos);
ps.println("print");
ps.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private static void chatPrint(){
File file = new File("D:\\school\\test.txt");
try {
Writer out = new FileWriter(file);
BufferedWriter bos = new BufferedWriter(out);
PrintWriter pw = new PrintWriter(bos);
pw.println("xxxxxx");
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
chatPrint();
}
}
7.對象流
對象流的兩個分類:
- ObjectOutputStream 將java對象的基本數據類型和圖形寫入OutputStream
- ObjectInputStream 對以前使用ObjectOutputStream寫入的基本數據和對象進行反序列化
序列化和反序列化:
-
Java序列化就是指把Java對象轉換爲字節序列的過程。Java反序列化就是指把字節序列恢復爲Java對象的過程。
-
序列化最重要的作用:在傳遞和保存對象時.保證對象的完整性和可傳遞性。對象轉換爲有序字節流,以便在網絡上傳輸或者保存在本地文件中。
反序列化的最重要的作用:根據字節流中保存的對象狀態及描述信息,通過反序列化重建對象。
總結:核心作用就是對象狀態的保存和重建。(整個過程核心點就是字節流中所保存的對象狀態及描述信息)
transient
關鍵字:在變量聲明時使用,表示在序列化中該變量被忽略。
//如果一個類創建的對象,需要被序列化,那麼該類必須實現Serializable接口
//Serializable是一個標記接口,沒有任何定義,爲了告訴JVM該類對象可以被序列化
//什麼時候對象需要被序列化?
//1.對象保存到文件中(存儲到物理介質)
//2.對象需要在網絡上傳輸
public class Dog implements Serializable {
private String name;
private int age;
private String sex;
private transient int id;//在序列化中被忽略。
...getter setter...
public Dog(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public Dog() {
super();
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
}
public class ObjectStreamDemo {
//對象序列化,把對象寫入文件。實際寫入的是類名,屬性名,屬性類型,屬性的值等。
private static void writeObject(){
Dog dog = new Dog("ww",2,"母");
File file = new File("D:\\school\\test.txt");
OutputStream out = null;
try {
out = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(dog);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//反序列化,從文件中讀取
private static void readObject(){
File file = new File("D:\\school\\test.txt");
try {
InputStream in = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(in);
Dog dog = (Dog)ois.readObject();
ois.close();
System.out.println(dog);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
writeObject();
readObject();
}
}
如果要同時序列化多個對象,可以採用序列化對象數組的形式。
8.字節流數組
ByteArrayInputStream
: 包含一個內部緩衝區,該緩衝區包含從流中讀取的字節。內部計數器跟蹤read方法要提供的下一個字節。關閉ByteArrayInputStream
無效。此類中的方法在關閉此流後仍可被調用,而不會產生異常。
ByteArrayOutputStream
: 此類實現一個輸出流,其中的數據被寫入到一個byte數組中,緩衝區會隨着數據的不斷寫入而自動增長。可以使用toByteArray()
和toString()
獲取數據。關閉ByteArrayOutputStream
無效。此類中的方法在關閉此流後仍可被調用,而不會產生異常。
//字節數組流,基於內存操作,內部維護着一個數組,我們可以利用流的讀取機制來處理字符串。無序關閉。
public class ByteArrayStreamDemo {
private static void byteArray(){
String s = "123456789adfjadjf*)(@@#$@!##jiHJi";
ByteArrayInputStream bais = new ByteArrayInputStream(s.getBytes());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int curr = -1;//每次讀取的字節
while ((curr = bais.read())!=-1){
if((curr>=65 && curr<=90) ||(curr>=97 && curr<=122)){
baos.write(curr);
}
}
//無序關閉,原因:字節數組是基於內存的操作
System.out.println(baos.toString());
}
public static void main(String[] args) {
byteArray();
}
}
9.數據流
DataInputStream : 數據輸入流允許應用程序以與機器無關方式從底層輸入流中讀取基本java數據類型。應用程序可以使用數據輸出流寫入稍後由數據輸入流讀取的數據。DataInputStream對於多線程訪問不一定是安全的。線程安全是可選的,它由此類方法的使用者負責。
DataOutputStream : 數據輸出流允許應用程序以適當方式將基本java數據類型寫入輸出流中,然後,應用程序可以使用數據輸入流將數據讀入。
//數據流
public class DataStreamDemo {
private static void read(){
File file = new File("D:\\school\\test.txt");
InputStream in = null;
try {
in = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(in);
DataInputStream dis = new DataInputStream(bis);
int num = dis.readInt();
byte b = dis.readByte();
String s = dis.readUTF();
System.out.println(num+" "+ b+ " "+s);
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void write(){
File file = new File("D:\\school\\test.txt");
try {
OutputStream out = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(out);
DataOutputStream dos = new DataOutputStream(bos);
dos.writeInt(10);
dos.writeByte(1);
dos.writeUTF("中");
dos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
write();
read();
}
}
10.合併流、字符串流、管道流
SequenceInputStream
表示其他輸入流的邏輯串聯。它從輸入流的有序集合開始,從第一個輸入流開始讀取,直到到達文件末尾,接着從第二個輸入流讀取,依次類推,直到到達包含的最後一個輸入流的末尾爲止。
字符串流:
- StringReader 其源爲一個字符串的字符流
- StringWriter 一個字符流,可以用於其回收在字符串緩衝區中的輸出,來構造字符串。關閉無效,關閉流後依舊可以被調用。
管道流:
管道輸入流應該連接到管道輸出流,管道輸入流提供寫入管道輸出流的所有數據字節。通常,數據由某個線程從PipedInputStream對象讀取,並由其他線程將其寫入到相應的PipedOutputStream。不建議對這兩個對象嘗試使用單線程,因爲這樣可能死鎖。管道輸入流包含一個緩衝區,可在緩衝區限定的範圍內將讀操作和寫操作分離開。如果向連接管道輸出流提供數據字節的線程不再存在,則認爲管道已經損壞。
11. Properties 文件操作
Properties(Java.util.Properties),主要用於讀取java的配置文件,各種語言都有自己所支持的配置文件,讓用戶脫離程序本身修改相關配置。
主要方法:
- getProperty(String key) 用指定的鍵在此屬性列表中搜索屬性,也就是通過參數key得到對應的value
- load(InputStream inStream) 從輸入流中讀取屬性列表(鍵和元素對),通過指定的文件來裝載獲取該文件中的所有鍵值對,以供getProperty(String key) 來搜索
- setProperty(String key,String comments) 調用Hashtable的方法put,通過調用基類的put來設置鍵值對
- store(OutputStream out , String comments) 以適合使用load方法加載到properties表中的格式,將此properties表中的屬性列表寫入輸出流,與load方法相反,該方法將鍵值對寫入到指定的文件中。
- clear() 清除所有裝載的鍵值對。
public class PropertiesDemo {
private static String version = "";
private static String name = "" ;
private static String password = "";
static {
writeConfig("2","ffff","4354293");
readConfig();
}
//讀取配置文件
private static void readConfig(){
Properties p = new Properties();
InputStream inStream = null;
try {
//inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("com/src/../user.properties");//可以放在其他路徑上
inStream = new FileInputStream("user.properties");
p.load(inStream);
version = p.getProperty("version");
name = p.getProperty("username");
password = p.getProperty("password");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//寫入配置文件
private static void writeConfig(String version, String username,String password){
Properties p = new Properties();
p.put("version",version);
p.put("username",username);
p.put("password",password);
OutputStream out = null;
try {
out = new FileOutputStream("user.properties");
p.store(out,"update config");
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// readConfig();
System.out.println(version+" ,"+name+","+password);
}
}
#update config
#Fri Jun 26 13:44:54 CST 2020
password=4354293
version=2
username=ffff
12.文件壓縮與解壓縮
ZipOutputStream 實現文件的壓縮
- ZipOutputStream(OutputStream out) 創建新的zip輸出流
- void putNextEntry(ZipEntry e) 開始寫入新的zip文件條目並將流定位到條目數據的開始處。
- ZipEntry(String name) 使用指定名稱創建新的zip條目
ZipInputStream 實現文件的解壓
- ZipInputStream(InputStream in) 創建新的zip輸入流
- ZipEntry getNextEntry() 讀取下一個zip文件條目並將流定位到該條目數據的開始處
public class CompressionAndDecompressionDemo {
//壓縮
private static void compression(String zipFileName, File targetFile){
System.out.println("正在壓縮");
try {
//要生成的壓縮文件
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
BufferedOutputStream bos = new BufferedOutputStream(out);
zip(out,targetFile,targetFile.getName(),bos);
bos.close();
out.close();;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//解壓
private static void decompression(String targetFileName ,String parent){
try {
ZipInputStream zIn = new ZipInputStream(new FileInputStream(targetFileName));
ZipEntry entry = null;
File file = null;
while ((entry = zIn.getNextEntry())!=null && !entry.isDirectory()){
file = new File(parent,entry.getName());
if(!file.exists()){
new File(file.getParent()).mkdirs();//創建文件的上級目錄
}
FileOutputStream out = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(out);
byte[] bytes = new byte[1024];
int len = -1;
while ((len=zIn.read(bytes))!=-1){
bos.write(bytes,0,len);
}
bos.close();
System.out.println(file.getAbsolutePath()+"解壓成功");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void zip(ZipOutputStream zOut,File targetFile,String name,BufferedOutputStream bos) throws IOException {
//如果是目錄
if(targetFile.isDirectory()){
File[] files = targetFile.listFiles();
if(files.length==0){
zOut.putNextEntry(new ZipEntry(name+"/"));//處理空目錄
}
for(File f:files){
zip(zOut,f,name+"/"+f.getName(),bos);
}
}else {
zOut.putNextEntry(new ZipEntry(name));
FileInputStream in = new FileInputStream(targetFile);
BufferedInputStream bis = new BufferedInputStream(in);
byte[] bytes = new byte[1024];
int len = -1;
while ((len=bis.read(bytes))!=-1){
bos.write(bytes,0,len);
}
bis.close();
}
}
public static void main(String[] args) {
// compression("D:\\school\\test.zip",new File("D:\\school\\test.txt"));
decompression("D:\\school\\test.zip","D:\\school\\test");
}
}
13.Path和Files
Path接口
- Path表示的是一個目錄名序列,其後可以跟着一個文件名,路徑中第一個部件是根部件時就是絕對路徑,如 / 或 c:\ ,而允許訪問的根部件取決於文件系統。
- 以根部件開始的路徑是絕對路徑,否則是相對路徑
- 靜態的Paths.get方法接受一個或多個字符串,字符串之間自動使用默認文件系統的路徑分隔符鏈接起來(Unix是/ ,windows是 \ ) ,這就解決了跨平臺的問題,接着解析連接起來的結果,如果不是合法路徑,就拋出InvalidPathException異常。否則就返回一個path對象。
public class PathFilesDemo {
public static void main(String[] args) {
File file = new File("D:\\school\\test.txt");
Path p1 = Paths.get("D:\\school","test.txt");
System.out.println(p1);
Path p2 = file.toPath();
System.out.println(p2);
Path p3 = FileSystems.getDefault().getPath("D:\\school","test.txt");
System.out.println(p3);
}
}
Files工具類
-
1.讀寫文件
static Path write(Path path,byte[] bytes,OpenOption … options) 寫入文件
static byte[] readAllBytes(Path path) 讀取文件中的所有字節
-
2.複製,剪切,刪除
static Path copy(Path source,Path target,CopyOption … options)
static Path move(Path source,Path target,CopyOption … options)
static Path delete(Path path)//如果path不存在,文件將拋出異常,建議使用下面的接口
static boolean deleteIfExists(Path path)
-
3.創建文件和目錄
Files.createDirectory(path) // 創建新目錄,除了最後一個部件,其他必須是已存在的。
Files.createDirectories(path) //創建路徑中的中間目錄,能創建不存在的中間部件。
Files.createFile(Path) //創建一個空文件,檢查文件存在,如果已存在則拋出異常,檢查文件是原子性的,在此過程中無法執行文件創建操作。
Path newPath = Files.createTempFile(dir,prefix,suffix);
Path newPath = Files.createTempDirectory(dir,prefix);//添加前、後綴創建臨時文件或臨時目錄
public class PathFilesDemo {
public static void main(String[] args) throws IOException {
File file = new File("D:\\school\\test.txt");
Path p1 = Paths.get("D:\\school","test.txt");
System.out.println(p1);
Path p2 = file.toPath();
System.out.println(p2);
Path p3 = FileSystems.getDefault().getPath("D:\\school","test.txt");
System.out.println(p3);
//寫入文件
Path p4 =Paths.get("D:\\school\\test.txt");
String info = "xxxxxfjiadfoan";
Files.write(p4,info.getBytes("gb2312"),StandardOpenOption.APPEND);
//讀取文件
byte[] bytes = Files.readAllBytes(p4);
System.out.println(new String(bytes));
//複製文件
Files.copy(p3,Paths.get("D:\\school\\tcopy.txt"),StandardCopyOption.REPLACE_EXISTING);
//移動文件
Files.move(p3,Paths.get("D:\\school\\tcopy.txt"),StandardCopyOption.REPLACE_EXISTING);
//刪除文件
Files.delete(p3);
//創建目錄
Files.createDirectories(Paths.get("D:\\school\\BB"));
//創建文件
Files.createFile(Paths.get("D:\\school\\test.out"));
}
}