Write to file using a BufferedWriter
2012-03-12 11:35
316 查看
Write to file using a BufferedWriter - A Java Code Example
Write to file using a BufferedWriter
When you want to output text to a file it's better to use a Writer class instead of an OutputStream such as the BufferedOutputStream since the purpose of Writer classes are to handle textual content.
With the BufferedWriter (as opposed to the BufferedOutputStream) you don't have to translate your String parameter to a byte array, and there is also a handy method for writing a new line character.
In this example we simply write two lines of text, and finally we call flush on the BufferedWriter object before closing it.
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
/**
*
* @author javadb.com
*/
public class Main {
/**
* Prints some data to a file using a BufferedWriter
*/
public void writeToFile(String filename) {
BufferedWriter bufferedWriter = null;
try {
//Construct the BufferedWriter object
bufferedWriter = new BufferedWriter(new FileWriter(filename));
//Start writing to the output stream
bufferedWriter.write("Writing line one to file");
bufferedWriter.newLine();
bufferedWriter.write("Writing line two to file");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
//Close the BufferedWriter
try {
if (bufferedWriter != null) {
bufferedWriter.flush();
bufferedWriter.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main().writeToFile("myFile.txt");
}
}
Write to file using a BufferedWriter
When you want to output text to a file it's better to use a Writer class instead of an OutputStream such as the BufferedOutputStream since the purpose of Writer classes are to handle textual content.
With the BufferedWriter (as opposed to the BufferedOutputStream) you don't have to translate your String parameter to a byte array, and there is also a handy method for writing a new line character.
In this example we simply write two lines of text, and finally we call flush on the BufferedWriter object before closing it.
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
/**
*
* @author javadb.com
*/
public class Main {
/**
* Prints some data to a file using a BufferedWriter
*/
public void writeToFile(String filename) {
BufferedWriter bufferedWriter = null;
try {
//Construct the BufferedWriter object
bufferedWriter = new BufferedWriter(new FileWriter(filename));
//Start writing to the output stream
bufferedWriter.write("Writing line one to file");
bufferedWriter.newLine();
bufferedWriter.write("Writing line two to file");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
//Close the BufferedWriter
try {
if (bufferedWriter != null) {
bufferedWriter.flush();
bufferedWriter.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main().writeToFile("myFile.txt");
}
}
相关文章推荐
- Write a method to print the last K lines of an input file using C++
- Csharp:The .dat File using BinaryReader and BinaryWriter Convert to DataTable
- Csharp:The .dat File using BinaryReader and BinaryWriter Convert to DataTable
- Using Windows Live Writer to write first offline blog
- prjGetWriteINIFile - How to Read-Write INI file using VB & API - 读取/写入INI文件 - VB6 + API - SourceCode - HackerJLY
- Read and Write compressed data to a binary file using ASP.NET
- Using windows live writer to write my blog
- Why is using BufferedInputStream to read a file byte by byte faster than using FileInputStream
- linux - How do I write stderr to a file while using "tee" with a pipe?
- File Uploading to Server Hard Disk using ASP.NET
- [Training Video - 6] [File Reading] [Java] Create and Write Excel File Using Apache POI API
- Can't create/write to file 'C:\WINDOWS\TEMP\...MYSQL报错解决方法
- Error code: 0x80004005 "The Microsoft Access database engine cannot open or write to the file ''
- (By Marco Nanni)Using XML to Improve File-Upload Processing
- ios writeToFile 和 对象序列化
- Visual Studio error BC31019: Unable to write to output file
- WriteToCSVFile
- How do I write to files using cheerio and request in node?
- IAR Build error: Multiple tools write to the same file
- Steps to write a simple batch bat file