[英]How to read, write content to text file using FileWriter and BufferedWriter in Java
I have a basic requirement of writing some text into a text file and read that text and print in Console. 我有一个基本要求,即将一些文本写入文本文件,然后读取该文本并在控制台中打印。
What is the better approach? 有什么更好的方法? Is anything better(in terms of performance/usage) than BufferedReader or BufferedWriter for writing and reading simple text?
在写和读简单文本方面,有什么比BufferedReader或BufferedWriter更好的(在性能/用途上)?
Can someone also suggest better way of writing the below code aswell: 有人还能建议更好的方式编写以下代码:
public class FileReadWrite
{
public static void main(String[] args) throws IOException
{
File objFile=new File("SampFile.txt");
if(!objFile.exists())
objFile.createNewFile();
FileReader objFR=new FileReader(objFile);
BufferedReader objBR=new BufferedReader(objFR);
FileWriter objFW=new FileWriter(objFile);
BufferedWriter objBW=new BufferedWriter(objFW);
objBW.write("Hello World!!!");
objBW.write("How Are you");
objBW.close();
String strContent;
while((strContent=objBR.readLine())!=null)
System.out.println(objBR.readLine());
objBR.close();
}
}
try printing the string you are reading in your while loop: 尝试在while循环中打印正在读取的字符串:
while((strContent=objBR.readLine())!=null)
System.out.println(strContent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.