File processing class

xiaoxiao2021-04-10  370

DEALSTRING.JAVA

First, the description:

1. A class that is mainly responsible for string processing

Second, the variable:

Serial number

variable name

Application range

Variable type

Initial value

Whether static

effect

1

Infile

Package

String

""

no

Enter a file

2

Outfile

Package

String

""

no

Output file

3

FIS

Package

FileInputStream

NULL

no

Enter file stream

4

FOS

Package

FileOutputstream

NULL

no

Output file stream

Third, the method brief:

Serial number

Method name

parameter

return value

Features

1

Connfis

String strfile

Void

Create a file input stream, the file path specified in the parameter

FIS = New FileInputStream (Infile);

2

Connfos

String strfile

Void

Create file output stream, file path specified in parameters

FOS = New fileOutputStream (outfile);

3

Closefis

no

Void

Turn off the input file stream

fis.close ()

4

Closefos

no

Void

Turn off output file stream

fos.close ()

5

Getfis

no

FileInputStream

Get input stream

Return FIS

6

Getfos

no

FileOutputstream

Acquire output flow

Return fos;

Seduce

Deletefile

Strign Strfile

Void

Delete files, Strfile finger file

8

MoveFile_FileStream

Void

Copy file content fis-> fos

9

MoveFile_BufferedBytestream

Void

Copy file content fis-> fos

10

Movefile_bufferedcharstream

Void

Copy file content infile-> outfile

11

READCHSTR

String

Read Chinese string from infile->

12

READCHSTR

InputStream IS

String

Reading Chinese strings from inputstream

13

TOINPUTSTREAM

String Str

FileInputStream

Convert strings to data streams

14

Writechstr

String Str

Void

Writing Chinese strings to files -> Outfile

15

Writechstr

OutputStream OS

String Str

Void

Writing Chinese strings to files -> Outfile

16

Appendchstr

String Outfile

String Str

Void

Add to the end of the document

In one

Seekstrpos

Long Cur

String Str

Long

Retrieve the position of the first STR from the position CUR

18

Seekstrpos

String Str

Long

Locate a certain string position in the file (left from left)

19

Seekstrpos

String str1

String STR2

Long

Positioning from string str1 to start the first string STR2 position

20

Substring

Long Pos

Int Len

String

Start from the POS location to the string of the LEN

twenty one

Substring

String str1

String STR2

Int Len

String

String of the length of LEN after Str2 starts from string str1

Four, original code

Package oa.main;

Import java.io. *;

