[Java] various methods for reading and writing files and small examples

Source: Internet
Author: User

1. Use filewriter and filereader to read and write files

Import Java. io. *; <br/> public class javaapplication2 {</P> <p>/** <br/> * @ Param ARGs the command line arguments <br/> */<br/> public static void main (string [] ARGs) throws filenotfoundexception, ioexception {</P> <p> filewriter fw = new filewriter ("test.txt"); <br/> FW. write ("dddd"); // just do this .. it will automatically convert to its unicode encoding .. or FW. write (INT) C); force conversion. <br/> FW. append ("wwwwww"); <br/> FW. flush (); <br/> FW. close ( ); </P> <p> filereader Fr = new filereader ("test.txt"); <br/> int CH = 0; <br/> while (CH = Fr. read ())! =-1) <br/>{< br/> system. Out. Print (char) CH); <br/>}< br/>}Ii. Examples of Multiple File Reading Methods

1. Read File Content by byte
2. Read File Content by character
3. Read File Content by row
4. Randomly Read File Content

<Br/> package readfile; <br/> Import Java. io. bufferedreader; <br/> Import Java. io. file; <br/> Import Java. io. fileinputstream; <br/> Import Java. io. filereader; <br/> Import Java. io. ioexception; <br/> Import Java. io. inputstream; <br/> Import Java. io. inputstreamreader; <br/> Import Java. io. randomaccessfile; <br/> Import Java. io. reader; <br/> public class readfromfile {<br/>/** <br/> * reads files in bytes. It is often used to read binary files, slices, sounds, Images and other files. <Br/> * @ Param filename file name <br/> */<br/> Public static void readfilebybytes (string filename) {<br/> file = new file (filename); <br/> inputstream in = NULL; <br/> try {<br/> system. out. println ("Read File Content in bytes, one byte at a time :"); <br/> // read one byte at a time <br/> In = new fileinputstream (File); <br/> int tempbyte; <br/> while (tempbyte = in. read ())! =-1) {<br/> system. out. write (tempbyte); <br/>}< br/> in. close (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/> return; <br/>}< br/> try {<br/> system. out. println ("Read File Content in bytes, read multiple bytes at a time :"); <br/> // read multiple bytes at a time <br/> byte [] tempbytes = new byte [100]; <br/> int byteread = 0; <br/> In = new fileinputstream (filename); <br/> readfromfile. showavailablebytes (in); <br/> // read multiple bytes into the byte array. Read is the number of bytes read at a time <br/> while (byteread = in. Read (tempbytes ))! =-1) {<br/> system. out. write (tempbytes, 0, byteread); <br/>}< br/>} catch (exception E1) {<br/> e1.printstacktrace (); <br/>} finally {<br/> If (in! = NULL) {<br/> try {<br/> in. close (); <br/>} catch (ioexception E1) {<br/>}< br/>/** <br/> * reads a file in characters, commonly used for reading text, numbers, and other types of files <br/> * @ Param filename file name <br/> */<br/> Public static void readfilebychars (string filename) {<br/> file = new file (filename); <br/> reader = NULL; <br/> try {<br/> system. out. println ("Read File Content in characters, one byte at a time:"); <br/> // read one character at a time <br/> reader = N EW inputstreamreader (New fileinputstream (File); <br/> int tempchar; <br/> while (tempchar = reader. Read ())! =-1) {<br/> // for Windows, when the RN characters are together, a line break is displayed. <Br/> // if the two characters are displayed separately, the rows are changed twice. <Br/> // Therefore, block R or N. Otherwise, there will be a lot more blank lines. <Br/> If (char) tempchar )! = 'R') {<br/> system. out. print (char) tempchar); <br/>}< br/> reader. close (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> try {<br/> system. out. println ("Read File Content in characters, multiple bytes read at a time :"); <br/> // read multiple characters at a time <br/> char [] tempchars = new char [30]; <br/> int charread = 0; <br/> reader = new inputstreamreader (New fileinputstream (filename); <br/> // read multiple characters into the character array, charread is the number of characters read at a time <B R/> while (charread = reader. Read (tempchars ))! =-1) {<br/> // If (charread = tempchars. length) & (tempchars [tempchars. length-1]! = 'R') {<br/> system. out. print (tempchars); <br/>}else {<br/> for (INT I = 0; I <charread; I ++) {<br/> If (tempchars [I] = 'R') {<br/> continue; <br/>}< br/> else {<br/> system. out. print (tempchars [I]); <br/>}< br/>} catch (exception E1) {<br/> e1.printstacktrace (); <br/>}finally {<br/> If (reader! = NULL) {<br/> try {<br/> reader. close (); <br/>} catch (ioexception E1) {<br/>}< br/>/** <br/> * read files in behavior units, commonly used to read row-oriented formatted files <br/> * @ Param filename file name <br/> */<br/> Public static void readfilebylines (string filename) {<br/> file = new file (filename); <br/> bufferedreader reader = NULL; <br/> try {<br/> system. out. println ("read the file content in the unit of action, one whole row at a time:"); <br/> reader = new bufferedreade R (New filereader (File); <br/> string tempstring = NULL; <br/> int line = 1; <br/> // read a row at a time, until the end of reading null is the file <br/> while (tempstring = reader. readline ())! = NULL) {<br/> // display the row number <br/> system. out. println ("line" + LINE + ":" + tempstring); <br/> line ++; <br/>}< br/> reader. close (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}finally {<br/> If (reader! = NULL) {<br/> try {<br/> reader. close (); <br/>} catch (ioexception E1) {<br/>}< br/>/** <br/> * Randomly Read File content <br/> * @ Param filename file name <br/> */<br/> Public static void readfilebyrandomaccess (string filename) {<br/> randomaccessfile randomfile = NULL; <br/> try {<br/> system. out. println ("randomly read a file:"); <br/> // open a random access file stream in read-only mode <br/> randomfile = new randomaccessfile (filenam E, "R"); <br/> // file length, number of bytes <br/> long filelength = randomfile. length (); <br/> // start position of the file to be read <br/> int beginindex = (filelength> 4 )? 4: 0; <br/> // move the start position of the read file to the beginindex position. <Br/> randomfile. seek (beginindex); <br/> byte [] bytes = new byte [10]; <br/> int byteread = 0; <br/> // read 10 bytes at a time. If the file content is less than 10 bytes, read the remaining bytes. <Br/> // assign the number of bytes read at a time to byteread <br/> while (byteread = randomfile. Read (bytes ))! =-1) {<br/> system. out. write (bytes, 0, byteread); <br/>}< br/>} catch (ioexception e) {<br/> E. printstacktrace (); <br/>}finally {<br/> If (randomfile! = NULL) {<br/> try {<br/> randomfile. close (); <br/>} catch (ioexception E1) {<br/>}< br/>/** <br/> * display the remaining bytes in the input stream count <br/> * @ Param in <br/> */<br/> Private Static void showavailablebytes (inputstream in) {<br/> try {<br/> system. out. println ("number of bytes in the Current byte input stream:" + in. available (); <br/>} catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/> Public static void main (string [] ARGs) {<br/> string filename = "D: /t.txt "; <br/> readfromfile. readfilebybytes (filename); <br/> readfromfile. readfilebychars (filename); <br/> readfromfile. readfilebylines (filename); <br/> readfromfile. readfilebyrandomaccess (filename); <br/>}< br/>

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.