try{File fout =newFile("myOutFile.txt");FileOutputStream fos =newFileOutputStream(fout);BufferedWriter bw =newBufferedWriter(newOutputStreamWriter(fos));
bw.write("Write somthing to the file ...");
bw.newLine();
bw.close();}catch(FileNotFoundException e){// File was not found
e.printStackTrace();}catch(IOException e){// Problem when writing to the file
e.printStackTrace();}
使用 FileWrite
try{FileWriter fw =newFileWriter("myOutFile.txt");
fw.write("Example of content");
fw.close();}catch(FileNotFoundException e){// File not found
e.printStackTrace();}catch(IOException e){// Error when writing to the file
e.printStackTrace();}
使用PrintWriter
try{PrintWriter pw =newPrintWriter("myOutFile.txt");
pw.write("Example of content");
pw.close();}catch(FileNotFoundException e){// File not found
e.printStackTrace();}catch(IOException e){// Error when writing to the file
e.printStackTrace();}
使用 OutputStreamWriter
try{File fout =newFile("myOutFile.txt");FileOutputStream fos =newFileOutputStream(fout);OutputStreamWriter osw =newOutputStreamWriter(fos);
osw.write("Soe content ...");
osw.close();}catch(FileNotFoundException e){// File not found
e.printStackTrace();}catch(IOException e){// Error when writing to the file
e.printStackTrace();}