• File类基本操作之OutputStream字节输出流


    贴代码了,已经測试,可正常编译

    package org.mark.streamRW;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.OutputStream;
    
    /**
     * 字节流输出OutputStream
     * 注意:1,write(byte[] b)数组方法写入;
     * 
     * @author Mark
     */
    public class OutputStream1 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) throws Exception{
    		// TODO Auto-generated method stub
    		//第1步:使用File类找到一个文件
    		File file = new File("d:" +File.separator +"test.txt");
    		//第2步:通过子类实例化父类对象
    		OutputStream output = null; 
    		output = new FileOutputStream(file);//通过对象多态性,进行实例化
    		//第3步:进行写操作
    		String str = "Hello World!!!";
    		byte[] b = str.getBytes();	//转成byte数组
    		output.write(b);			//查看api  write(byte[] b)
    		//第4步:关闭输出流
    		output.close();	//别忘记关闭
    		
    		//假设文件不存在,则会为用户自己主动创建文件;
    		
    	}
    
    }
    

    使用write(int b)将指定的字节写入此输出流。 

    package org.mark.streamRW;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.OutputStream;
    
    //使用write(int b)将指定的字节写入此输出流。 
    public class OutputStream2 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) throws Exception{
    		// TODO Auto-generated method stub
    		//第1步:使用File类找到一个文件
    		File file = new File("d:" +File.separator +"test.txt");
    		//第2步:通过子类实例化父类对象
    		OutputStream output = null; 
    		output = new FileOutputStream(file);//通过对象多态性,进行实例化
    		//第3步:进行写操作
    		String str = "Hello World!!!";
    		byte[] b = str.getBytes();	//转成byte数组
    //		output.write(b);			//查看api  write(byte[] b)
    		for (int i = 0; i < b.length; i++) {
    			output.write(b[i]); //write(int b)将指定的字节写入此输出流。 
    		}
    		
    		//第4步:关闭输出流
    		output.close();	//别忘记关闭
    		
    		//假设文件不存在,则会为用户自己主动创建文件;
    		//之前文件内容已经不存在了
    	}
    
    }
    

    //追加文件,换行,

    	String str = "
    Hello World!!!";// 
    换行

    有什么问题,给我留言

  • 相关阅读:
    [转载] set IDENTITY_INSERT on 和 off 的设置
    固定GridView的表头和某几列
    图形文件格式小常识
    MVP——ModelViewerPresenter [ZT]
    VB.NET 中图形旋转任意角度 [ZT]
    工厂方法模式和抽象工厂模式之比较 [Z]
    Visual C# 语言概念数据类型(C# 与 Java)
    LINQ 中使用 Distinct 的 Compare过滤重复的字段
    How to load AJAX content into current Colorbox window?
    解决FTP服务器FileZilla server中文乱码问题
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4042889.html
Copyright © 2020-2023  润新知