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();
    		}
    
    	}
    
    
    }
    
  • 相关阅读:
    Luogu1309 瑞士轮(分治,归并排序)
    HYSBZ(BZOJ) 4300 绝世好题(位运算,递推)
    Luogu 1220 关路灯(动态规划)
    HDU 2087 剪花布条(字符串匹配,KMP)
    HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP)
    HDU 1711 Number Sequence (字符串匹配,KMP算法)
    Luogu 3375 【模板】KMP字符串匹配(KMP算法)
    KMP算法(研究总结,字符串)
    CJOJ 1331 【HNOI2011】数学作业 / Luogu 3216 【HNOI2011】数学作业 / HYSBZ 2326 数学作业(递推,矩阵)
    Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂)
  • 原文地址:https://www.cnblogs.com/wujianbo123/p/7492109.html
Copyright © 2011-2022 走看看