java.io.*

View Code

创建文件test2.txt

public class test {
    public static void main(String[] args) {
        File f = new File("D:\txt\test2.txt");
        if(!f.exists()){
            //可以创建
            try {
                f.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        System.out.println("文件的位置" + f.getAbsolutePath());
        System.out.println("文件的大小" + f.length());
    }
}
View Code

创建文件夹hello

import java.io.*;

public class test {
    public static void main(String[] args) {
        File f = new File("D:\txt\hello");
        if (!f.isDirectory()) {
            f.mkdir();
        }
    }
}
View Code
文件字节流实现:文件——>内存  FileInputStream
View Code

FileOutputStream   写入文件中

package io;

/**
 * file类的基本用法
 */
import java.io.*;

public class test {
    public static void main(String[] args) {
        File f = new File("D:\txt\test1.txt");// 代表文件对象
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(f);
            String s = "庞静赢是王志的娘子
";
            String s1 = "王志是庞静赢的相公";
            fos.write(s.getBytes());
            fos.write(s1.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
View Code
原文地址:https://www.cnblogs.com/helloworld2019/p/10831492.html