学习笔记 - FileOutputStream流将字符串写入文件中
来源:互联网 发布:自媒体怎么赚钱知乎 编辑:程序博客网 时间:2024/05/20 18:54
public class FileOutputStreamTest {<span style="white-space:pre"></span>public static void main(String[] args) {<span style="white-space:pre"></span>// TODO Auto-generated method stub<span style="white-space:pre"></span>new FileOutputStreamTest().writerByByte("abcd", "d:\\JAVA之路.txt");<span style="white-space:pre"></span>}<span style="white-space:pre"></span><span style="white-space:pre"></span>//将字符串与入输出流中<span style="white-space:pre"></span>public void writerByByte(String source,String filePath){<span style="white-space:pre"></span><span style="white-space:pre"></span>File file = new File(filePath);<span style="white-space:pre"></span>FileOutputStream fos = null;<span style="white-space:pre"></span>byte[] buf = source.getBytes();<span style="white-space:pre"></span>try {<span style="white-space:pre"></span>fos = new FileOutputStream(file,true);//true是默认是添加到文件尾部<span style="white-space:pre"></span><span style="white-space:pre"></span>//一次写入一个字节<span style="white-space:pre"></span>for (int i = 0; i < buf.length; i++) {<span style="white-space:pre"></span>fos.write(buf[i]);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>//一次写入字节数组<span style="white-space:pre"></span>//fos.write(buf);<span style="white-space:pre"></span><span style="white-space:pre"></span><span style="white-space:pre"></span>} catch (FileNotFoundException e) {<span style="white-space:pre"></span>// TODO Auto-generated catch block<span style="white-space:pre"></span>e.printStackTrace();<span style="white-space:pre"></span>} catch (IOException e) {<span style="white-space:pre"></span>// TODO Auto-generated catch block<span style="white-space:pre"></span>e.printStackTrace();<span style="white-space:pre"></span>} finally{<span style="white-space:pre"></span>try {<span style="white-space:pre"></span>fos.close();<span style="white-space:pre"></span>} catch (IOException e) {<span style="white-space:pre"></span>// TODO Auto-generated catch block<span style="white-space:pre"></span>e.printStackTrace();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}}
0 0
- 学习笔记 - FileOutputStream流将字符串写入文件中
- Java学习笔记1:将字符串写入txt文件中
- 学习笔记 - FileWriter将字符串写入文件
- 学习笔记 - BufferedWriter将字符串通过字符缓冲流写入文件
- 将字符串写入到txt文件中
- java将字符串写入到文件中
- 将字符串写入文件
- 将字符串写入文件
- 将字符串写入文件
- java FileOutputStream写入文件
- Java学习笔记(二十三):将InputStream转成字符串和写入文件
- 【JAVA笔记】将字符串信息写入xml文件的方法
- 学习笔记 - FileInputStream FileOutputStream文件拷贝
- Java使用FileOutputStream写入文件
- java将字符串写入到txt文件中
- VC将字符串写入到txt文件中
- 将字符串json写入到服务器文件中
- 将字符串写入txt中
- 第六课for循环(2)---使用循环变量
- 互换二维数组的行列。要求数组的行数、列数以及各元素均从键盘输入;输出互换前数组内容和互换后数组内容。(要求循环输入进行测试)Java
- 【Android】Android之Activity的生命周期
- 直流电机故障问题
- php 如何设置一个严格控制过期时间的session
- 学习笔记 - FileOutputStream流将字符串写入文件中
- JVM-ClassLoader
- 计算机网络系列学习:Internet简介
- 我的hosts文件
- Android 中 View 炸裂特效的实现分析
- getGenericSuperclass getSuperclass 区别
- 安卓音响OEM/ODM方案商-创联时代
- 如何使用Github创建respository(项目)
- 协议设计注意事项