Package thread;
Import java.io.*;
public class HH {
/**
* Create a file
* @param fileName
* @return
*/
public static Boolean createFile (File fileName) throws exception{
try{
if (!filename.exists ()) {
Filename.createnewfile ();
}
}catch (Exception e) {
E.printstacktrace ();
}
return true;
}
/**
* Read TXT content
* @param file
* @return
*/
public static String readtxtfile (file file) {
String result = "";
try {
InputStreamReader reader = new InputStreamReader (new FileInputStream (file), "GBK");
BufferedReader br = new BufferedReader (reader);
String s = null;
while ((S=br.readline ())!=null) {
Result = result + S;
System.out.println (s);
}
} catch (Exception e) {
E.printstacktrace ();
}
return result;
}
/**
* Write txt, overwrite the original content
* @param content
* @param fileName
* @return
* @throws Exception
*/
public static Boolean writetxtfile (String content,file fileName) throws exception{
Randomaccessfile Mm=null;
Boolean flag=false;
FileOutputStream Fileoutputstream=null;
try {
FileOutputStream = new FileOutputStream (fileName);
Fileoutputstream.write (Content.getbytes ("GBK"));
Fileoutputstream.close ();
Flag=true;
} catch (Exception e) {
E.printstacktrace ();
}
return flag;
}
/**
* Write txt, append write
* @param FilePath
* @param content
*/
public static void Filechasefw (string filePath, string content) {
try {
The second parameter in the constructor true indicates that the file is written in append form
FileWriter FW = new FileWriter (filepath,true);
Fw.write (content);
Fw.close ();
} catch (IOException e) {
System.out.println ("File write Failed! "+ e);
}
}
public static void Main (string[] args) throws exception{
File File = new file ("D:\\123wu ng. txt");
CreateFile (file);
Readtxtfile (file);
Writetxtfile ("I am writing a content one", file);
FILECHASEFW ("D:\\123wu Wu. txt", "66666666");
}
}
Java read txt file and overwrite write TXT file and append write txt