JAVA File工具类

import java.io.*;

public class Fun1 {
    public static void main(String[] args) throws IOException {
        File csc=new File("fan.txt");
        csc.createNewFile();//建立空文件fan.txt

        PrintWriter w=new PrintWriter("csc.txt");
        w.write("Jonny Joestar Kojo Jotora");
        w.flush();//建立文件csc.txt,写入内容

        FileOutputStream cos=new FileOutputStream("cos.txt",true);
        for(int i=0;i<10;i++) {
            cos.write("fans\r\n".getBytes());
        }
            cos.close();


            FileWriter sin=new FileWriter("sin.txt");
            sin.write("Hey,wo go~~!");
            sin.close();
        }
    }

建立文件 空文件 fan.txt
创建文件csc.txt,写入内容"Jonny Joestar Kojo Jotora"

用字节输出流创建文件"cos.txt",并输入内容

用FileWriter字符输出流创建文件"sin.txt"

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.nio.charset.StandardCharsets;

public class Fun2 {
    public static void main(String[] args) throws Exception {
        File ade=new File("csc.txt");
        FileInputStream cot=new FileInputStream(ade);
        System.out.println(new String(cot.readAllBytes(), StandardCharsets.UTF_8));

        FileReader dnf=new FileReader(ade);
        BufferedReader br=new BufferedReader(dnf);
       // while(br.ready()){
        System.out.println(br.readLine());
        //}
    }
}

使用字符流读取文件

使用BufferedReader类读取文件

import com.sun.source.util.SourcePositions;

import java.io.File;
import java.net.SocketOption;
import java.net.SocketTimeoutException;
import java.sql.SQLOutput;

public class Fun3 {
    public static void main(String[] args) throws Exception {
        File cot=new File("csc.txt");
        System.out.println(cot.length());//文件内容长度
        System.out.printf("%tF %<tT%n",cot.lastModified());//创建时间
        System.out.println(cot.getName());//获得文件名
        System.out.println(cot.getPath());//获得文件名
        System.out.println(cot.getAbsolutePath());
        //获得文件目录

        String t = cot.getAbsolutePath();
        System.out.println(t.substring(0,t.lastIndexOf("\\")));//文件所在文件夹

        //文件基本名
        System.out.println(t.substring(t.lastIndexOf("\\")+1,t.lastIndexOf(".")));

        //文件扩展名
        System.out.println(t.substring(t.lastIndexOf(".")));


    }
}

在指定文件夹创建文件

import java.io.File;

public class Fun4 {
    public static void main(String[] args) throws Exception {
        File ufo = new File("ufo.txt");
        ufo.createNewFile();
        String tom = "D:\\JAVA\\jse2204\\jse2204.ufo.txt";
        if (ufo.exists()) {
            ufo.delete();
            System.out.println("删除成功");
        }else {
            System.out.println("文件不存在");
        }
    }
}

 拷贝文件并删除

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

public class Fun5 {
    public static void main(String[] args) {
        String gyro="C:\\Users\\dell.000\\OneDrive\\图片\\其他\\10002.png";
        try(
                var en=new FileInputStream(gyro);
                var an=new FileOutputStream("E:\\BaiduNetdiskDownload\\097533.png")
        )
        {
            byte[] buffer = new byte[1024 * 25];
            int read;
            while ((read = en.read(buffer)) >= 0) {
                an.write(buffer, 0, read);
            }
        } catch (Exception e) {
            
        }
        File sis=new File("E:\\BaiduNetdiskDownload\\097533.png");
        if(sis.exists()){
            sis.delete();
            System.out.println("已经将图片成功拷贝后删除");
        }else{
            System.out.println("文件不存在");
        }
    }
}

创建文件夹

import java.io.File;

public class Fun6 {
    public static void main(String[] args) {
        String vs = "GREAT";
        File code = new File(vs);
        code.mkdirs();
        if (code.exists()){
            System.out.println("创建文件夹1成功");
    }else{
        System.out.println("创建失败");
    }
        code.delete();

        File mk=new File("D:\\JAVA\\mkjh");
        if(!mk.exists()){
            mk.mkdirs();
            System.out.println("创建文件夹2成功");
        }
        mk.delete();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import java.io.*; /** * FileUtil. Simple file operation class. * * @author BeanSoft * */ public class FileUtil { /** * The buffer. */ protected static byte buf[] = new byte[1024]; /** * Read content from local file. FIXME How to judge UTF-8 and GBK, the * correct code should be: FileReader fr = new FileReader(new * InputStreamReader(fileName, "ENCODING")); Might let the user select the * encoding would be a better idea. While reading UTF-8 files, the content * is bad when saved out. * * @param fileName - * local file name to read * @return * @throws Exception */ public static String readFileAsString(String fileName) throws Exception { String content = new String(readFileBinary(fileName)); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(String fileName, String encoding) throws Exception { String content = new String(readFileBinary(fileName), encoding); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(InputStream in) throws Exception { String content = new String(readFileBinary(in)); return content; } /** * Read content from local file to binary byte array. * * @param fileName - * local file name to read * @return * @throws Exception */ public static byte[] readFileBinary(String fileName) throws Exception { FileInputStream fin = new FileInputStream(fileName); return readFileBinary(fin); } /** * 从输入流读取数据为二进制字节数组. * @param streamIn * @return * @throws IOException */ public static byte[] readFileBinary(InputStream streamIn) throws IOException { BufferedInputStream in = new BufferedInputStream(streamIn); ByteArrayOutputStream out = new ByteArrayOutputStream(10240); int len; while ((len

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值