[JAVA] Read file methods Daquan __java

Source: Internet
Author: User

Original address: http://www.cnblogs.com/lovebread/archive/2009/11/23/1609122.html

[Java] Read file method encyclopedia 1, read the contents of the file by byte 2, read the contents of the file by character 3, read the contents of the file by line 4, Random read the contents of the file

public class ReadFromFile {/**
* 1, read files in bytes, often used to read binary files, such as pictures, sounds, images and other documents.
*/
public static void Readfilebybytes (String fileName) {
File File = new file (fileName);
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 at a time
while ((Byteread = In.read (tempbytes))!=-1) {
System.out.write (tempbytes, 0, Byteread);
}
catch (Exception E1) {
E1.printstacktrace ();
finally {
if (in!= null) {
try {
In.close ();

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.