java学习第七天

2020-01-14  本文已影响0人  toro宇

IO流


\在文件中是路径的分隔符,但是在java编程中一个\的意思是转义符,在java中\或者/才是文件的分割符
import java.io.File;
import java.io.IOException;

public class Test {
public static void main(String[] args) {
 // 注意, \在文件中是路径的分隔符,但是在java编程中一个\的意思是转义符,在java中\\或者/才是文件的分割符
    File f1 = new File("/Users/codeyy/Desktop/Java/test/222.rtf");// 这个时候对象f就是ceshi.rtf文件
//      File f2 = new File("//Users//codeyy//Desktop//Java//test","ceshi.rtf");
    File f3 = new File("/Users/codeyy/Desktop/Java");
    System.out.println(f1.getName());// 获取文件名
    System.out.println(f3.getName());// 获取文件夹名
    
    File f = new File("src/day11.Test.java");// 使用相对路径来创建file对象

    System.out.println(f.getPath()); // 获取文件或文件夹的路径,就是new file传入的路径
    // src/day11.Test.java
    System.out.println(f.getAbsolutePath()); // 获取当前文件的绝对路径
    ///Users/codeyy/Desktop/Java/testDemo/src/day11.Test.java
  
    
  System.out.println(f);
  // src/day11.Test.java
  System.out.println(f.getAbsoluteFile()); // 返回一个用当前的文件的绝对路径构建的file对象
 //  /Users/codeyy/Desktop/Java/testDemo/src/day11.Test.java
  
  System.out.println(f.getParent()); // 返回当前文件或者文件夹的父级路径
  
  f1.renameTo( new File("/Users/codeyy/Desktop/Java/test/111.rtf"));// 给文件或者文件夹重命名
  
  File f4 = new File("/Users/codeyy/Desktop/Java/test/221.rtf");
  System.out.println(f4.exists()); // 判断文件或文件夹是否存在
  // false
  
  System.out.println(f1.canWrite()); //判断文件是否可写
  System.out.println(f1.canRead()); // 判断文件是否可读
  
  System.out.println(f.isFile()); // 判断当前的file对象是不是文件
  System.out.println(f.isDirectory()); // 判断当前file是不是文件夹或目录
  
  System.out.println(f.lastModified());//获取文件的最后修改时间,返回一个毫秒数
  System.out.println(f.length());// 返回文件的长度,单位是字节
  
   File f5 = new File("/Users/codeyy/Desktop/Java/test/tt2.rtf");
   if(!f5.exists()) {
       try {
        f5.createNewFile(); // 创建新的文件
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
   }
   f5.delete(); // 删除文件
   
   File f6 = new File("/Users/codeyy/Desktop/Java/test2");
   f6.mkdir();// 创建单层目录,如果这一方法来来创建多层目录,就得一层一层的执行mkdir()
   
   File f7 = new File("/Users/codeyy/Desktop/Java/a/b/c");
   f7.mkdirs(); // 直接创建多层目录

   File f8 = new File("/Users/codeyy/Desktop/Java");
   String[] ff = f8.list(); // 返回的时当前文件夹的子集的名称,包括目录和文件
   for(String s: ff) {
       System.out.println(s);
   }
   
   File[] fs = f8.listFiles(); // 返回的时当前文件夹的子集的file对象,包括目录和文件
   for(File l : fs) {
       System.out.println(l);
   }
}

}
// 文件字节输出流FileOutputStream
public static void TestFileOutStream() {
    try {
        FileOutputStream out = new FileOutputStream("/Users/codeyy/Desktop/Java/test/ddd.txt");
        String str = "hekkkkkkk";
        out.write(str.getBytes());// 把数据写到内存
        out.flush();// 把内存中的数据刷写到硬盘
        out.close();// 关闭流
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

文件字节流非常通用,可以用来操作字符的文档,还可以操作任何的其他类型文件(图片,压缩包),因为字节流直接使用的是二进制

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Test {
public static void main(String[] args) {
      copyFile("/Users/codeyy/Desktop/Java/111111.png","/Users/codeyy/Desktop/Java/222222.png");
}

/*
 * 赋值文件到指定位置
 * @param inPath 源文件路径
 * @param outPath 复制到的文件夹
 * */
public static void copyFile(String inPath, String outPath) {
    try {
        FileInputStream in = new FileInputStream(inPath);
        FileOutputStream out =  new FileOutputStream(outPath);
        
        byte[] b = new byte[100];
        int len = 0;
        while((len = in.read(b)) != -1) {
            out.write(b,0,len);// 参数1是写的缓冲数组,参数2是从数组的那个位置开始,参数3是获取的数组的总长度
        }
        out.flush();// 把写到内存的数据刷到硬盘
        out.close();
        in.close();
    
    }catch(Exception e) {
    }
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Test2 {
public static void main(String[] args) {
    try {
        testBufferedInputStream();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    try {
        testBufferedOutputStream();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
}
// 缓冲字节输入流
public static void testBufferedInputStream() throws Exception {
    // 文件字节输入流
    FileInputStream in = new FileInputStream("/Users/codeyy/Desktop/Java/test/ddd.txt");
    
    // 把文件字节输入流放到缓冲字节输入流对象
    BufferedInputStream br = new BufferedInputStream(in);
    
    byte[] b = new byte[1024];
    int len = 0;
    while((len = br.read(b)) != -1) {
        System.out.println(new String(b,0,len));
    }
    
    // 关闭流的时候,本着一个最晚开的最早管,依次管
    br.close();
    in.close();
}

// 缓冲字节输出流
public static void testBufferedOutputStream() throws Exception{
    // 创建字节输出流对象
    FileOutputStream out = new FileOutputStream("/Users/codeyy/Desktop/Java/test/ccc.txt");
    
    // 把字节输出流对象放到缓冲字节输出流中
    BufferedOutputStream bo = new  BufferedOutputStream(out);
    
    String s = "hello world";
    bo.write(s.getBytes()); // 写到内存中
    
    bo.flush(); // 刷到硬盘上
    
    //关闭流的时候,本着一个最晚关的最先关,依次关
    bo.close();
    out.close();    
}
}
上一篇下一篇

猜你喜欢

热点阅读