Java File Operations

Source: Internet
Author: User

In Java, we call an object that can read a byte sequence an input stream, and we call it an output stream if we can write a byte sequence. They are represented by the inputstream and outputstream classes. Because byte-oriented streams are not convenient for processing Unicode (two bytes for each character) information. Therefore, Java introduces the class layers used to process Unicode characters. These classes are derived from the abstract classes reader and writer, which are used to read and write double-byte Unicode characters, rather than single-byte characters.

Java. Io package Introduction

The JDK standard help document explains the java. Io package in this way and provides input and output for the system through data streams, sequences, and file systems.

Inputstream and outputstream

The inputstream class is the parent class of all input data streams. It is an abstract class that defines the common features of all input data streams.

The Java. Io. inputstream method is as follows:

Public abstract read () throws ioexception

Read a byte and return this byte. If it reaches the end of the Input Source, return-1. A specific input stream class needs to reload this method to provide useful functions. For example, in the fileinputstream class, this method reads one byte from a file.

Public int read (byte [] B) throws ioexception

Reads data into a single byte and returns the actual number of bytes read. If the stream ends,-1 is returned. This method can read up to B. length bytes.

Public abstract int read (byte [] B, int off, int Len) throws ioexception

Read data into a byte array and return the actual number of bytes read. If the stream ends,-1 is returned. The off parameter indicates the position of the first byte in B, and Len indicates the maximum number of bytes read.

Public long SKIP (long n) throws ioexception

If n Bytes are skipped, the number of actually skipped bytes is returned. Because the remaining data in the data stream may be less than n Bytes, the returned value is less than N.

Public int available () throws ioexception

The read method (including the write method of the outputstream class to be discussed later) can plug a thread until the bytes are actually read or written. This means that if a stream cannot be read or written immediately

/*

* Created on 2005-3-10

* To change the template for this generated file go

* WINDOW> preferences> JAVA> code generation> code and comments

*/

Package mytestfiles;

Import java. Io. bufferedreader;

Import java. Io. file;

Import java. Io. filereader;

Import java. Io. filewriter;

Import java. Io. ioexception;

Import java. Io. printwriter;

/**

* @ Author zhangqinglin

* To change the template for this generated type comment go

* WINDOW> preferences> JAVA> code generation> code and comments

*/

Public class files

{

Public static void main (string [] ARGs) throws ioexception

{

Files F = new files ();

// System. Out. println (F. readfile ("F: // linkfile. Java "));

// F. readallfile ("F: //", "linkfile. Java ");

// F. readlinefile ("F: //", "linkfile. Java ");

// System. Out. println (F. fileisnull ("F: //", "122.txt "));

// F. readfolderbyfile ("F: // pdf ");

// System. Out. println (F. createanddeletefolder ("SS", "F ://"));

// System. Out. println (F. createanddeletefile ("F: // SS //", "testfile. dat "));

String [] Ss = new string [50];

For (INT I = 0; I <ss. length; I ++)

{

Ss [I] = "information technology and Internet (computer software and hardware, communication)" + I;

}

F. writefile ("F: // SS //", "testfile.txt", SS );

}

/**

* File writing

* @ Param filepath (file path)

* @ Param filename (file name)

* @ Param ARGs []

* @ Throws ioexception

*/

Public void writefile (string filepath, string filename, string [] ARGs) throws ioexception

{

Filewriter fw = new filewriter (filepath + filename );

Printwriter out = new printwriter (FW );

For (INT I = 0; I <args. length; I ++)

{

Out. Write (ARGs [I]);

Out. println ();

Out. Flush ();

}

FW. Close ();

Out. Close ();

}

/**

* File writing

* @ Param filepath (file path)

* @ Param filename (file name)

* @ Param ARGs

* @ Throws ioexception

*/

Public void writefile (string filepath, string filename, string ARGs) throws ioexception

{

Filewriter fw = new filewriter (filepath + filename );

FW. Write (ARGs );

FW. Close ();

}

/**

* Creating and deleting files

* @ Param filepath

* @ Param filename

* @ Return returns true if creation is successful.

* @ Throws ioexception

*/

Public Boolean createanddeletefile (string filepath, string filename) throws ioexception

{

Boolean result = false;

File file = new file (filepath, filename );

If (file. exists ())

{

File. Delete ();

Result = true;

System. Out. println ("the file has been deleted! ");

}

Else

{

File. createnewfile ();

Result = true;

System. Out. println ("the file has been created! ");

}

Return result;

}

/**

* Create and delete directories

* @ Param Foldername

* @ Param filepath

* @ Return returns true If deletion is successful.

*/

Public Boolean createanddeletefolder (string Foldername, string filepath)

{

Boolean result = false;

Try

{

File file = new file (filepath + Foldername );

If (file. exists ())

{

File. Delete ();

System. Out. println ("directory already exists, deleted! ");

Result = true;

}

Else

{

File. mkdir ();

System. Out. println ("the directory does not exist. It has been created! ");

Result = true;

}

}

Catch (exception ex)

{

Result = false;

System. Out. println ("createanddeletefolder is error:" + ex );

}

Return result;

}

/**

* Name of all files and directories in the output directory

* @ Param filepath

*/

Public void readfolderbyfile (string filepath)

{

File file = new file (filepath );

File [] tempfile = file. listfiles ();

For (INT I = 0; I <tempfile. length; I ++)

{

If (tempfile [I]. isfile ())

{

System. Out. println ("file:" + tempfile [I]. getname ());

}

If (tempfile [I]. isdirectory ())

{

System. Out. println ("Directory:" + tempfile [I]. getname ());

}

}

}

/**

* Check whether the file is empty.

* @ Param filepath

* @ Param filename

* @ Return: Return true if null.

* @ Throws ioexception

*/

Public Boolean fileisnull (string filepath, string filename) throws ioexception

{

Boolean result = false;

Filereader Fr = new filereader (filepath + filename );

If (Fr. Read () =-1)

{

Result = true;

System. Out. println (filename + "file has no data! ");

}

Else

{

System. Out. println (filename + "file contains data! ");

}

Fr. Close ();

Return result;

}

/**

* Read all the content in the file.

* @ Param filepath

* @ Param filename

* @ Throws ioexception

*/

Public void readallfile (string filepath, string filename) throws ioexception

{

Filereader Fr = new filereader (filepath + filename );

Int COUNT = Fr. Read ();

While (count! =-1)

{

System. Out. Print (char) count );

Count = Fr. Read ();

If (COUNT = 13)

{

Fr. Skip (1 );

}

}

Fr. Close ();

}

/**

* Reading data from a file in one row

* @ Param filepath

* @ Param filename

* @ Throws ioexception

*/

Public void readlinefile (string filepath, string filename) throws ioexception

{

Filereader Fr = new filereader (filepath + filename );

Bufferedreader BR = new bufferedreader (FR );

String line = Br. Readline ();

While (line! = NULL)

{

System. Out. println (line );

Line = Br. Readline ();

}

BR. Close ();

Fr. 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.