zoukankan      html  css  js  c++  java
  • 文件操作

    1、新建一个 E:/java/test/1.txt 文件,打印出这个文件的名字,路径,父级文件的名字

    public static void main(String[] args) throws IOException {
        
        String FilePath="E:\java\test\1.txt";
        File file=new File(FilePath);
        
        File path=new File(file.getParent());
        //创建目录
        if(!path.exists()){
            path.mkdirs();
        }
        if(!file.exists()){
            file.createNewFile();
        }
        System.out.println("file path is ->"+file.getPath());
        System.out.println("fileName is ->"+file.getName());
    }

     2.用字节输入输出流 把E:/java/test/3.txt 文件内容 输出到E:/java/test/4.txt 文件中

    /**
     * 8、用字节输入输出流 把E:/java/test/3.txt 文件内容 输出到E:/java/test/4.txt 文件中
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        String filePath="E:/java/test/4.txt";
        File file=new File(filePath);
        File path=new File(file.getParent());
        if(!path.exists()){
            path.mkdirs();
        }
        if(!file.exists()){
            file.createNewFile();
        }
        FileInputStream fis=new FileInputStream("E:/java/test/3.txt");
        byte[] buf = new byte[1024];
        // 定义一个StringBuffer用来存放字符串
        String str="";
        // 开始读取数据
        int len = 0;
        // 每次读取到的数据的长度
        while ((len = fis.read(buf)) != -1) {// len值为-1时,表示没有数据了
            str+=new String(buf, 0, len, "utf-8");
        }
        // 输出字符串
        System.out.println(str);
    }

     3.数组截取

    public static byte[] subBytes(byte[] src, int begin, int count) {  
    byte[] bs = new byte[count];  
    System.arraycopy(src, begin, bs, 0, count);  
    

    src:源数组

    srcPos:源数组要复制的起始位置

    dest:目的数组

    destPos:目的数组放置的起始位置

    length:要复制的长度

  • 相关阅读:
    职业生涯系列
    自我进修系列
    每周问题系列
    职业生涯系列
    软件测试专用名词
    Java系列 – 用Java8新特性进行Java开发太爽了(续)
    Java系列
    EJB系列
    EJB系列
    EJB系列
  • 原文地址:https://www.cnblogs.com/zhuxiang1633/p/7814633.html
Copyright © 2011-2022 走看看