Public class dealfile {/ ** input file * / string infile = "/ ** Output file * / string outfile =" "; / ** Enter file stream * / fileInputstream fis = null; / ** Output file stream * / FileOutputStream Fos = NULL; public dealfile () {}

/ ** * Establish input file stream * / public void connfis (string i) {try {infile = i; fis = new fileinputstream (infile);} catch (ooException ie) {

System.out.println ("calls deAlFile.connfis () function error: / r / n" oe);

}

/ **

* Establish an output file stream

* / Public void connfos (string o) {try {outfile = o; fos = new fileoutputstream (outfile);} catch (ooException ie) {

System.out.println ("Call DEALFILE.CONNFOS () function error: / r / n" IOE);

}

/ ** * Close input file stream * * / public void closefis () {try {if (fis! = Null)

{

fis.close ();

}}} Catch (ioexception ie) {

System.out.println ("calls deAlFile.closefis () function error: / r / n" IOE);

}

/ ** * Close Output file stream * * / public void closefos () {try {if (fos! = NULL)

{

Fos.close ();

}}} Catch (ioexception ie) {

System.out.println ("Call DEALFILE.CLOSEFOS () function error: / r / n" oe);

}

/ ** * acquired input stream * / public fileinputstream getfis () {return fis;}

/ ** * acquisition output * * / public fileoutputstream getfos () {return fos;}

/ ** * Remove file * * / public void deletefile (String DF) {file file = new file (file.delete ();

/ ** * Copy file content fis-> fos * * / public void fose_filestream () {Try {file f = new file (infile); Byte B [] = new byte [(int))] Fis.read (b); fos.write (b);} catch (ioException ie) {system.out.println ("call DEALFILE.MOVEFILE_FILESTREAM () function error: / r / n" IOE);

}

/ * ** Copy the file contents fis-> fos * * / public void movefile_BufferedByteStream () {try {BufferedInputStream in = new BufferedInputStream (fis); BufferedOutputStream out = new BufferedOutputStream (fos); int c; while ((c = in. Read ())! = -1) {Out.write ();} in.close (); out.close ();} catch (ioexception ie) {

System.out.println ("Call DEALFILE.MOVEFILE_BUFFEREDBYTESTREAM () function error: / r / n" iee);

}

/ * ** Copy the file contents infile-> outfile * * / public void movefile_BufferedCharStream () {try {BufferedReader in = new BufferedReader (new FileReader (infile)); BufferedWriter out = new BufferedWriter (new FileWriter (outfile)); int c WHILE ((c = in.read ())! = -1) {Out.write (c);} in.close (); out.close ();} catch (ooException ie) {

System.out.println ("Call DEALFILE.MOVEFILE_BUFFEREDCHARSTREAM () function error: / r / n" oe);

}

/ ** * Read Chinese string infile-> * * / public string readchstr () {return readchstr (fis);

/ * ** Read Chinese string infile-> * * / public String readCHStr (InputStream is) {String str = ""; try {// build a Unicode character stream InputStreamReader isw = new InputStreamReader (is); BufferedReader br = new BufferedReader (ISW); // Read unicode string string s = ""; while ((s = br.readline ())! = null) {if (! str.equals ("))

{

Str = STR "/ r / n" s;} else {

Str = Str S;}

}

Br.close (); isw.close ();} catch (ioexception ie) {

System.out.println ("Call DEAALFILE.ReadchStr () function error: / r / n" oe);

} RETURN STR;

}

/ ** * Convert strings to data stream * / public fileinputstream toinputStream (String str) {fileInputstream fis_t = null; try {// Write strings into temporary files, then generate data streams from file FileOutputStream Fos_t = New FileoutputStream ("tmp.txt"); WRITECHSTR (FOS_T, STR); FIS_T = New FileInputStream ("tmp.txt");} catch (ioException ie) {

System.out.println ("Call DEALFILE.TOINPUTSTREAM () function error: / r / n" oe);

} Return Fis_t;}

/ ** * written Chinese string to file -> outfile * * / public void writechstr (string str) {WritechStr (fos, str);}

/ ** * Write Chinese string to file -> outfile * * / public void writeCHStr (OutputStream os, String str) {try {// establish Unicode character stream OutputStreamWriter osw = new OutputStreamWriter (os); BufferedWriter bw = new BufferedWriter ( OSW);

// Write a Unicode string bw.write (STR, 0, str.length ()); bw.newline (); bw.close (); osw.close ();} catch (ioException ooe) {system.out. Println ("calls dealfile.writechstr () function error: / r / n" IOE);

}} / ** * Add to File End * * / Public Void Appendchstr (String Outfile, String Str) {Try {RandomaccessFile RF = New RandomaccessFile (Outfile, "RW"); rf.seek (rf.length ()); Rf.writebytes (STR); rf.close ();} catch (ioexception ie) {

System.out.println ("Call DEALFILE.APPpendchStr () function error: / r / n" oe);}}

/ ** * From the position CUR Start the position of the first STR * * / public long seekerstrpos (long cur, string str) {long fcur = cur; try {randomaccessfile file = new randomaccessfile (new file (infile), "R "); Long flen = file.Length (); int Slen = str.length (); byte b [] = new byte [Slen]; for (; fcur

// The remaining length of the file is no longer enough if (Flen-Fcur

// Determine whether the string read in the current location matches the parameter string, not to search File.Read (B, 0, Slen); string bstr = new string (b); if (str.equals (bstr)) {

Break;

}} File.close ();} catch (ooexception ie) {

System.out.println ("Call DEAALFILE.SEEKSTRPOS () function error: / r / n" oe);

} Return Fcur;} / ** * Locate a string in the file (left) * * / public long seekerstrpos (String str) {Return SeekstrPos (0, str);}

/ ** * Start from the string str1 in the file, the first string STR2 position * * / public long seekstrpos (String str1, string str2) {long cur = seekstrpos (0, str1); Return Seekstrpos (CUR , STR2);} / ** * Starting from the POS location of the file, take the length of the string * * / public string substring (long pos, int LEN) {string str = ""; try {randomaccessfile file = new randomaccessFile (New File (Infile), "R"); long flen = file.length ();

// Return to the null value if it cannot be returned (POS <0 || (Flen - POS)

{

Return "";} file.seek (POS); Byte B [] = new byte [len]; file.read (b, 0, len); str = new string (b); file.close ();} catch IOEXCEPTION IOE {

System.out.println ("calls dealfile.substring () function error: / r / n" IOE);

} Return Str;

/ ** * Start from the string str1, after STR2, the length of LEN * * / public string subString (String string substring (string string substring (string string SUBSTRING (STR1, STR2); RETURN SUBSTRING POS STR2.LENGTH (), LEN) .TRIM ();

Public static void main (string args []) throws ioException, arrayindexoutofboundsexception {dealfile df = new dealfile (); // * Additional file method (once as the output file is opened, it is emptied) //df.confis("out. TXT "); // string str = df.readchStr () " Tests to add wwwwwwwwwtttw My Guide "; //df.confos("out.txt"); //df.writechstr (STR); // DF. Closefis (); //df.closefos ();

// Copy file DF.Connfis ("out.txt"); DF.CONNFOS ("in.txt"); df.movefile_filestream (); //df.movefile_bufferedbytestream (); //df.movefile_bufferedcharstream (); DF. Closefis (); df.closefos (); // df.connfis ("out.txt"); //system.out.println (DF.SUBSTRING ("flyline", "background-color :" ,7 ).touppercase ()); //Df.closefis ();}}

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

New Post(0)