文档章节

IODemo

韩毅Evan
 韩毅Evan
发布于 2017/05/01 23:29
字数 398
阅读 4
收藏 0

//写入文件,读取文件

import java.io.*;

public class Fileinputstream {
	public static void main(String[] args) {
		File a = new File("E:\\mycode\\day11IO\\Test.txt");
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(a);
			byte[] arr ="中国".getBytes();
			out.write(arr);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}finally {
			try {
				if (out!=null) {
					out.close();
				}
				
			} catch (IOException e) {
				
				e.printStackTrace();
			}
		}
	System.out.println("-------------------------------------");
			FileInputStream in = null;
		try {
			 in = new FileInputStream(a);
			byte[] byt =new byte[1024]; 
			int temp = in.read(byt);
			System.out.println(new String(byt,0,temp));
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
		
			e.printStackTrace();
		}finally {
			try {
				if (in!=null) {
					in.close();
				}
				
			} catch (IOException e) {
				
				e.printStackTrace();
			}
		}
		
		
	
	}
}

//FileWriter练习

import java.io.*;

public class Filewriter {

	public static void main(String[] args) {
		FileWriter out = null;

		try {
			out = new FileWriter("D:"+File.separator+"ab.txt");
			out.write("dasdqwfrqfqwfqfas");
			out.flush();
			
		} catch (IOException e) {
		
			e.printStackTrace();
		}finally {
			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		

	}

}

//FileReader练习

import java.io.*;

public class Fliereader {

	public static void main(String[] args) {
		FileReader in = null;
		try {
			in = new FileReader("D:"+File.separator+"ab.txt");
			char[] arr = new char[3];
			int a = 0;
			while ((a = in.read(arr))!=-1) {
			
				System.out.print(new String(arr,0,a));
			}
			
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		

	}

}

//用FileInputStream与FileOutputStream进行txt拷贝

* 不同盘拷贝txt文件内容
 */

import java.io.*;

public class CopyDemo {

	public static void main(String[] args) {
		FileInputStream in = null;
		FileOutputStream out = null;
		try {
			in = new FileInputStream("");
			out = new FileOutputStream("");
			byte[] byt = new byte[1024];
			int len = 0;
			while((len = in.read(byt))!=-1){
				out.write(byt);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}

//Reader和writer拷贝

/**
 * 
 * Reader和writer拷贝
 */

import java.io.*;

public class WriterCopy {

	public static void main(String[] args) {
		FileReader in = null;
		FileWriter out = null;
		try {
			in = new FileReader("E:\\eclipsejava\\dayuanruanjian\\src\\day14IO\\Fliereader.java");
			out = new FileWriter("D:\\ab.txt");
			char [] temp = new char[1024];
			int len = 0;
			while ((len = in.read(temp))!=-1) {
				out.write(temp);
				out.flush();
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(in!=null)
				in.close();
				if(out!=null)
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		

	}

}

 

© 著作权归作者所有

韩毅Evan
粉丝 2
博文 49
码字总数 67347
作品 0
南京
私信 提问

暂无文章

Redis缓存穿透、缓存雪崩和缓存击穿

Redis缓存穿透、缓存雪崩 缓存雪崩,是指在某一个时间段,缓存集中过期失效。 产生雪崩的原因之一,比如在写本文的时候,马上就要到双十二零点,很快就会迎来一波抢购,这波商品时间比较集中...

architect刘源源
15分钟前
1
1
ArrayList源码分析

一、核心变量 // 序列化ID private static final long serialVersionUID = 8683452581122892189L; // 默认初始化容量 private static final int DEFAULT_CAPACITY = 10; ......

星爵22
24分钟前
1
0
++a a++的再次理解

public class Test { //// public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub int a=1; int b=2; int c; int d; c=......

南桥北木
24分钟前
0
0
整合Spring和SpringMVC

1.Spring容器和SpringMVC容器的关系 Spring容器是一个父容器,SpringMVC容器是一个子容器,它继承自Spring容器。因此,在SpringMVC容器中,可以访问到Spring容器中定义的Bean,而在Spring容器...

薛小二
25分钟前
0
0
递归实现后序遍历二叉树

问题描述 从键盘接受输入先序序列,以二叉链表作为存储结构,建立二叉树(以先序来建立)并对其进行后序遍历,然后将遍历结果打印输出。要求采用递归方法实现。 解题思路 递归实现 程序实现 ...

niithub
今天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部