首页 >> 药膳食疗

使用实现在文件中添加字符串牛

药膳食疗  2021年09月06日  浏览:5 次

我在一个项目中需要使用C:\\WINDOWS\\system32\\drivers\\etc这个目录下的hosts文件,并且在该文件的最后加上一个这样的字符串:\"202.206.219.246 rsgl_dbserve\",由于对Java的文件操作不是很熟练,花了半天的功夫才找到了,具体的实现办法如下:

import leOutputStream;

import Exception;

import tputStreamWriter;

public class FileWriterTest {

public static void main(String[] args) {

FileOutputStream stream ;

OutputStreamWriter writer;

try {

//主要是使用了FileOutputStream的构造函数FileOutputStream(Filefile, booleanappend)

//这里参数append为true表示可以添加,详细使用参考JDK帮助文档资料.

stream = new FileOutputStream(\"C:\\\\WINDOWS\\\\system32\\\\drivers\\\\etc\\\\hosts\", true);

writer = new OutputStreamWriter(stream);

ite(\"202.206.219.246 rsgl_dbserve\");

ose();

ose();

} catch (IOException e) {

intStackTrace();

}

}

}

工作人员怀疑两个人是将其他彩票上的数字剪贴到这张彩票上 以上代码在eclipse上调试成功!

为了增加代码的重用性,可以编写一个方法如下:

public void appendToFile(String str, String filename) throws Exception

{

// Open up an outputstreamwriter to the file, and append str to it.

FileOutputStream stream;//provides file access

OutputStreamWriter writer;//writes to the file

try

{

stream = new FileOutputStream(filename, true);

writer = new OutputStreamWriter(stream);

ite(str);

ose();

ose();

}

catch(Exception e)

{

throw e;

}

}//appendToFile

西宁医院哪家男科医院好
冠心病注意什么饮食
南宁白癜风医院哪家好
友情链接