java中关于文本文件的读写方法实例总结

所属分类: 软件编程 / java 阅读数: 49
收藏 0 赞 0 分享

本文实例总结了java中关于文本文件的读写方法。分享给大家供大家参考,具体如下:

写文本数据

方法 一:

import java.io.*;
public class A {
  public static void main(String args[]) {
    FileOutputStream out;
    PrintStream ps;
    try {
      out = new FileOutputStream("a.txt");
      ps = new PrintStream(out);
      ps.println("qun qun.");
      ps.println("fei fei");
      ps.close();
    } catch (Exception e) {
      System.out.println(e.toString());
    }
  }
}

方法 二:

import java.io.*;
public class B {
  public static void main(String args[]) {
    FileWriter fw;
    PrintWriter pw;
    try {
      fw = new FileWriter("b.txt");
      pw = new PrintWriter(fw);
      pw.print("qunqu n ");
      pw.println("feiefi ss");
      pw.print("qunqu n ");
      pw.close();
      fw.close();
    } catch (IOException e) {
      System.out.println(e.toString());
    }
  }
}

方法三:

import java.io.*;
public class C {
  public static void main(String args[]) {
    String str_written = "This is a simple example";
    try {
      FileWriter fwriter = new FileWriter("c.txt");
      BufferedWriter bfwriter = new BufferedWriter(fwriter);
      bfwriter.write(str_written, 0, str_written.length());
      bfwriter.flush();
      bfwriter.close();
    } catch (IOException e) {
      System.out.println(e.toString());
    }
  }
}

附注:方法一和方法二,方法三都是在操作文本文件不存在的时候将创建,否则,当覆盖之!

另;方法三

BufferedWriter将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。

附:追加写入:

import java.io.*;
public class C {
  public static void main(String args[]) {
    String str_written = "This is a simple example";
    try {
      FileWriter fwriter = new FileWriter("c.txt", true);
      BufferedWriter bfwriter = new BufferedWriter(fwriter);
      bfwriter.newLine();
      bfwriter.write(str_written, 0, str_written.length());
      bfwriter.flush();
      bfwriter.close();
    } catch (IOException e) {
      System.out.println(e.toString());
    }
  }
}

读文本数据

方法一:

import java.io.*;
public class A {
  public static void main(String args[]) {
    try {
      FileInputStream fstream = new FileInputStream("a.txt");
      DataInputStream in = new DataInputStream(fstream);
      while (in.available() != 0) {
        String a = in.readLine();
        System.out.println(a);
        System.out.println(a.length());
      }
      in.close();
    } catch (Exception e) {
      System.out.println(e.toString());
    }
  }
}

方法二:

import java.io.*;
public class B {
  public static void main(String args[]) {
    try {
      FileReader fr = new FileReader("a.txt");
      BufferedReader br = new BufferedReader(fr);
      String str;
      int count = 0;
      while ((str = br.readLine()) != null) {
        count++;
        System.out.println(count + " : " + str);
      }
      br.close();
      fr.close();
    } catch (Exception e) {
      System.out.println(e.toString());
    }
  }
}

附:方法二的能够高效的实现文本数据的读出

希望本文所述对大家Java程序设计有所帮助。

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多