美文网首页Java学习笔记
java读写文本文件

java读写文本文件

作者: ciantian | 来源:发表于2017-09-15 10:44 被阅读249次

java的操作相对python繁琐一点,所以这里把一些常用的记录下来。


  1. 判断文件夹是不是存在,不存在创建
        String path = "/home/tianchaoxiong/LinuxData/data/verifies/";
        File file =new File(path);   
        //如果文件夹不存在则创建   
        if  (!file.exists())     
        {     
            file.mkdir();   
        }
  1. 判断文件是不是存在,不存在创建
        String fileName = "ceshi.txt";
        File filename = new File(path+fileName);
        if(!file.exists())   
        {   
            try {   
                file.createNewFile();//不存在直接创建   
            } catch (IOException e) { 
                e.printStackTrace();   
            }   
        } 

  1. java 从指定路径中读文本文件

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class OptTxt {
    public static void main(String[] args) throws IOException {
        readTxt();
        // saveTxt();
    }

    private static void readTxt() throws IOException {
        // 可为文本相对路劲,可以文件夹绝对路径
        String path3 = "ceshi.txt";
        // StringBuffer用来接收解析完了之后的文本内容
        StringBuffer sb = new StringBuffer();
        // 自定义函数读文本 返回一个StringBuffer
        readToBuffer(sb, path3);
        // StringBuffer转为String显示
        String resultString = sb.toString();
        System.out.println(resultString);

    }

    public static void readToBuffer(StringBuffer buffer, String filePath) throws IOException {
        InputStream is = new FileInputStream(filePath);
        String line; // 用来保存每行读取的内容
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        line = reader.readLine(); // 读取第一行
        while (line != null) { // 如果 line 为空说明读完了
            buffer.append(line); // 将读到的内容添加到 buffer 中
            buffer.append("\n"); // 添加换行符
            line = reader.readLine(); // 读取下一行
        }
        reader.close();
        is.close();
    }
}

  1. 把txt写入指定路径的文件文件中。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class OptTxt {
    public static void main(String[] args) throws IOException {
        // readTxt();
        saveTxt();
    }

    private static void saveTxt() throws IOException {
        // 可为文本相对路劲,可以文件夹绝对路径
        String path = "/home/tianchaoxiong/LinuxData/data/verifies/";
        File file = new File(path);
        // 如果文件夹不存在则创建
        if (!file.exists()) {
            file.mkdir();
        }
        // 如果文件不存在则创建
        String fileName = "ceshi.txt";
        File filename = new File(path + fileName);
        if (!file.exists()) {
            try {
                file.createNewFile();// 不存在直接创建
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 写入内容
        String content = "This is the content to write into file";
        writeToBuffer(content, filename);
    }

    private static void writeToBuffer(String content, File filename) {
        try {
            FileWriter fw = new FileWriter(filename.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
            System.out.println("success");
        } catch (IOException e) {
            System.out.println("fail");
            e.printStackTrace();
        }

    }
}

有缘至此,连接彼此

相关文章

  • java读写文本文件

    java的操作相对python繁琐一点,所以这里把一些常用的记录下来。 判断文件夹是不是存在,不存在创建 判断文件...

  • Java 读写文本文件

    读取txtFileFileInputStreamInputStreamReaderBufferedReader 写...

  • 2.2、Python进阶02 文本文件的输入输出

    Python具有基本的文本文件读写功能。Python的标准库提供了更丰富的读写功能。文本文件的读写主要通过open...

  • Java IO流之拷贝(复制)文件

    方式一(字符流读写复制文件,仅限文本文件) 方式二(字符流缓冲区读写文件-高效,仅限文本文件) 方式三(字节流读写...

  • Python文本文件的输入输出操作学习

    Python具有基本的文本文件读写功能。Python的标准库提供有更丰富的读写功能。 文本文件的读写主要通过ope...

  • 使用Haskell读写文件

    1. 直接读写文本文件 Haskell的Prelude库提供了两个函数用来直接读写文本文件,分别是readFile...

  • C# File类创建读写修改文本文件

    上一篇使用读写流创建和修改,读取文本文件,本篇使用File类创建,修改,读写文本文件。 1. 创建 Write c...

  • day09 作业 2018-07-26

    文本文件读写 滚滚长江东逝水   二进制文件读写   json文件读写   姓名灰太狼电话234567[{'nam...

  • 文件和io(cookbook笔记)

    读写文本数据 读写文本数据 读写各种不同编码的文本数据 使用带有 rt 模式的 open() 函数读取文本文件 写...

  • 文本文件读写

    一、文件的打开 1.普通文件:数据持久化的最简单类型,仅仅在一个文件名下的字节流(流的意思是按照顺序一个字节以个字...

网友评论

    本文标题:java读写文本文件

    本文链接:https://www.haomeiwen.com/subject/njlssxtx.html