Java:io Operation Tool Class

Source: Internet
Author: User

[1] string Filereaderstringhandle (String fileName)

Reads the file (specified by filename) into a string;

[2] byte[] Filereaderbytehandle (String fileName)

Reads a file (specified by filename) into a byte array;

[3] void Filewriterhandle (String fileName, string text)

Writes a string (specified by text) to a file (specified by filename).

Ioutil.java

import Java.io.BufferedInputStream;


import Java.io.BufferedReader;


import Java.io.File;


import Java.io.FileInputStream;


import Java.io.FileReader;


import java.io.IOException;


import Java.io.PrintWriter;


public class Ioutil {


  /**


* Reads the file into a string, using Filereader+bufferedreader (provides ReadLine method)


   *


* @param fileName


* @return String


   */


public static string Filereaderstringhandle (String fileName) {


StringBuilder sb = new StringBuilder ();


try {


BufferedReader in = new BufferedReader (new FileReader (New File (


fileName). Getabsolutefile ());


try {


String S;


while ((s = in.readline ())!= null) {


Sb.append (s);


sb.append ("\ n");


       }


} finally {


In.close ();


      }


} catch (IOException e) {


throw new RuntimeException (e);


    }


return sb.tostring ();


  }


  /**


* Use Fileinputstream+bufferedinputstream to process files in byte


   *


* @param fileName


* @return byte[]


   */


public static byte[] Filereaderbytehandle (String fileName) {


byte[] data = null;


try {


bufferedinputstream bf = new Bufferedinputstream (


new FileInputStream (FileName));


try {


data = new byte[bf.available ()];


bf.read (data);


} finally {


Bf.close ();


      }


} catch (IOException e) {


throw new RuntimeException (e);


    }


return data = null? New byte[] {}: Data;


  }


  /**


* Writes the specified text to a file with file name filename


   *


* @param fileName


* @param text


   */


public static void Filewriterhandle (string fileName, string text) {


try {


PrintWriter out = new PrintWriter (new File (FileName)


. Getabsolutefile ());


try {


out.print (text);


} finally {


Out.close ();


      }


} catch (IOException e) {


throw new RuntimeException (e);


    }


  }


public static void Main (string[] args) throws IOException {


System.out.print (Filereaderstringhandle ("Src/ioutil.java"));





for (Byte B:filereaderbytehandle ("Src/ioutil.java"))


System.out.print (b);


filewriterhandle ("Zj.txt",


filereaderstringhandle ("Src/ioutil.java"));


  }


}

This article is from the "Zi Postman" blog, please be sure to keep this source http://zhangjunhd.blog.51cto.com/113473/74647

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.