欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-07 18:48:09
本文实例总结了java中关于文本文件的读写方法。分享给大家供大家参考,具体如下: 写文本数据 方法 一: import java.io.*; public...

本文实例总结了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程序设计有所帮助。