package cn.edu.fhj.day009.FileDemo;
import java.io.File;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) throws IOException {
// 將路徑描述成File對象
// File file = new File("d:/java_fd_test/fileDemo.txt");
File file = new File(
"F:/up_mouth_10/code_data/Data_Structure_And_Algorithm");
boolean exists = file.exists(); // 如果路徑所表示的文件或者文件夾存在,則返回true
System.out.println(exists);
// 判斷該file是文件夾還是文件
boolean directory = file.isDirectory();
System.out.println(directory); // true
boolean ifFile = file.isFile();
System.out.println(ifFile); // false
// 獲取文件的絕對路徑
String absolutePath = file.getAbsolutePath();
System.out.println(absolutePath);
// 可以獲取文件名或文件夾名
String name2 = file.getName();
System.out.println(name2);
File file2 = new File(
"F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/Demo.txt");
ifFile = file2.isFile(); // true
System.out.println(ifFile);
// 獲取文件名
String name = file2.getName();
System.out.println(name);
// 獲取上一級目錄的file對象
File parentFile = file2.getParentFile();
System.out.println(parentFile.getAbsolutePath());
// 獲取上一級目錄的路徑字符串
String parent = file2.getParent();
System.out.println(parent);
// 獲取文件長度 字節(8個bit-- 二進制位)
long length = file2.length();
System.out.println(length);
System.out.println("------------------------");
// 獲取指定目錄下的子節點的名稱字符串
String[] list = file.list();
for (String s : list) {
System.out.println(s);
}
System.out.println("------------------------");
// 獲取指定目錄下的子節點的File描述對象
File[] listFiles = file.listFiles();
for (File f : listFiles) {
System.out.println(f.getAbsolutePath());
}
System.out.println("------------------------");
// 創建一個文件夾
File f = new File(
"F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/xx/yy/zz");
// boolean mkdir = f.mkdir(); // 不能創建多級目錄
// System.out.println(mkdir);
// boolean mkdirs = f.mkdirs(); // 可以創建多級目錄
// System.out.println(mkdirs);
//
// // 創建文件
File file3 = new File(
"F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/xx/yy/zz/cls.txt");
boolean createNewFile = file3.createNewFile();
System.out.println(createNewFile);
// 重命名文件:其實可以把路徑都給改了
file3.renameTo(new File(
"F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/xx/yy/zz/cls001.txt"));
// 刪除文件
boolean delete = file3.delete();
System.out.println(delete);
}
}
FileOutputStreamDemo文件的寫
package cn.edu.fhj.day009.FileDemo;
import java.io.FileOutputStream;
public class FileOutputStreamDemo {
public static void main(String[] args) throws Exception {
// 覆蓋的方式寫數據
FileOutputStream fos = new FileOutputStream(
"F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/Demo.txt");
String s = "a你好";
byte[] bytes = s.getBytes();
fos.write(bytes);
// 將字符串按指定編碼集編碼--》將信息轉成二進制數 fos.write(bytes); // 這樣寫入的數據,會將文件中的原數據覆蓋
// 追加的方式寫數據:如果要往一個文件中追加數據,則在FileOutputStream的構造參數中多傳一個true
FileOutputStream fos2 = new FileOutputStream(
"F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/Demo.txt",
true);
fos2.write(",sb".getBytes("UTF-8"));
fos2.close();
/**
* 第一句和后兩句話寫到文件中的數據完全相同
*/
fos.write("我用一生一世為你祈禱".getBytes()); // .getBytes()編碼的過程
fos.write((byte) 49);
fos.write((byte) 51);
/**
* 這兩句話寫到文件中的數據完全相同
*/
// fos.write((byte)13);
// fos.write("\r".getBytes());
fos.close();
}
}
FileInputStreamDemo的讀
package cn.edu.fhj.day009.FileDemo;
import java.io.FileInputStream;
import java.io.InputStream;
public class FileInputStreamDemo {
public static void main(String[] args) throws Exception {
// 要讀文件,首先要構造一個FileInputStream對象
InputStream fis = new FileInputStream(
"F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/Demo.txt");
/**
* 把數從文件中讀取出來 如何讀取字符
*/
// FileInputStream是一種字節流,是按照一個一個字節去文件中取數據的
// 手動一個字節一個字節地讀取
/*
* int read = fis.read();
*
* System.out.println(read);
*
* read = fis.read(); System.out.println(read);
*/
/**
* 利用fis讀到文件末尾后會返回-1的特性,來用循環進行讀取
*/
int read = 0;
/*
* while((read=fis.read())!=-1) { System.out.println(read); }
*/
System.out.println("-------------------");
/**
* 如果我要讀出數據(文本文件中的數據其實就是字符) 過程是:還是先讀數,然后按照碼表,將這個數轉成字符
*
*/
/*
* read = 0; while((read=fis.read())!=-1) { //
* char就代表一個英文字符,而且使用的是ascII碼表規則 char c = (char)read;
* System.out.println(c); }
*/
/**
* 一次讀取多個字節然后轉成某種數據類型 read(buf)方法,一次讀取buf長度個字節數據,並且讀到的數據直接填入了buf數組中
*/
/*
* byte[] buf = new byte[8]; int num = fis.read(buf); // 返回的是真實讀到的字節數量
* String string = new String(buf,2,5); // 利用二進制的byte數組來轉成字符串
* System.out.println(string);
*/
/**
* 用while循環來反復讀取
*/
int num = 0;
byte[] buf = new byte[8];
while ((num = fis.read(buf)) != -1) {
System.out.println(new String(buf, 0, num));
}
// 關流
fis.close();
}
}