zoukankan      html  css  js  c++  java
  • 【自动化__持续集成】___java___文本文件__字节流

    一、代码如下

    package www.wujianbotwo;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    
    public class Demo09 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Demo09 demo09= new Demo09();
    		demo09.byteRead();
    
    	}
    	
    	private void byteRead() {
    		try {
    			File file= new File("D:\Test.txt");
    			InputStream in= new FileInputStream(file);
    			byte[] buf= new byte[(int)file.length()];
    			in.read(buf);
    			in.close();
    			for(byte b: buf) {
    				System.out.println(b);
    			}
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    
    	}
    
    }
    
    package www.wujianbotwo;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    public class Demo09 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Demo09 demo09= new Demo09();
    		//demo09.byteRead();
    		demo09.byteWrite();
    		demo09.byteWriteFalse();
    
    	}
    	
    	private void byteRead() {
    		try {
    			File file= new File("D:\Test.txt");
    			InputStream in= new FileInputStream(file);
    			byte[] buf= new byte[(int)file.length()];
    			in.read(buf);
    			in.close();
    			for(byte b: buf) {
    				System.out.println(b);
    			}
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    
    	}
    	
    	private void byteWrite() {
    		try {
    			File file= new File("D:\Test.txt");
    			OutputStream out= new FileOutputStream(file, true);
    			String content= "这是文件内容。
    ";
    			byte[] buf= content.getBytes();
    			out.write(buf, 0, buf.length);
    			out.close();
    			System.out.println("输入成功!!");
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    
    	}
    	
    	
    	private void byteWriteFalse() {
    		try {
    			File file= new File("D:\Test.txt");
    			OutputStream out= new FileOutputStream(file, false);
    			String content= "这是文件内容。
    ";
    			byte[] buf= content.getBytes();
    			out.write(buf, 0, buf.length);
    			out.close();
    			System.out.println("输入成功!!");
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    
    	}
    
    
    }
    
  • 相关阅读:
    使用yum命令报错
    CentOS6.8安装Python3.6.3
    解决 linux 下安装 node 报: command not found
    Python的pip源切换为国内阿里云镜像
    开源Java加密工具Jasypt 1.4发布
    JavaFX对Java开发者到底意味着什么?
    16 个印象深刻的 HTML5/CSS3/JavaScript 体验
    高效的Java异常处理
    Java开发的学习过历程
    Java中23种设计模式详解
  • 原文地址:https://www.cnblogs.com/wujianbo123/p/7492109.html
Copyright © 2011-2022 走看看