首页 > 其他分享 >文件读取和写入工具类

文件读取和写入工具类

时间:2023-11-26 11:13:43浏览次数:29  
标签:文件 读取 写入 printStackTrace IOException file catch new null

Java IO流共涉及40多个类,但基类只有四个:

  • InputStream / Reader :所有输入流的基类,前者是字节输入流,后者是字符输 入流。
  • OutputStream / Writer :所有输出流的基类,前者是字节输出流,后者是字符输出 流。

BufferedReader整行读取

public static String readFile(String filename) {
    File file = new File(filename);
    InputStream is = null;
    Reader reader = null;
    BufferedReader bufferedReader = null;
    StringBuilder result = new StringBuilder();
    try {
        is = new FileInputStream(file);
        reader = new InputStreamReader(is);
        bufferedReader = new BufferedReader(reader);
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            result.append(line);
            result.append("\r\n");
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (null != bufferedReader)
                bufferedReader.close();
            if (null != reader)
                reader.close();
            if (null != is)
                is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result.toString();
}

InputStream字节读取

public static String readFileByChar(String filename) {
    File file = new File(filename);
    InputStream is = null;
    Reader isr = null;
    StringBuilder result = new StringBuilder();
    try {
        is = new FileInputStream(file);
        isr = new InputStreamReader(is);
        int index;

        while (-1 != (index = isr.read())) {
            result.append((char) index);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (null != is)
                is.close();
            if (null != isr)
                isr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result.toString();
}

BufferedWriter写文件

public static void writeByBuffered(String filename, String context) {
    File file = new File(filename);
    FileOutputStream fos = null;
    OutputStreamWriter osw = null;
    BufferedWriter bw = null;
    try {
        if (!file.exists()) {
            file.createNewFile();
        }
        fos = new FileOutputStream(file);
        osw = new OutputStreamWriter(fos);
        bw = new BufferedWriter(osw);
        bw.write(context);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (null != bw) {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (null != osw) {
            try {
                osw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (null != fos) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

OutputStreamWriter写文件

public static void write(String filename, String context) {
    File file = new File(filename);
    FileOutputStream fos = null;
    OutputStreamWriter osw = null;
    try {
        if (!file.exists()) {
            file.createNewFile();
        }
        fos = new FileOutputStream(file);
        osw = new OutputStreamWriter(fos);
        osw.write(context);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (osw != null) {
            try {
                osw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (null != fos) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

标签:文件,读取,写入,printStackTrace,IOException,file,catch,new,null
From: https://www.cnblogs.com/xfeiyun/p/17856628.html

相关文章

  • python 截取xlsx文件中某个时间段的数据
    Python截取xlsx文件中某个时间段的数据引言在日常工作和数据分析中,我们经常需要处理各种各样的数据文件。而其中一种较为常见的文件格式是Excel文件,尤其是.xlsx文件。Python作为一种强大的编程语言,提供了丰富的库和工具来处理Excel文件。本文将介绍如何使用Python截取.xlsx文件中......
  • python 读取文件名中带有循环变量
    标题:Python中使用循环变量读取文件名的方法**摘要:**在Python编程中,我们经常需要读取并处理多个文件。而文件名中的循环变量可以帮助我们更加灵活地处理这种情况。本文将介绍如何使用Python中的循环变量来读取文件名,并给出相关的代码示例和详细说明。1.引言在实际的数据处理中,我......
  • python 读取文件 with open
    Python读取文件withopen流程图flowchartTDA[开始]-->B[打开文件]B-->C[读取文件内容]C-->D[关闭文件]D-->E[结束]类图classDiagramclass文件文件:+打开文件()文件:+读取文件内容()文件:+关闭文件()代码实现打开......
  • python 读取模块内容
    Python读取模块内容介绍在Python中,我们可以使用import语句来导入一个模块,并读取其中的内容。这是非常重要的,因为模块使我们能够组织和重用代码。在本文中,我将向你介绍如何使用Python来读取模块内容。流程下面是读取模块内容的整个流程的概览。我们将按照以下步骤进行操作:journ......
  • python 读取xml为字符串
    读取XML为字符串的方法XML(eXtensibleMarkupLanguage)是一种用于存储和传输数据的标记语言。在Python中,我们可以使用xml模块来读取和处理XML文件。本文将介绍如何使用Python读取XML文件,并将其内容转换为字符串。1.导入模块首先,我们需要导入xml.etree.ElementTree模块,它提供了一......
  • python 读取 str存储的byte
    Python读取str存储的byte介绍在Python中,我们可以使用字符串(str)来存储二进制数据(byte)。但是,当我们需要读取这些存储在字符串中的字节时,我们需要进行一些特定的操作。本文将向你展示如何使用Python读取str存储的byte,并提供详细的步骤和示例代码。流程在开始具体的实现过程之前,我......
  • python 单击按钮弹出选择文件的窗口
    Python单击按钮弹出选择文件的窗口在编写Python程序时,有时我们需要让用户选择一个文件,例如读取文件内容进行处理,或者保存处理结果到指定的文件中。为了实现这个功能,我们可以使用tkinter库提供的文件选择对话框。tkinter库简介tkinter是Python的标准GUI库,可以用来创建各种图形用......
  • 从样板文件中导入样式到当前文档
    2023年11月24日QQ群友提出一个问题:SectionViewStyle的个别属性值无法获取,从而无法进行修改,开放的API中确实没有响应的枚举类型,从而无法使用SectionViewStyle.GetDisplayStylePlan()这个方法获取到DisplayStyle,于是我提出了采用导入样式的方法,提前准备好自己的样板文件,在样......
  • Eslint 的rules一些配置 (.eslintrc.js文件中的rules选项)
    rules:{//off=0,warn=1,error=2,如果是数组,第二项表示参数option//indent:[2,2],//控制缩进为2eqeqeq:1,//警告使用全等//quotes:[2,'single'],//单引号singleQuote:true,'no-console':0,//不禁用console'no-debugger......
  • 使用JavaWeb实现文件的上传和下载
    文件上传[文件上传的注意事项]1.为保证服务器安全,上传文件应该放在外界无法直接访问的目录下,比如放于WEB-INF目录下。2.为防止文件覆盖的现象发生,要为上传文件产生一个唯一的文件名3.要限制上传文件的最大值。4.可以限制上传文件的类型,在收到上传文件名时,判断后缀名是否合......