java写入文件的方法有哪些
java写入文件的方法有哪些?下面本站小编带大家一起来看看详细内容,有需要的朋友们一起看看吧!想了解更多相关信息请持续关注我们应届毕业生考试网!
一,FileWritter写入文件
FileWritter, 字符流写入字符到文件。默认情况下,它会使用新的内容取代所有现有的内容,然而,当指定一个true (布尔)值作为FileWritter构造函数的第二个参数,它会保留现有的内容,并追加新内容在文件的末尾。
1. 替换所有现有的内容与新的内容。
new FileWriter(file);2. 保留现有的内容和附加在该文件的末尾的`新内容。
复制代码 代码如下:
new FileWriter(file,true);
追加文件示例
一个文本文件,命名为“”,并包含以下内容。
ABC Hello追加新内容 new FileWriter(file,true)
复制代码 代码如下:
package ;
import ;
import Writer;
import eredWriter;
import ception;
public class AppendToFileExample
{
public static void main( String[] args )
{
try{
String data = " This content will append to the end of the file";
File file =new File("");
//if file doesnt exists, then create it
if(!ts()){
teNewFile();
}
//true = append file
FileWriter fileWritter = new FileWriter(ame(),true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
e(data);
e();
tln("Done");
}catch(IOException e){
tStackTrace();
}
}
}
结果
现在,文本文件“”内容更新如下:
ABC Hello This content will append to the end of the file
二,BufferedWriter写入文件
缓冲字符(BufferedWriter )是一个字符流类来处理字符数据。不同于字节流(数据转换成字节),你可以直接写字符串,数组或字符数据保存到文件。
复制代码 代码如下:
package le;
import eredWriter;
import ;
import Writer;
import ception;
public class WriteToFileExample {
public static void main(String[] args) {
try {
String content = "This is the content to write into file";
File file = new File("/users/mkyong/");
// if file doesnt exists, then create it
if (!ts()) {
teNewFile();
}
FileWriter fw = new FileWriter(bsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
e(content);
e();
tln("Done");
} catch (IOException e) {
tStackTrace();
}
}
}
三,FileOutputStream写入文件
文件输出流是一种用于处理原始二进制数据的字节流类。为了将数据写入到文件中,必须将数据转换为字节,并保存到文件。请参阅下面的完整的例子。
复制代码 代码如下:
package ;
import ;
import OutputStream;
import ception;
public class WriteFileExample {
public static void main(String[] args) {
FileOutputStream fop = null;
File file;
String content = "This is the text content";
try {
file = new File("c:/");
fop = new FileOutputStream(file);
// if file doesnt exists, then create it
if (!ts()) {
teNewFile();
}
// get the content in bytes
byte[] contentInBytes = ytes();
e(contentInBytes);
h();
e();
tln("Done");
} catch (IOException e) {
tStackTrace();
} finally {
try {
if (fop != null) {
e();
}
} catch (IOException e) {
tStackTrace();
}
}
}
}
//更新的JDK7例如,使用新的“尝试资源关闭”的方法来轻松处理文件。
package ;
import ;
import OutputStream;
import ception;
public class WriteFileExample {
public static void main(String[] args) {
File file = new File("c:/");
String content = "This is the text content";
try (FileOutputStream fop = new FileOutputStream(file)) {
// if file doesn't exists, then create it
if (!ts()) {
teNewFile();
}
// get the content in bytes
byte[] contentInBytes = ytes();
e(contentInBytes);
h();
e();
tln("Done");
} catch (IOException e) {
tStackTrace();
}
}
}
-
编写更好的Java单元测试的7个技巧
测试是开发的一个非常重要的方面,可以在很大程度上决定一个应用程序的命运。良好的测试可以在早期捕获导致应用程序崩溃的问题,但较差的测试往往总是导致故障和停机。虽然有三种主要类型的软件测试:单元测试,功能测试和集成测试,但是在这篇博文中,我们将讨论开发人员...
-
Java语法基础for语句练习
本文是本站小编搜索整理的关于Java语法基础for语句练习,供参考学习,希望对大家有所帮助!想了解更多相关信息请持续关注我们应届毕业生考试网!控制语句——for练习语句的嵌套应用累加求和,计数器循环嵌套一、语句的嵌套应用语句嵌套形式。其实就是语句中...
-
Java中的对象与引用知识详解
在Java中,有一组名词经常一起出现,它们就是“对象和对象引用”,很多朋友在初学Java的时候可能经常会混淆这2个概念,觉得它们是一回事,事实上则不然。今天我们就来一起了解一下对象和对象引用之间的区别和联系。以下仅供参考!1.何谓对象?在Java中有一句比较流行的话,叫...
-
Java 正则表达式
Java是一门编程语言,那么大家知道Java正则表达式是怎样的呢?下面一起来看看!Java正则表达式正则表达式定义了字符串的模式。正则表达式可以用来搜索、编辑或处理文本。正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别。正则表达式实例一个字符串其...