字符集编码解码,File迭代查找文件并运行,FileInputStream读取文件

  • 字符集编码,解码
package com.heima.test;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class ByteTest {
    public static void main(String[] args) throws UnsupportedEncodingException {
        //默认utf8,中文三个字节
        //编码
        String name = "abc我爱你中国";
        //utf8
        byte[] bytes = name.getBytes();
        //gbk
        byte[] bytes1 = name.getBytes("GBK");
        System.out.println(bytes.length);//18
        System.out.println(Arrays.toString(bytes));
        //[97, 98, 99, -26, -120, -111, -25, -120, -79, -28, -67, -96, -28, -72, -83, -27, -101, -67]


        //解码
        //utf8
        String rs = new String(bytes);
        System.out.println("rs = " + rs);//rs = abc我爱你中国
        //gbk
        String rs1 = new String(bytes1,"GBK");
        System.out.println("rs1 = " + rs1);//rs1 = abc我爱你中国
    }
}


  • File查找文件运行
package com.heima.test;

import java.io.File;
import java.io.IOException;

public class FileSearch {
    public static void main(String[] args) {
        searchFile(new File("C:/"), "git-bash.exe");
    }

    public static void searchFile(File dir, String fileName) {
        if (dir != null && dir.isDirectory()) {
            File[] files = dir.listFiles();
            if (files != null && files.length > 0) {
                for (File file : files) {
                    if (file.isFile()) {
                        //   if (file.getName().contains(fileName)){
                        if (file.getName().equals(fileName)) {
                            System.out.println("找到了! " + file.getAbsolutePath());
                            Runtime r = Runtime.getRuntime();
                            try {
                                r.exec(file.getAbsolutePath());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    } else {
                        searchFile(file, fileName);
                    }
                }
            }
        } else {
            System.out.println("错误,当前搜索位置不是文件夹");
        }
    }
}

  • FileInputStream读取文件
package com.heima.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileInputStreamTest {
    public static void main(String[] args) throws IOException {
        File f = new File("src/fileinput.txt");
        InputStream is = new FileInputStream(f);
        byte[] buffer = new byte[3];
        int len = is.read(buffer);

        String rs2 = new String(buffer, 0, len);
        System.out.println("rs2 = " + rs2);
    }
}

package com.heima.test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileInputStreamTest {
    public static void main(String[] args) throws IOException {
        InputStream is = new FileInputStream("src/fileinput.txt");
        byte[] buffer = new byte[3];
        int len;//读取的字节数
        while ((len = is.read(buffer)) != -1) {
            System.out.print(new String(buffer, 0, len));
        }
    }
}

package com.heima.test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileInputStreamTest {
    public static void main(String[] args) throws IOException {
        InputStream is = new FileInputStream("src/fileinput.txt");
//        byte[] buffer = new byte[3];
//        int len;//读取的字节数
//        while ((len = is.read(buffer)) != -1) {
//            System.out.print(new String(buffer, 0, len));
//        }

/*
        int b1 = is.read();
        System.out.println(b1);
        System.out.println((char)b1);

        int b2 = is.read();
        System.out.println((char)b2);*/

        int b ;
        while ((b = is.read())!=-1){
            System.out.print((char)b);
        }
    }
}

package com.heima.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileInputStreamTest {
    public static void main(String[] args) throws IOException {
        File f = new File("src/fileinput.txt");
        InputStream is = new FileInputStream(f);
       byte[] buffer = new byte[(int) f.length()];
        // byte[] buffer = is.readAllBytes();
       int len = is.read(buffer);
        System.out.println("读取了多少个字节: "+len);
        System.out.println("文件大小: "+f.length());
        System.out.println(new String(buffer));
       
    }
}

package com.heima.test;

import java.io.File;
import java.text.SimpleDateFormat;

public class FileTest {
    public static void main(String[] args) {
        File file = new File("./src/deng.txt");
        File file1 = new File("src/shi.txt");
        System.out.println(file.length());
        System.out.println(file1.length());
        System.out.println(file1.exists());
        System.out.println(file1.delete());
        System.out.println(file.getAbsolutePath());
        long time = file.lastModified();
        System.out.println(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(time));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值