java基础-输出流-将字符串数组输出至文本文件

参考:

http://www.cnblogs.com/plant/p/4637123.html

https://zhidao.baidu.com/question/32741401.html

 

内容:

import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
 * @author czchina
 *
 */
public class TestStream {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //声明输入流的引用
        FileInputStream fls = null;
        //声明输出流的引用
        FileOutputStream fos =null;
        try{
            //一、生成代表输入流的对象
            fls = new FileInputStream("E:/Android/AndroidStudioProjects/text.txt");
            //生成一个字节数组
            byte [] buffer= new byte [100];
            //调用输入流对象的read方法,读取数据(将读出来的长度为<buffer.length-5>的数据放入buffer数组中,5是开始存放的位置)
            //注意读出来的数据长度不要超过数组长度。
            fls.read(buffer,5,buffer.length-5);
            
            //二、生成代表输出流的对象
            fos = new FileOutputStream("E:/Android/AndroidStudioProjects/store.txt");
            fos.write(buffer, 0,buffer.length);
            
            
            //String s = new String(buffer);
            //System.out.println(s);
            //调用一个String对象的trim方法,会去掉字符串的首尾空格,测试如下
            //s = s.trim();
            //System.out.println(s);
            
        }
        catch(Exception e){
            System.out.println(e.toString());
        }
    }
}

  内容增加:(String to FileOutputStream)

import java.io.*;
class Test{ 
 public static void main(String[] args) throws Exception{ 
String str="你的内容"; 
 FileOutputStream fos = new FileOutputStream(new File"文件名")); 
 fos.write(str.getBytes()); 
 fos.close();
 }
 } 

  

posted @ 2017-06-17 19:19  tangyongathuse  阅读(806)  评论(0编辑  收藏  举报