这里有新鲜出炉的 Java 函数式编程,程序狗速度看过来!
java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的 Java 程序设计语言和 Java 平台(即 JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。
这篇文章主要介绍了 Java 字符串写入文件三种方式的实现的相关资料, 需要的朋友可以参考下
Java 字符串写入文件三种方式的实现
1、使用 FileWriter
- String str = "hello world!";
- FileWriter writer;
- try {
- writer = new FileWriter("E:/token.txt");
- writer.write(str);
- writer.flush();
- writer.close();
- } catch(IOException e) {
- e.printStackTrace();
- }
2、使用 FileOutPutStream
- File txt = new File("E:/log1.txt");
- if (!txt.exists()) {
- txt.createNewFile();
- }
- byte bytes[] = new byte[512];
- bytes = str.getBytes();
- int b = bytes.length; //是字节的长度,不是字符串的长度
- FileOutputStream fos = new FileOutputStream(txt);
- fos.write(bytes, 0, b);
- fos.write(bytes);
- fos.close();
3、使用 FileOutPutStream 追加写入文件
- FileOutputStream fos = new FileOutputStream("E:/log.txt", true);
- //true表示在文件末尾追加
- fos.write(log.getBytes());
- fos.close();
来源: http://www.phperz.com/article/17/0820/338335.html