FileIO

    xiaoxiao2021-04-18  50

    public static void main(String[] args) throws IOException { File file = new File("E:\\lalala.txt"); // 1.构建文件 // 检查异常 编译异常 file.createNewFile(); // 判定文件是否存在 if (file.exists()) { // code execute here .存在 System.out.println("exists"); } File file2 = file.getAbsoluteFile(); long length = file.length(); long time = file.lastModified(); System.out.println("文件大小" + length); System.out.println(time); System.out.println(file2); }

    /** * 1.1 读取硬盘上的一个文本文件 * @throws IOException */ public static void main(String[] args) throws IOException { // FileInputStream FileInputStream fis = new FileInputStream("E:/lalalal.txt"); // 1.定义一个缓冲区 byte[] byte[] bytes = new byte[1024];// 1k 1.5k // int data; while ((data = fis.read(bytes)) != -1) { // 有数据可读 // byte[] String String temp = new String(bytes, 0, data); System.out.println(temp); } fis.close(); }

    /** * 1.字符流读取文件 * @throws IOException */ public static void main(String[] args) throws IOException { FileReader reader = new FileReader("E:/lalala.txt"); char[] chars = new char[1024]; int data; while ((data = reader.read(chars)) != -1) { String temp = new String(chars, 0, data); System.out.println(temp); } reader.close(); }

    /** * 1.1 带缓冲区 的读取 BufferedReader * @throws IOException */ public static void main(String[] args) throws IOException { Reader reader = new FileReader("E:/lalala.txt"); BufferedReader br = new BufferedReader(reader); String line; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); reader.close(); }

    /** * 二进制读取 DataInputStream * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub InputStream is = new FileInputStream("D:/啦啦啦.png"); DataInputStream dis = new DataInputStream(is); OutputStream os = new FileOutputStream("E:/啦啦啦.png"); DataOutputStream dos = new DataOutputStream(os); byte[] bytes = new byte[1024]; int data; while ((data = dis.read(bytes)) != -1) { dos.write(bytes, 0, data); } dos.close(); os.close(); dis.close(); is.close(); System.out.println("OK"); }

    转载请注明原文地址: https://ju.6miu.com/read-674595.html

    最新回复(0)