Home > Back-end >  Java for help
Java for help

Time:01-16

A: what is this line of code
Public void replacement (String filePath, String oldstr, String newStr) {
The File f=new File (database. Path. Resident File path);
Long fileLength=f.l ength ();
Byte [] fileContext=new byte [fileLength intValue ()];

FileInputStream in=null;
PrintWriter out=null;

Try {
In=new FileInputStream (filePath);
In the read (fileContext);
String STR=new String (fileContext, "GBK");
STR=STR. Replace (oldstr, newStr);
Out=new PrintWriter (filePath);
Out. Write (STR);
} the catch (IOException e) {
e.printStackTrace();
} the finally {
Try {
out.flush();
out.close();
in.close();
} the catch (IOException e) {
e.printStackTrace();
}
}
}

CodePudding user response:

 import Java. IO. The File; 
import java.io.FileInputStream;
import java.io.IOException;
Import the Java. IO. PrintWriter;

/* *
* @ author: jiaolian
* @ date: Created in these 2021-01-15
* @ description: file reading and writing test
* @ modified By:
* public: call practice
*/
Public class FileTest {

Public static void main (String [] args) {
Replece (" E: \ \ a1. TXT ", ""," practice called ");
}

//replace strings that will be file
Public static void replece (String filePath, String oldstr, String newStr) {
The File f=new File (filePath);
Long fileLength=f.l ength ();
Byte [] fileContext=new byte [fileLength intValue ()];

FileInputStream in=null;
PrintWriter out=null;

Try {
//the file read into a byte array
In=new FileInputStream (filePath);
In the read (fileContext);
//an array of bytes into a string
String STR=new String (fileContext, "utf-8");
STR=STR. Replace (oldstr, newStr);
//replace string to write to the file
Out=new PrintWriter (filePath);
Out. Write (STR);
} the catch (IOException e) {
e.printStackTrace();
} the finally {
Try {
out.flush();
out.close();
in.close();
} the catch (IOException e) {
e.printStackTrace();
}
}
}
}

Intention is to do the string file method is used to replace, there are several errors in the code above, has a simple annotation, hope to help you

CodePudding user response:

Code or write not enough strong, such as when the file is too large, replacement is particularly slow,
  • Related