Home > Back-end >  The programming for the great god do
The programming for the great god do

Time:09-18

A programming, statistics on a hard disk to a text file (for example, d: \ test. TXT) contain letters, Numbers, and the number of other characters, and the number of information to a new text file,

CodePudding user response:

For your reference
 
Import the Java. IO. FileInputStream;
Import the Java. IO. FileWriter;
import java.io.IOException;

Public class Count {
Public static void main (String [] args) {
Count Count=new Count ();
String STR=count. ReadFile (" D://read. TXT ");//read
Count. WriteFile (" D://write. TXT ", STR);//write
}

Private void writeFile (String filePath String STR) {
FileWriter fw.
Try {
Fw=new FileWriter (filePath);
Fw. Write (STR);
Fw. Close ();
} the catch (IOException e) {
//TODO Auto - generated the catch block,
e.printStackTrace();
}
}

Public String readFile (String fileName) {
Int zm=0;
Int sz=0;
Int other=0;
Try {
@ SuppressWarnings (" resource ")
FileInputStream fis=new FileInputStream (fileName);
Byte [] buff=new byte [1024].
While (fis) available () & gt; 0 {
Int len=fis. Read (buff);
String s=new String (buff, 0, len);
for(int i=0; IChar c=s.c harAt (I);
If (Character. IsUpperCase (c) | | Character. IsLowerCase (c)) {
Zm++;
} else if (Character. IsDigit (c)) {
Sz++;
} else {
Other++;
}
}
}
} the catch (Exception e) {
e.printStackTrace();
}
Return "letters:" + zm + "" +" \ n number: "+ sz +" "+" \ n other characters: "+ other;
}
}

CodePudding user response:

Baidu search, use the file input and output flow operation
  • Related