Home>

Write text data

method one:

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 ());
    }
  }
}

Method Two:

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 ());
    }
  }
}

Method three:

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 ());
    }
  }
}

Note:Method 1 and Method 2,The third method is to create when the operation text file does not exist.Otherwise, when covered!

Another;method three

bufferedwriter writes text to a character output stream,Buffer individual characters,This provides efficient writing of single characters, arrays, and strings.

Attachment:additional write:

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 ());
    }
  }
}

Read text data

method one:

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 ());
    }
  }
}

Method Two:

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 ());
    }
  }
}

Attachment:Method 2 can efficiently read out text data

  • Previous Summary of ten NET exception handling recommendations
  • Next Explain what the instanceof keyword and the instanceof keyword do in PHP
  • Trends