Home>

Java's method of reading and writing files is believed to have many uses in the work.I used to use java to read and write files to process data input and output, includingIt's really convenient.But my memory is really anxious,Many times, I can't remember how to write,But my java code volume is really poor.So you should practice a lot.To summarize here,Focus on aspects to look at in the future.

java read file

package genius idiot dream;
import java.io.bufferedreader;
import java.io.file;
import java.io.fileinputstream;
import java.io.filereader;
import java.io.ioexception;
import java.io.inputstream;
import java.io.inputstreamreader;
import java.io.randomaccessfile;
import java.io.reader;
public class javaio {
  / **
   * Adopts the default encoding method of the underlying operating system,gbk, etc., non-utf8
   * * /
  / **
   * Read the file content in bytes,Commonly used to read binary files,Such as pictures, images, sounds, etc.
   * * /
  public static void readfilebybytes (string filename) {
    file file=new file (filename);
    fileinputstream in=null;
    try {
      system.out.println ("read the file in bytes,Read one byte at a time:");
      in=new fileinputstream (file);
      int temp=0;
      while ((temp=in.read ())!=-1) {
        system.out.println (temp);
      }
      in.close ();
    } catch (ioexception e) {
      e.printstacktrace ();
      return;
    }
    try {
      system.out.println ("read the file in bytes,Read multiple bytes at once:");
      byte [] temp=new byte [100];
      int byteread=0;
      in=new fileinputstream (file);
      javaio.showavailablebytes (in);
      while ((byteread=in.read (temp))!=-1) {
        system.out.write (temp, 0, byteread);
      }
    } catch (exception e1) {
      e1.printstacktrace ();
    } finally {
      if (in!=null) {
        try {
          in.close ();
        } catch (ioexception e1) {
        }
      }
    }
  }
  / **
   * Read the file in characters,Often used to read text,Number and other types of files
   * * /
  public static void readfilebychar (string filename) {
    file file=new file (filename);
    reader reader=null;
    try {
      system.out.println ("reads the contents of the file in characters,One byte at a time:");
      // inputstreamreader class:is the bridge from byte to character conversion
      reader=new inputstreamreader (new fileinputstream (file));
      int temp;
      while ((temp=reader.read ())!=-1) {
        if (((char) temp)!="\ r") {
          system.out.println ((char) temp);
        }
      }
      reader.close ();
    } catch (exception e) {
      e.printstacktrace ();
    }
    try {
      system.out.println ("reads the contents of the file in characters,Read multiple bytes at once:");
      char [] temp=new char [30];
      int charread=0;
      reader=new inputstreamreader (new fileinputstream (filename));
      while ((charread=reader.read (temp))!=-1) {
        if ((charread == temp.length)&&(temp [temp.length-1]!="\ r")) {
          system.out.println (temp);
        } else {
          for (int i=0;i<charread;i ++) {
            if (temp [i] == "\ r") {
              break;
            } else {
              system.out.println (temp [i]);
            }
          }
        }
      }
    } catch (exception e) {
      e.printstacktrace ();
    } finally {
      if (reader!=null) {
        try {
          reader.close ();
        } catch (ioexception e) {
        }
      }
    }
  }
  / **
   * Read files in units of lines,Commonly used to read line-oriented formatted files
   * * /
  public static void readfilebyline (string filename) {
    file file=new file (filename);
    bufferedreader reader=null;
    try {
      system.out.println ("Read the contents of the file in units of lines,Read a whole line at a time:");
      reader=new bufferedreader (new filereader (file));
      string temp=null;
      int line=1;
      while ((temp=reader.readline ())!=null) {
        system.out.println ("line" + line + ":" + temp);
        line ++;
      }
      reader.close ();
    } catch (ioexception e) {
      e.printstacktrace ();
    } finally {
      if (reader!=null) {
        try {
          reader.close ();
        } catch (ioexception e) {
        }
      }
    }
  }
  / **
   * Randomly read file contents
   * * /
  public static void readfilebyrandomaccess (string filename) {
    randomaccessfile randomfile=null;
    try {
      system.out.println ("Read a random file content");
      randomfile=new randomaccessfile (filename, "r");
      long filelength=randomfile.length ();
      int beginindex=(filelength>4?4:0);
      randomfile.seek (beginindex);
      byte [] bytes=new byte [10];
      int byteread=0;
      while ((byteread=randomfile.read (bytes))!=-1) {
        system.out.write (bytes, 0, byteread);
      }
    } catch (ioexception e) {
      e.printstacktrace ();
    } finally {
      if (randomfile!=null) {
        try {
          randomfile.close ();
        } catch (ioexception e) {
        }
      }
    }
  }
  private static void showavailablebytes (inputstream in) {
    try {
      system.out.println ("The number of bytes in the current byte input stream is:" + in.available ());
    } catch (ioexception e) {
      e.printstacktrace ();
    }
  }
  public static void main (string [] args) {
    string filename="e:\\ baiyishaonian.txt";
    javaio.readfilebybytes (filename);
    javaio.readfilebychar (filename);
    javaio.readfilebyline (filename);
    javaio.readfilebyrandomaccess (filename);
  }
}

java write file

package genius idiot dream;
import java.io.bufferedwriter;
import java.io.file;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.filewriter;
import java.io.ioexception;
import java.io.outputstreamwriter;
public class javaio2 {
  public static void main (string [] args) throws ioexception {
    string path="e:\\ Genius idiot dream \\ java";
    file file=new file ("e:\\ Genius idiot dream \ java", "baiyishaonian.txt");
    if (! file.exists ()) {
      try {
        file.createnewfile ();
      } catch (ioexception e) {
        e.printstacktrace ();
      }
    }
    / **
     * Three ways to write files to java
     * * /
    fileoutputstream fos=null;
    bufferedwriter bw=null;
    filewriter fw=null;
    int value=1000;
    try {
      fos=new fileoutputstream (new file (path + "fos.txt"));
      long begin=system.currenttimemillis ();
      for (int i=1;i<= value;i ++) {
        fos.write (5);
      }
      long end=system.currenttimemillis ();
      system.out.println ("thecosttime of fileoutputstream is:" + (end-begin));
      fos.close ();
      bw=new bufferedwriter (new outputstreamwriter (new fileoutputstream (new file (path + "br.txt")), "utf8"));
      begin=system.currenttimemillis ();
      for (int i=1;i<= value;i ++) {
        bw.write (5);
        bw.newline ();
      }
      bw.close ();
      end=system.currenttimemillis ();
      system.out.println ("thecosttime of bufferedwriter is:" + (end-begin));
      fw=new filewriter (path + "fw.txt");
      begin=system.currenttimemillis ();
      for (int i=1;i<= value;i ++) {
        fw.write (5);
      }
      fw.close ();
      end=system.currenttimemillis ();
      system.out.println ("thecosttime of filewriter is:" + (end-begin));
    } catch (exception e) {
      // todo auto-generated catch block
      e.printstacktrace ();
    } finally {
      try {
        fos.close ();// fileoutputstream
        bw.close ();// bufferedwriter
        fw.close ();// filewriter
      } catch (exception e) {
        e.printstacktrace ();
      }
    }
  }
}
  • Previous jQuery easyUI datagrid add the implementation code of the sum statistics line
  • Next Basic socket programming in Python
  • Trends