Java-文件操作

Java-文件操作

主要记录一下操作方法没有任何教学意义

BufferReader 文件缓冲读取方式

package cn.fileio.output;

/**
 * 所需要的库
 */

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author Jensen
 * {@data 2022/11/13}
 */
public class BufferReaderTest {
    public static void main(String[] args) {
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader("/users/jensenhuang/desktop/test/s2project/src/cn/fileio/output/test.txt");
            br = new BufferedReader(fr);
            /**
             * 读取第一行
             */
            String line = br.readLine();
            /**
             * 这里会循环一行一行的读取文件里面内容直至循环之后没有内容了
             * 此时这个line就会为空
             * while判断就break;
             */
            while (line != null) {
                /**
                 * 循环一行输出一行
                 */
                System.out.println(line);
                /**
                 * 读取下一行因为在while里面所以它会一直循环直至文件里面没有东西了;
                 */
                line = br.readLine();
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            /**
             * 关闭文件
             */
            try {
                br.close();
                fr.close();
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

BufferWirter 文件缓冲写入方式

package cn.fileio.output;

import java.io.*;

/**
 * @author Jensen
 * {@data 2022/11/13}
 */
public class BuuferWirteTest {
    /**
     * 定义写入类
     */
    private static Writer fw = null;
    /**
     * 定义缓冲区写入方法
     */
    private static BufferedWriter bw = null;
    /**
     * 文件绝对路径
     */
    private static File file = new File("/Users/jensenhuang/Desktop/test/S2project/src/cn/baseclasspacket/output/myPrime.txt");
    /**
     * 缓冲读取文件方法定义
     */
    private static BufferedReader br = null;
    /**
     * 文件读取方法定义
     */
    private static FileReader fr = null;

    /**
     * 第一种写入方式
     * @throws IOException
     */
    public void write() throws IOException{
        /**
         * 定义文件写入目标文件
         */
        fw = new FileWriter(file);
        fw.write("写入木马!\nmr6=java.lang.Runtime.getRuntime().exec(“calc”);\n" +
                "var JFrame = Java.type(\"javax.swing.JFrame\");");
        fw.flush();
        fw.close();
    }

    /**
     * 第二种写入方式
     * @throws IOException
     */
    public void bufferWrite()throws IOException{
        fw = new FileWriter(file);
        /**
         * 导入预写入文件
         */
        bw = new BufferedWriter(fw);
        bw.write("写入木马!");
        bw.newLine();
        bw.write("mr6=java.lang.Runtime.getRuntime().exec(“calc”);");
        bw.newLine();
        bw.write("var JFrame = Java.type(\"javax.swing.JFrame\");");
        bw.flush();
        bw.close();
    }

    /**
     * 缓冲读取
     * @throws IOException
     */
    public void read() throws IOException{
        /**
         * 将目标文件导入进行进行读取操作
         */
        fr = new FileReader(file);
        /**
         * 通过上面的导入到FileReader
         * 再赋值到缓冲区进行循环读取
         */
        br = new BufferedReader(fr);
        String line = br.readLine();
        while (line != null) {
            System.out.println(line);
            line = br.readLine();
        }
        fr.close();
        br.close();
    }
    public static void main(String[] args) throws IOException {
        BuuferWirteTest t4 = new BuuferWirteTest();
        t4.bufferWrite();
        t4.read();
        // t4.write();

    }
}

FileInputStream 读取文件操作

package cn.fileio.output;

import javax.imageio.IIOException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author Jensen
 * {@data 2022/11/12}
 */
public class FileInputStreamTest {
    /**
     * 抛出异常
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        /**
         * 声名对象流
         */
        FileInputStream fis = null;
        try {
            /**
             * 定义一个对象流
             * 填写绝对路径
             */
            fis = new FileInputStream("/Users/jensenhuang/Desktop/test/S2project/src/cn/fileio/output/test.txt");
            /**
             * 由于后面需要通过int类型进行转换遍历读取其内容
             * 这里定义一个data属性
             * @param data
             */
            int data;
            /**
             * 输出内容长度
             */
            System.out.println("可读取的字节数: "+fis.available());
            /**
             * 这里遍历fis赋值里面的文件
             * read()方法是读取二进制流所以这里直接通过赋值给data
             * 然后进行判断是否为空
             * 为空则为false
             * 不为空则循环里面的内容通过类型转换成char类型进行输出
             */
            System.out.println("文件内容为: ");
            while ((data = fis.read()) !=-1){
                System.out.print((char)data+"");
            }
            /**
             * FIleNotFoundException进行文件是否存在错误捕获
             *
             */
        }catch (FileNotFoundException e){
            e.printStackTrace();
            /**
             * 捕获IO错误信息
             */
        }catch(IIOException e){
            e.printStackTrace();
            /**
             * 最后不管怎么样都会判断fis是否为空
             * 不为空则需要关闭读取流
             * 因为上面的操作对文件进行了操作是open状态
             */
        }finally {
            try {
                if (fis != null) {
                    fis.close();
                }
             /*
             * 捕获IO错误信息
             */
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

FileOutputStream 文件写入操作

package cn.fileio.output;

/**
 * @author Jensen
 * {@data 2022/11/12}
 */
import java.io.*;
import java.nio.charset.StandardCharsets;

public class FileOutputStreamTest {
    /**
     * 定义文件绝对路径
     */
    File file = new File("/Users/jensenhuang/Desktop/test/S2project/src/cn/fileio/output/test.txt");
    /**
     * 定义输入文本数据
     */
    String str = "mr6=java.lang.Runtime.getRuntime().exec(“calc”);";
    String str1 = "\nvar JFrame = Java.type(\"javax.swing.JFrame\");";

    /**
     * 定义开始操作文件
     */
    public void start() {
        /**
         * 将OutputStream 类 设置 方法名 为空
         */
        OutputStream out = null;
        try {
            /**
             * 定义输出文件流的源文件路径
             * 如果为true 则追加 否则直接覆盖原来文件内容
             * @param("append")
             */
            out = new FileOutputStream(file, true);
            /**
             * 写入数据
             * 将str字符数据进行bytes位转换并设置为Charsets的utf-8字符标准类型
             */
            out.write(str1.getBytes(StandardCharsets.UTF_8));
            /**
             * 刷新文件内容
             */
            out.flush();
            System.out.println("写入成功");
            /**
             * 捕获io异常
             */
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            /**
             * 关闭文件流
             */
            if (out != null) {
                try {
                    out.close();
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String[] args) {
        FileOutputStreamTest run = new FileOutputStreamTest();
        run.start();
    }


}
//参考文献: https://ispacesoft.com/48533.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值