文件操作

package ch15;

import java.io.*;

/**
 * Created by Jiqing on 2016/12/28.
 */
public class FileTest {
    public static void main(String[] args) throws IOException{
        // 以当前路径创建一个File对象
        File file = new File(".");
        System.out.println(file.getName());
        // 获取相对路径
        System.out.println(file.getParent());
        // 获取绝对路径
        System.out.println(file.getAbsoluteFile()); //C:\Users\e550\IdeaProjects\crazyJava\.
        // 获取上级路径
        System.out.println(file.getAbsoluteFile().getParent()); //C:\Users\e550\IdeaProjects\crazyJava

        // 在当前路径下创建一个临时文件
        File tmpFile = File.createTempFile("temp",".txt",file);
        // 指定当JVM退出时删除文件
        tmpFile.deleteOnExit();

        // 使用list()方法列出当前路径下所有文件和路径
        String[] fileList = file.list();
        System.out.println("====当前路径下所有文件和路径如下====");
        for (String fileName :fileList) {
            System.out.println(fileName);
        }

        // listRoots() 静态方法列出所有磁盘根路径
        File[] roots = File.listRoots();
        for(File root:roots) {
            System.out.println(root);
        }
    }
}

方法论:尝试自己改造一下,能够加深对内容的理解。

Java可以读取并操作文件,还是蛮厉害的。

Java中的IO流

1.输入流、输出流

2.字节流、字符流

3.节点流、处理流

package ch15;

import java.io.*;

/**
 * Created by Jiqing on 2016/12/28.
 */
public class FileInputStreamTest {
    public static void main(String[] args) throws IOException {
        File file = new File(".");
        // 创建字节输入流
        FileInputStream fis = new FileInputStream(file.getAbsoluteFile().getParent()+"/src/ch15/FileInputStreamTest.java");

        // 创建一个长度为1024的竹筒
        byte[] bbuf = new byte[1024];

        // 保存实际读取的字节数
        int hasRead = 0;

        // 使用循环来重复“取水”
        while ((hasRead = fis.read(bbuf)) > 0) {
            // 取出竹筒中的水滴
            System.out.print(new String(bbuf,0,hasRead));
        }

        // 关闭文件输入流
        fis.close();
    }
}

Java文件操作与输入输出流-LMLPHP

package ch15;

import java.io.*;

/**
 * Created by Jiqing on 2016/12/28.
 */
public class FileReaderTest {
    public static void main(String[] args) throws IOException {
        File file = new File(".");
        try(
            // 创建字符输入流,输入到内存
            FileReader fr = new FileReader(file.getAbsoluteFile().getParent()+"/src/ch15/FileReaderTest.java"))
        {
            // 创建一个长度为32的竹筒
            char[] cbuf = new char[32];
            // 用于保存实际读取的字符数
            int hasRead = 0;
            // 使用循环来重复取水
            while ((hasRead = fr.read(cbuf)) > 0) {
                System.out.print(new String(cbuf,0,hasRead));
            }
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
package ch15;

import java.io.*;

/**
 * Created by Jiqing on 2016/12/28.
 */
public class FileInputStreamTest {
    public static void main(String[] args) throws IOException {
        File file = new File(".");
        // 创建字节输入流
        FileInputStream fis = new FileInputStream(file.getAbsoluteFile().getParent()+"/src/ch15/FileInputStreamTest.java");

        FileOutputStream fos = new FileOutputStream("newFile.txt");

        // 创建一个长度为1024的竹筒
        byte[] bbuf = new byte[128];

        // 保存实际读取的字节数
        int hasRead = 0;

        // 使用循环来重复“取水”
        while ((hasRead = fis.read(bbuf)) > 0) {
            // 取出竹筒中的水滴
//            System.out.println(hasRead);
//            System.out.print(new String(bbuf,0,hasRead));
            fos.write(bbuf,0,hasRead);
        }

        // 关闭文件输入流
        fis.close();
    }
}

Java文件操作与输入输出流-LMLPHP

package ch15;

import java.io.*;

/**
 * Created by Jiqing on 2016/12/28.
 */
public class FileReaderTest {
    public static void main(String[] args) throws IOException {
        File file = new File(".");
        try(
            // 创建字符输入流,输入到内存
            FileReader fr = new FileReader(file.getAbsoluteFile().getParent()+"/src/ch15/FileReaderTest.java");
            FileWriter fw = new FileWriter("newWriterFile.txt")
        )
        {
            // 创建一个长度为32的竹筒
            char[] cbuf = new char[32];
            // 用于保存实际读取的字符数
            int hasRead = 0;
            // 使用循环来重复取水
            while ((hasRead = fr.read(cbuf)) > 0) {
//                System.out.println(hasRead);
//                System.out.print(new String(cbuf,0,hasRead));
                fw.write(cbuf,0,hasRead);
            }
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
04-24 20:26
查看更多