码迷,mamicode.com
首页 > 编程语言 > 详细

【自动化__持续集成】___java___文本文件__字节流

时间:2017-09-07 23:12:17      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:集成   oid   div   tput   method   logs   catch   output   print   

一、代码如下

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= "这是文件内容。\r\n";
			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= "这是文件内容。\r\n";
			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();
		}

	}


}

 

【自动化__持续集成】___java___文本文件__字节流

标签:集成   oid   div   tput   method   logs   catch   output   print   

原文地址:http://www.cnblogs.com/wujianbo123/p/7492109.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!