Project Structure:
Running effect:
========================================================== ======================
The following is the code:
========================================================== ======================
/Text/src/com/b510/txt/MyFile. java
Copy codeThe Code is as follows: package com.b510.txt;
Import java. io. BufferedReader;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
Import java. io. FileReader;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java. io. PrintWriter;
/**
* @ Author Hongten
*
* @ Time 2011-12-12 2011
*/
Public class MyFile {
@ SuppressWarnings ("static-access ")
Public static void main (String [] args ){
MyFile myFile = new MyFile ();
Try {
For (int I = 0; I <5; I ++ ){
MyFile. creatTxtFile ("test ");
MyFile. writeTxtFile ("displays append information" + I );
String str = myFile. readDate ();
System. out. println ("********** \ n" + str );
}
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
Private static String path = "txt /";
Private static String filenameTemp;
/**
* Create a file
*
* @ Throws IOException
*/
Public static boolean creatTxtFile (String name) throws IOException {
Boolean flag = false;
FilenameTemp = path + name + ". txt ";
File filename = new File (filenameTemp );
If (! Filename. exists ()){
Filename. createNewFile ();
Flag = true;
}
Return flag;
}
/**
* Write a file
*
* @ Param newStr
* New Content
* @ Throws IOException
*/
Public static boolean writeTxtFile (String newStr) throws IOException {
// Read the content of the original file before writing
Boolean flag = false;
String filein = newStr + "\ r \ n ";
String temp = "";
FileInputStream FCM = null;
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
PrintWriter pw = null;
Try {
// File path
File file = new File (filenameTemp );
// Read the file into the input stream
FS = new FileInputStream (file );
Isr = new InputStreamReader (FCM );
Br = new BufferedReader (isr );
StringBuffer buf = new StringBuffer ();
// Save the original content of the file
For (int j = 1; (temp = br. readLine ())! = Null; j ++ ){
Buf = buf. append (temp );
// System. getProperty ("line. separator ")
// The delimiter between the row and the row is equivalent to "\ n"
Buf = buf. append (System. getProperty ("line. separator "));
}
Buf. append (filein );
Fos = new FileOutputStream (file );
Pw = new PrintWriter (fos );
Pw. write (buf. toString (). toCharArray ());
Pw. flush ();
Flag = true;
} Catch (IOException e1 ){
// TODO automatically generates catch Blocks
Throw e1;
} Finally {
If (pw! = Null ){
Pw. close ();
}
If (fos! = Null ){
Fos. close ();
}
If (br! = Null ){
Br. close ();
}
If (isr! = Null ){
Isr. close ();
}
If (FS! = Null ){
FCM. close ();
}
}
Return flag;
}
/**
* Read data
*/
Public void readData1 (){
Try {
FileReader read = new FileReader (filenameTemp );
BufferedReader br = new BufferedReader (read );
String row;
While (row = br. readLine ())! = Null ){
System. out. println (row );
}
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
Public String readDate (){
// Define an empty string to be returned
String strs = "";
Try {
FileReader read = new FileReader (new File (filenameTemp ));
StringBuffer sb = new StringBuffer ();
Char ch [] = new char [1024];
Int d = read. read (ch );
While (d! =-1 ){
String str = new String (ch, 0, d );
Sb. append (str );
D = read. read (ch );
}
System. out. print (sb. toString ());
String a = sb. toString (). replaceAll ("@@@@@",",");
Strs = a. substring (0, a. length ()-1 );
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
Return strs;
}
}