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基础语法面试题
引导语:Java的主要工作是通过编程语言来制作互联网页面、制作动态效果以及网站等技术,以下是小编整理的Java基础语法面试题,欢迎参考阅读!shorts1=1;s1=s1+1;有什么错?对于shorts1=1;s1=s1+1;由于s1+1运算时会自动提升表达式的类型,所以结果是int型,再赋值给short类...
-
Java开发岗位面试题
为帮助参加Java开发岗位面试的朋友们做好准备,以下是本站小编搜索整理的一份Java集合面试问题40个【附答案】,供参考练习,希望对大家有所帮助!想了解更多相关信息请持续关注我们应届毕业生考试网!一、Java基础1、String类为什么是final的。2、HashMap的源码,实现原...
-
java语言的接口与继承
Java是一个面向对象的语言。对程序员来说,这意味着要注意应中的数据和操纵数据的方法,而不是严格地用过程来思考。下面是小编分享的java语言的接口与继承,一起来看一下吧。大多数人认为,接口的意义在于顶替多重继承。众所周知Java没有c++那样多重继承的机制,但是却...
-
java system类使用方法示例
常用的方法:复制代码代码如下:longcurrentTimeMillis();获取当前时间的毫秒值voidexit();终止当前正在运行的Java虚拟机。复制代码代码如下:publicstaticvoidMethod(){longl=entTimeMillis();tln(l);();}描述系统属性信息:Propertiesroperties();该方法获取的`信...