Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.PrintWriter;
public class Txtexport {private static String path = ' d:/';
private static String filenametemp;
public static void Main (string[] args) throws IOException {txtexport.creattxtfile ("Hello");
Txtexport.writetxtfile ("Hello"); /** * Create 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 File * * @param newstr * NEW content * @throws IOException/public static Boolean Writetxtfile
(String newstr) throws IOException {//Read the original file contents first, then write to Boolean flag = false;
String Filein = newstr + "\ r \ n"; String temp = "";
FileInputStream FIS = null;
InputStreamReader ISR = null;
BufferedReader br = null;
FileOutputStream fos = null;
PrintWriter pw = null;
try {//filename path File File = new file (filenametemp);
Reads the file into the input stream FIS = new FileInputStream (file);
ISR = new InputStreamReader (FIS);
br = new BufferedReader (ISR);
StringBuffer buf = new StringBuffer (); Save the original contents of the file for (int j = 1; (temp = Br.readline ())!= null;
J + +) {buf = Buf.append (temp); System.getproperty ("Line.separator")//the separator between line and line equals "\ 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;
The 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 (FIS!= null) {fis.close ();
} return flag;
}
}