4.2.1 File类简介
java.io.File
看api:http://docs.oracle.com/javase/8/docs/api/
4.2.2 文件的创建,删除,重命名
方法:
- exist()
- createNewFile()
-
Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist
- 创建成功返回true,否则false
- 如果这个文件已经存在,不会重新创建
-
- isFile()
- isDirectory()
- file.delete();
- file.renameTo(fileWithNewName); 如果目标文件或者文件夹存在,重命名方法返回false。
- File file = new File("file1");
- // 文件名字没有以/开头,是相对路径,相对于当前工程目录下
- // createNewFile()的时候会把文件创建在当前工程目录下
- if(file.exists()){
- System.out.println("File exists");
- System.out.println("isFile :" + file.<span style="background-color: rgb(255, 255, 153);">isFile</span>());
- System.out.println("isDirectory :" + file.<span style="background-color: rgb(255, 255, 153);">isDirectory</span>());
- }else {
- System.out.println("File does not exist");
- try {
- file.<span style="background-color: rgb(255, 255, 153);">createNewFile</span>();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
相对路径:相对于当前工程目录下
- File file = new File("file1");
- File file1 = new File("bin/file1");
- File file2 = new File("../file1");
给文件重命名:
- File file = new File("file1");
- if(file.exists()) {
- File <span style="background-color: rgb(255, 153, 102);">nameTo</span> = new File("newFileName");
- file.<span style="background-color: rgb(255, 255, 153);">renameTo</span>(<span style="color:#000000;background-color: rgb(255, 153, 102);">nameTo</span>);
- }else {
- try {
- file.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
移动文件(用重命名方法):新的路径必须在同一个分区下,mac下本身就是一个分区。
- File nameTo = new File("src/newFileName");
4.2.3 文件夹的创建,删除,重命名
方法:
- mkdir();
- 创建成功返回true,否则false
- 如果这个目录已经存在,不会重新创建
- File folder = new File("newFolder");
- if(folder.mkdir()){
- System.out.println("Folder created");
- }else{
- if (folder.exists()) {
- System.out.println("folder exists already");
- } else {
- System.out.println("cannot create folder");
- }
- }
- 用mkdir创建一个文件夹在一个路径下,无论这个路径是相对路径还是绝对路径,这个路径必须存在,否则文件夹创建失败
- File folder = new File("newFolder/one/two");
- 创建成功返回true,否则false
- mkdirs() 根据路径创建整个文件夹结构
-
- File folder = new File("newFolder/one/two");
- if(folder.<span style="background-color: rgb(255, 255, 153);">mkdirs</span>()){
- System.out.println("Folder created");
- }else{
- if (folder.exists()) {
- System.out.println("folder exists already");
- } else {
- System.out.println("cannot create folder");
- }
- }
-
folder.delete(); 只有文件夹是空文件夹的时候,才可以删除成功。
给文件夹重命名
- File folder = new File("newFolder");
- if (folder.mkdirs()) {
- System.out.println("Folder created");
- } else {
- if (folder.exists()) {
- System.out.println("folder exists already");
- } else {
- System.out.println("cannot create folder");
- }
- }
- File folder_renamed = new File("newFolder-renamed");
- if(folder.renameTo(folder_renamed)){
- System.out.println("renamed");
- } else{
- System.out.println("rename failure");
- }
4.2.4 文件属性的读取
- //准备工作: 在工程目录下创建一个文件,名字是file.txt,输入些内容
- File f = new File("file.txt");
- System.out.println("file exists : " + f.exists());
- System.out.println("file name : " + f.getName());
- System.out.println("file reletive path: " + f.getPath());
- System.out.println("file absolute path: " + f.getAbsolutePath());
- System.out.println("file's parent path: " + new File(f.getAbsolutePath()).getParent());
- System.out.println("file's length: " + f.length());
- System.out.println("file is hidden:" + f.isHidden());
- System.out.println("file can read:" + f.canRead());
- System.out.println("file can white:" + f.canWrite());
- System.out.println("file is direcotry:" + f.isDirectory());
4.2.5 文件属性的设置
- //准备工作: 在工程目录下创建一个文件,名字是file.txt,输入些内容
- File f = new File("file.txt");
- //将文件设定为可读
- f.setWritable(true);
- //将文件设计为可读
- f.setReadable(true);
- //将文件设计为只读
- f.setReadOnly();
4.2.6 递归遍历文件夹
- public static void main(String[] args) throws IOException {
- File folder = new File("newFolder");
- printFolderFiles(folder, 1);
- }
- public static void printFolderFiles(File dir, int tab){
- if(dir.isDirectory()){
- File[] arrFiles = dir.listFiles();
- for (int i = 0; i < arrFiles.length; i++) {
- for (int j = 0; j < tab; j++) {
- System.out.print("|---");
- }
- System.out.println(arrFiles[i].getName());
- if(arrFiles[i].isDirectory()){
- printFolderFiles(arrFiles[i], tab+1);
- }
- }
- }else{
- System.out.println("path is not directory");
- }
- }
4.2.7 文件的简单读写
文件输入(read file to ram)
- 创建FileInputStream
- 创建InputStreamReader
- 创建带BufferedReader(带buffer的reader)
- File file = new File("file.txt");
- if(file.exists()){
- FileInputStream fis = null;
- try {
- fis = new FileInputStream(file);
- //FileInputStream是字节流, for reading streams of raw bytes
- //An InputStreamReader is a bridge from byte streams to character streams: It
- //reads bytes and decodes them into characters using a specified charset
- //
- InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
- BufferedReader br = new BufferedReader(isr);
- String line;
- while((line = br.readLine()) != null){
- System.out.println(line);
- }
- br.close();
- isr.close();
- fis.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
文件output(write ram to file)
- 创建文件的输出流
- 创建带有输出流的writer
- 创建带有buffer的输出writer
- File newFile = new File("newFile.txt");
- try {
- //new FileOutputStream的时候,如果文件不存在,会被创建
- FileOutputStream fos = new FileOutputStream(newFile);
- OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
- BufferedWriter bw = new BufferedWriter(osw);
- //white方法会覆盖原来的文件
- bw.write("1111111111\n");
- bw.write("2222222222");
- bw.write("3333333333");
- bw.write("4444444444");
- bw.close();
- osw.close();
- fos.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }