• Java读写txt文件


    1、Java读取txt文件

    1.1、使用FileInputStream:

    public static String readFile(File file, String charset){
            //设置默认编码
            if(charset == null){
                charset = "UTF-8";
            }
            
            if(file.isFile() && file.exists()){
                try {
                    FileInputStream fileInputStream = new FileInputStream(file);
                    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charset);
                    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                    
                    StringBuffer sb = new StringBuffer();
                    String text = null;
                    while((text = bufferedReader.readLine()) != null){
                        sb.append(text);
                    }
                    return sb.toString();
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
            return null;
        }

    最后此函数将会返回读取到的内容。当然,也可以在读取的过程中进行逐行处理,不必要一次性读出再进行处理。

    而且,bufferedReader还有read方法,满足更多的需要。下去我自己可以试一试,再补充。


    2、Java写入txt文件

    2.1、使用FileWriter方式:

    /**
     * 以FileWriter方式写入txt文件。
     * @param File file:要写入的文件
     * @param String content: 要写入的内容
     * @param String charset:要写入内容的编码方式
     */
    public static void writeToFile1(){
        
        try {
            String content = "测试使用字符串";
            File file = new File("./File/test1.txt");
            if(file.exists()){
                FileWriter fw = new FileWriter(file,false);
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(content);
                bw.close(); fw.close();
                System.out.println("test1 done!");
            }
            
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    这种方式简单方便,代码简单。顺带一提的是:上述代码是清空文件重写,要想追加写入,则将FileWriter构造函数中第二个参数变为true。

     

    2.2、文件不存在时候,主动创建文件。

    public static void writeToFile2(){    
        try {
            String content = "测试使用字符串";
            File file = new File("./File/test2.txt");
            //文件不存在时候,主动穿件文件。
            if(!file.exists()){
                file.createNewFile();
            }
            FileWriter fw = new FileWriter(file,false);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close(); fw.close();
            System.out.println("test2 done!");
            
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

     关键性的话语在于file.createNewFile();

    2.3、使用FileOutputStream来写入txt文件。

    public static void writeToFile3(){    
       String content = "测试使用字符串";
       FileOutputStream fileOutputStream = null;
       File file = new File("./File/test3.txt");
       
       try {
           if(file.exists()){
               file.createNewFile();
           }
           
           fileOutputStream = new FileOutputStream(file);
           fileOutputStream.write(content.getBytes());
           fileOutputStream.flush();
           fileOutputStream.close();
       } catch (Exception e) {
           // TODO: handle exception
       }
       System.out.println("test3 done");
    
    }

    使用输出流的方式写入文件,要将txt文本转换为bytes写入。


    3、总结

    1、对于写入文件路径的问题,我发现文件路径是以本项目为根目录的,也就是说:写文件路径的时候,要么写成绝对路径,防止错误;要么 以本项目为根目录写相对路径。

    举一个例子:

    |project/

    |----src/

    |       |----test.java

    |----File

    |       |----test1.txt

    |       |----test2.txt

    |       |----test3.txt

    要想访问到test1.txt,路径要写作绝对路径,或者相对路径:./File/test1.txt

    2、对于输入输出流读写文件,操作完成后要关闭流。

    以后再进行补充学习,暂时就这样记录下来。

  • 相关阅读:
    char类型细节
    Hibernate面试题
    线程
    IO流
    集合
    链表相关的一点东西
    正则表达式学习
    python中的变量域问题
    python的输出和输入形式
    python mutable 和 immutable
  • 原文地址:https://www.cnblogs.com/yanwenxiong/p/5584556.html
Copyright © 2020-2023  润新知