Java file operation details

xiaoxiao2021-03-05  21

Input output stream

In Java, we call objects that can read a byte sequence as an input stream; and we write enough to write a word sequence called an output stream. They are represented by abstract class inputStream and OutputStream class. Because of the information-oriented stream inconvenient to process information stored as Unicode (using two bytes per character). So Java introduces a class hierarchy to handle Unicode characters, which are derived from abstract classes Reader and Writer, which are used to read and write the double-byte Unicode characters, not single-byte characters.

Java.IO package

The JDK standard help documentation explains the Java.IO package, providing input and output for the system through data streams, sequences, and file systems.

InputStream class and OutputStream class

The InputStream class is a parent class for all input data streams, which is an abstract class defined all of the input data streams have a common feature.

The method of java.io.inputStream is as follows:

Public Abstract Read () THROWS IOEXCEPTION

Read a byte and return this byte, if you return -1 if the end of the input source is returned. A specific input stream requires overloading this method to provide useful features. For example: In the FileInputStream class, this method reads a byte from a file.

Public int in (byte [] b) throws oException

Read the data into one byte data and return the number of bytes that actually read. If you encounter a stream, return -1, the method reads up to B.Length bytes.

Public Abstract Int Read (Byte [] B, INT OFF, INT LEN) THROWS IOEXCEPTION

Read the data into a byte array and return the number of bytes actually read. Returns -1 if the end of the stream is encountered. The parameter OFF represents the position of the first byte in B, and LEN represents the maximum number of bytes read.

Public Long Skip (long n) THROWS IOEXCEPTION

Skth N-bytes do not read, will return the number of bytes that actually skip. Since the remaining data in the data stream may not be so much in n bytes, the return value will be less than N.

Public int available () THROWS IOEXCEPTION

The read method (including the Write method of the OutputStream class you want later) can be blocked by one thread until the byte is actually read or written. This means that if a stream can't be read or written immediately

/ *

* CREATED ON 2005-3-10

* To change the template for this generated file go to

* 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 to to TO

* Window> Preferences> Java> Code Generation> Code and Comments

* /

Public Class Files

{

Public static void main (string [] args) throws oException

{

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 ///////////////////////////////////////////////////////////////////>

String [] ss = new string [50];

For (int i = 0; i

{

SS [I] = "Information Technology and Internet (Computer Software Hardware, Communication)" i;

}

F.Writefile ("f: // ss ///", "testfile.txt", ss);

}

/ **

* Write

* @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

{

Out.write (Args [i]);

Out.println ();

Out.flush ();

}

Fw.close ();

Out.close ();

}

/ **

* Write

* @param filepath (file path)

* @Param FileName (file name)

* @Param Args

* @Throws ioException

* /

Public void Writefile (String Filepath, String FileName, String Args) throws oException

{

FileWriter FW = New FileWriter (FilePath FileName);

Fw.write (args);

Fw.close ();

}

/ **

* Create and delete files

* @Param FilePath

* @Param FileName

* @return creates successful returns true

* @Throws ioException

* /

Public Boolean CreateAnddeletefile (String Filepath, String FileName) Throws oException

{

Boolean Result = false;

File File = New File (FilePath, FileName);

IF (file.exists ())

{

File.delete ();

Result = TRUE;

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

}

Else

{

File.createNewFile ();

Result = TRUE;

System.out.println ("File has been created!");

}

Return Result;

}

/ **

* Create and delete the directory

* @Param Foldername

* @Param FilePath

* @return Delete Success Back to TRUE

* /

Public Boolean CreateAndDeletefolder (String Filepath)

{

Boolean Result = false;

Try

{

File File = New File (FilePath foldername);

IF (file.exists ())

{

File.delete ();

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

Result = TRUE;

}

Else

{

File.mkdir ();

System.out.println ("The directory does not exist, established!");

Result = TRUE;

}

}

Catch (Exception EX)

{

Result = false;

System.out.println ("CreateAndDeletefolder IS Error:" EX);

}

Return Result;

}

/ **

* Output all files and directory names in the directory

* @Param FilePath

* /

Public void readfolderbyfile (String Filepath)

{

File File = New File (FilePath);

File [] tempfile = file.listfiles ();

For (int i = 0; i

{

IF (tempfile [i] .isfile ())

{

System.out.println ("File:" TempFile [i] .getname ());

}

IF (TempFile [i] .Indirectory ())

{

System.out.println ("Directory:" TempFile [i] .getname ());

}

}

}

/ **

* Check if there is an empty in the file

* @Param FilePath

* @Param FileName

* @return returns to TRUE

* @Throws ioException

* /

Public Boolean Fileisnull (String Filepath, String FileName) Throws oException

{

Boolean Result = false;

FileReader fr = new fileReader (FilePath FileName);

IF (fr.read () == -1)

{

Result = TRUE;

There is no data in System.out.Println (FileName "file!");

}

Else

{

System.out.println (filename "file has data!");

}

Fr.close ();

Return Result;}

/ **

* Read all content in the file

* @Param FilePath

* @Param FileName

* @Throws ioException

* /

Public void readallfile (string filepath, string filename) Throws oException

{

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 ();

}

/ **

* Data in a line of reading

* @Param FilePath

* @Param FileName

* @Throws ioException

* /

Public void readlinefile (String Filepath, String FileName) Throws oException

{

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 ();

}

}

转载请注明原文地址:https://www.9cbs.com/read-36255.html

New Post(0)