java 读取文本文件,手动将字节转换为字符串 并复制到另外位置

时间:2022-01-23 21:01:32

a.txt 文本内容如下:

name=user
age=34
image=aa.PNG
url=E:\\

package cn.com.test05;

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

public class t01 {

    public static void main(String[] args) throws IOException {
        File f=new File("F:\\a.txt");
        InputStream in= new FileInputStream(f);
        OutputStream out=new FileOutputStream(new File("F:\\b.txt"));
        int i=0;
        int j=0;
        char[] c =new char[(int) f.length()];
        while((i=in.read())!=-1){
            c[j++]=(char)i;
        }
        String str=new String(c);
        System.out.println(str);
        out.write(str.getBytes());
        in.close();out.close();
    }

    

}