【文件操作】换行存文件(1种)+按行读取文件(2种方法) 工具类


需求

编写工具类,实现功能如下:

  • 按行读取文件
  • 换行存文件

按行读取文件

方法一

该方法主要将文件的一部分或全部数据读取出来用BufferReader缓存,然后需要再从缓存中取数据。通过该方法使用,可以比需要得时候去文件中读取要快一些。

  /**
     * 按行读取文件 方法1
     * 将文件的一部分或全部数据读取出来用BufferReader缓存起来,需要再冲缓存中取数据,这样比要得时候去文件中读取要快一些。
     * @param filePath
     * @return
     */
    public  static  List readTxtFileIntoStringArrList(String filePath)
    {
        List list = new ArrayList();try{
        String encoding= "GBK";
        File file= new File(filePath);if (file.isFile() &&file.exists())
        {//判断文件是否存在
            InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);//考虑到编码格式
            BufferedReader bufferedReader = new BufferedReader(read);
            String lineTxt= null;
            while ((lineTxt = bufferedReader.readLine()) != null)
        {
            list.add(lineTxt);
        }
            bufferedReader.close();
            read.close();
        }else{
            System.out.println("找不到指定的文件");
        }
    }catch(Exception e)
    {
        System.out.println("读取文件内容出错");
        e.printStackTrace();
    }return list;
    }
  • filePath:存储文件路径。

方法二(推荐)

该方法是一次性把文本的原始内容直接读取到内存中再做处理(暂时不考虑内存大小),这样做效率会提高。而且用readline()之类的方法,可能需要反复访问文件,而且每次readline()都会调用编码转换,降低了速度。

/**
     * 推荐*****
     * 按行读取文件 方法2
     * 是一次把文本的原始内容直接读取到内存中再做处理(暂时不考虑内存大小),这样做效率也会提高
     * 用readline()之类的方法,可能需要反复访问文件,而且每次readline()都会调用编码转换,降低了速度,
     * 所以,在已知编码的情况下,按字节流方式先将文件都读入内存,再一次性编码转换是最快的方式。
     * @param filePath
     * @return
     */
    public static String[] readToString(String filePath) {
        File file = new File(filePath);
        Long filelength = file.length(); //获取文件长度
        byte[] filecontent = new byte[filelength.intValue()];
        try {
            FileInputStream in = new FileInputStream(file);
            in.read(filecontent);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String[] fileContentArr = new String(filecontent).split("\r\n");
        return fileContentArr;//返回文件内容,默认编码

    }
  • filePath:存储文件路径。

换行存文件

/**
     * 换行存文件
     * @param data
     */
    public  static void save (String data){
        try {
            File file = new File("C:\\Users\\孙海涛\\Desktop\\1.txt");
            FileOutputStream fos = new FileOutputStream(file,true);
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write(data);
            bw.newLine();
            bw.flush();
            bw.close();
            osw.close();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • data :文件存储的内容。

源码整理

package com.qcw.springcloud.controller;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 孙海涛
 * @Date 2022/7/7 20:44
 * @ProjectName cloud2020
 * @PackageName com.qcw.springcloud.controller
 */
public class FileUtil {
    public static void main(String[] args) {
        //按行读取文件
        readToString("C:\\Users\\孙海涛\\Desktop\\1.txt");
        //换行存内容s
        save("s");
    }
    /**
     * 换行存文件
     * @param data
     */
    public  static void save (String data){
        try {
            File file = new File("C:\\Users\\孙海涛\\Desktop\\1.txt");
            FileOutputStream fos = new FileOutputStream(file,true);
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write(data);
            bw.newLine();
            bw.flush();
            bw.close();
            osw.close();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 按行读取文件 方法1
     * 将文件的一部分或全部数据读取出来用BufferReader缓存起来,需要再冲缓存中取数据,这样比要得时候去文件中读取要快一些。
     * @param filePath
     * @return
     */
    public  static  List readTxtFileIntoStringArrList(String filePath)
    {
        List list = new ArrayList();try{
        String encoding= "GBK";
        File file= new File(filePath);if (file.isFile() &&file.exists())
        {//判断文件是否存在
            InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);//考虑到编码格式
            BufferedReader bufferedReader = new BufferedReader(read);
            String lineTxt= null;
            while ((lineTxt = bufferedReader.readLine()) != null)
        {
            list.add(lineTxt);
        }
            bufferedReader.close();
            read.close();
        }else{
            System.out.println("找不到指定的文件");
        }
    }catch(Exception e)
    {
        System.out.println("读取文件内容出错");
        e.printStackTrace();
    }return list;
    }
    /**
     * 推荐*****
     * 按行读取文件 方法2
     * 是一次把文本的原始内容直接读取到内存中再做处理(暂时不考虑内存大小),这样做效率也会提高
     * 用readline()之类的方法,可能需要反复访问文件,而且每次readline()都会调用编码转换,降低了速度,
     * 所以,在已知编码的情况下,按字节流方式先将文件都读入内存,再一次性编码转换是最快的方式。
     * @param filePath
     * @return
     */
    public static String[] readToString(String filePath) {
        File file = new File(filePath);
        Long filelength = file.length(); //获取文件长度
        byte[] filecontent = new byte[filelength.intValue()];
        try {
            FileInputStream in = new FileInputStream(file);
            in.read(filecontent);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String[] fileContentArr = new String(filecontent).split("\r\n");
        return fileContentArr;//返回文件内容,默认编码

    }
    
}


文件操作部分的记录就先到这!接下来,是一些整理的c++要点概要!

额外补充

迭代器

迭代器可以看成一个指针, 指针有==, 判断是否相等, 指针有取值*,指针有自加++, 所以会重载==、 *、 ++运算符, 没有<<或者>>。

长度获取方法

  • strlen()、 sizeof()是针对字符的
  • length()、 size()是针对字符串的

int a[3]={1, 2, 3}解析

  • *(a + 2)数组首元素地址+2 单元(1 单元地址偏移量是 4),所以是 3;
  • 而 * (&a +1)偏移量是整个数组的长度, 则首元素地址+1 单元(1 单元地址偏移量是 12) ,所以是 3 后面的 1 位

指针函数区分

  • 对于 int *ptr()而言, ptr 是一个函数名, 该函数的返回值是一个 int 类型的指针;
  • 对于 int (*ptr)()而言, ptr 是一个函数指针, 该函数的返回值是一个 int 类型的数;
  • 对于 int *(*ptr)()而言, ptr 是一个函数指针, 该函数的返回值是指向 int 型数据的指针。

x–解析

x–是在整行代码运行结束后再–, 所以 int x = 23;printf("%d", x--);最后输出 23。

struct 和Class

  • 如果没有定义属性, struct 成员默认是 public, 继承默认也是 public;
  • Class 类成员默认是private, 继承默认也是 private。

C++内存划分

C++程序在执行时, 将内存大方向划分为 4 个区域

  • 代码区: 存放函数体的二进制代码, 由操作系统进行管理的;
  • 全局区(静态区) (static) : 存放全局变量和静态变量以及常量;
  • 栈区(stack) : 由编译器自动分配释放, 存放函数的参数值,局部变量等;
  • 堆区(heap) : 由程序员分配和释放,若程序员不释放,程序结束时由操作系统回收。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

傻傻虎虎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值