```java
import java.io.File;
import java.io.IOException;
public class domo01 {
public static void main(String[] args) throws IOException {
File file1 = new File("D:\\暴風(fēng)激活15.0.exe");
File file2 = new File("d:\\大電影");
File file3 = new File("d:/domo01/domo01.1");
File file4 = new File("d:/domo01/domo01.1");
File file5 = new File("D:\\test.txt");
File file6 = new File("D ://gesf");
// 1 創(chuàng)建一個(gè)文件
file2.createNewFile();
// 2 創(chuàng)建一個(gè)文件夾 mkdir 一個(gè)只能創(chuàng)建一個(gè)文件夾际邻,如果父路徑?jīng)]有寄月,則創(chuàng)建失敗
boolean res = file3.mkdir();
System.out.println(res);
// 3 mkdirs 創(chuàng)建多個(gè)文件夾船殉,如果父路徑不存在债沮,則一次性創(chuàng)建全部路徑
file4.mkdirs();
// 4 刪除文件
file2.delete();
// 5 renameTo 移動(dòng)文件
file1.renameTo(new File(""));
// 6 exists
System.out.println(file2.exists());
// 7 isFile 是否是文件
System.out.println(file4.isFile());
// 8 isDirectory 是否是目錄
System.out.println(file4.isDirectory());
// 9 getPath() 獲取路徑
System.out.println(file6.getPath());
}
}
package com.company;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class domo02 {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try { //1 準(zhǔn)備輸入輸出流
fis = new FileInputStream(new File("c:/jdk8.0.2_windows-x64_bin.exe"));
fos = new FileOutputStream(new File("c:/jik8.exe"));
//2 循環(huán)讀取操作
int len = 0;
long start = System.currentTimeMillis();
byte[] buff = new byte[1024];//開(kāi)辟緩沖區(qū)
while ((len = fis.read(buff)) != -1) {
fos.write(buff, 0, len);
}
long end = System.currentTimeMillis();
System.out.println("時(shí)間" + (end - start));//1265
//3 關(guān)閉輸入輸出資源
fis.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}}
package com.company;
import java.io.*;
public class domo03{
public static void main(String[] args)throws Exception {
BufferedInputStream bis = null ;
BufferedOutputStream bos = null ;
try {
//1 準(zhǔn)備輸入輸出流
bis=new BufferedInputStream( new FileInputStream(new File("c:/jdk8.0.2_windows-x64_bin.exe")));
bos=new BufferedOutputStream(new FileOutputStream(new File("c:/jik8.exe")));
//2 循環(huán)讀取操作
int len=0;
byte[] buff=new byte[1024];//開(kāi)辟緩沖區(qū)
long start = System.currentTimeMillis(); // 開(kāi)始計(jì)時(shí)
while ((len=bis.read(buff))!=-1){
bos.write(buff, 0,len);
}
long end = System.currentTimeMillis(); // 結(jié)束計(jì)時(shí)
System.out.println("時(shí)間"+ ( end - start)); // 453ms
//3 關(guān)閉輸入輸出資源
bis.close();
bos.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
jpackage com.company;
import java.io.*;
public class domo04 {
public static void main(String[] args) throws IOException {
// reader 和 writer 只能操作文本內(nèi)容
Reader reader = new FileReader("d:/1.txt");
FileWriter fileWriter = new FileWriter("d:/111.txt");
char [] buff = new char[1024];
int len = 0 ;
while ( ( len = reader.read(buff)) != -1) {
fileWriter.write(buff,0,len);
}
fileWriter.flush();
// 關(guān)閉資源
reader.close();
fileWriter.close();
}
}