Java Read File Method encyclopedia _java

Source: Internet
Author: User

1, read the contents of the file by byte

public class ReadFromFile {public

static void Readfilebybytes (String fileName) {
file File = new File (fileName); C3/>inputstream in = null;
try {
System.out.println (reads the contents of the file in bytes, one byte at a time: ");
Read one byte at a time in
= new FileInputStream (file);
int tempbyte;
while ((Tempbyte = In.read ())!=-1) {
System.out.write (tempbyte);
}
In.close ();
} catch (IOException e) {
e.printstacktrace ();
return;
}
try {
System.out.println (read the contents of the file in bytes, read multiple bytes at a time: ");
Read multiple bytes at a time
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream (fileName);
Readfromfile.showavailablebytes (in);
Reads multiple bytes into a byte array, byteread the number of bytes read in while
(Byteread = In.read (tempbytes))!=-1) {
System.out.write ( Tempbytes, 0, Byteread);
}
catch (Exception E1) {
e1.printstacktrace ();
} finally {
if (in!= null) {
try {
in.close (); c34/>} catch (IOException E1) {
}}}


2, read the contents of the file by character

public static void Readfilebychars (String filename) {File File = new file (fileName);
Reader reader = null; try {System.out.println (read the contents of the file in characters, one byte at a time:);//read one character reader = new InputStreamReader (new FileInputStream (file))
;
int Tempchar;
while ((Tempchar = Reader.read ())!=-1) {//for Windows, \ r \ n These two characters together indicate a newline.
However, if the two characters are displayed separately, the lines will be changed two times. So, block \ r, or block \ n.
Otherwise, there will be a lot more empty lines.
if ((char) tempchar)!= ' \ R ') {System.out.print ((char) tempchar);}
Reader.close (); catch (Exception e) {e.printstacktrace ();} try {System.out.println (read the contents of the file in characters, read multiple bytes at a time:);//Read multiple characters at once char[] tempch
ARS = new CHAR[30];
int charread = 0;
reader = new InputStreamReader (fileName) (new FileInputStream); Reads more than one character into an array of characters, charread as the number of read characters while (Charread = Reader.read (tempchars))!=-1) {//also shielded \ r does not display if (Charread = tempch Ars.length) && (tempchars[tempchars.length-1]!= ' \ R ')) {System.out.print (tempchars);} else {for (int i = 0; i < Charread; i++) {if (tempchars[i] = = ' \ r ') {continue} ElSE {System.out.print (tempchars[i]);} {E1.printstacktrace ()}} catch (Exception E1) {()} finally {if (reader!= null) {try {reader.close ();} catch (Ioexc Eption E1) {}}}}

3, read the contents of the file by line

public static void Readfilebylines (String filename) {
File File = new file (fileName);
BufferedReader reader = null;
try {
System.out.println ("reads the contents of the file in a unit of behavior, reads one whole line at a time:");
reader = new BufferedReader (new FileReader (file));
String tempstring = null;
int line = 1;
Read one row at a time until you read null for file end while
((tempstring = Reader.readline ())!= null) {
//Show line number
System.out.println (" Line "+ line +": "+ tempstring";
line++;
}
Reader.close ();
} catch (IOException e) {
e.printstacktrace ();
} finally {
if (reader!= null) {
try {
Reader.close ();
} catch (IOException E1) {
}}}

4, Random Read the contents of the file

public static void Readfilebyrandomaccess (String fileName) {randomaccessfile randomfile = null; try {SYSTEM.OUT.PRINTLN
("read the contents of a document randomly:");
Open a random Access file stream, read-only randomfile = new Randomaccessfile (FileName, "R");
File length, byte number long filelength = Randomfile.length (); Read the starting position of the file int beginindex = (Filelength > 4)?
4:0;
Moves the start position of the read file to the Beginindex location.
Randomfile.seek (Beginindex);
byte[] bytes = new BYTE[10];
int byteread = 0;
Read 10 bytes at a time and read the remaining bytes if the file content is less than 10 bytes. Assigns a read number of bytes to Byteread 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 (Ioex Ception E1) {}}}} 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 = ' c:/temp/newtemp.txt '; Readfromfile.readfilebybytes(FileName);
Readfromfile.readfilebychars (FileName);
Readfromfile.readfilebylines (FileName);
Readfromfile.readfilebyrandomaccess (FileName); }
}

5, append content to file tail

public class Appendtofile {public static void Appendmethoda (string fileName, string content) {try {//Open a random Access file stream, read/write
The way randomaccessfile randomfile = new Randomaccessfile (fileName, "RW");
File length, byte number long filelength = Randomfile.length ();
Moves the write file pointer to the end of the file.
Randomfile.seek (filelength);
Randomfile.writebytes (content);
Randomfile.close ();


catch (IOException e) {e.printstacktrace ();}}  public static void Appendmethodb (string fileName, string content) {try {//Open a write-file, the second argument in the constructor is true to write the file in append form FileWriter
writer = new FileWriter (FileName, true);
Writer.write (content);
Writer.close ();

catch (IOException e) {e.printstacktrace ();}}
public static void Main (string[] args) {String fileName = ' c:/temp/newtemp.txt ';
String content = "New append!";
Append file Appendtofile.appendmethoda (filename, content) by method A; Appendtofile.appendmethoda (FileName, "append end.")
\ n ");
Displays the contents of the file Readfromfile.readfilebylines (fileName);
Append file Appendtofile.appendmethodb (filename, content) by method B; Appendtofile.appenDmethodb (FileName, "append end.")
\ n ");
Displays the contents of the file Readfromfile.readfilebylines (fileName); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.