21天精通java基础之Day15IO流(一)

Day15:IO流(一)


java.io.File类:

1.凡是与输入输出有关的类、接口等都定义在java。io包下。

2.File是一个类,可以表示构造器创建其对象。此对象表示一个文件或一个文件目录。

3.File类对象是与平台无关的。

4.File中的方法,仅涉及到如何创建、删除、重命名等等。

5.路径:①绝对路径—包括盘符在内的完整的文件路径,②相对路径—在当前文件目录下文件的路径。


IO原理:

输入input:

|———读取外部数据到程序(内存)中。

输出output:

|——将程序(内存)数据输出到存储设备中。


   IO的体系:

按照数据流向的不同:输入流,输出流。

按照处理数据的单位的不同:字节流,字符流(处理文本文件)。

按照角色的不同:节点流(直接作用于文本),处理流。

抽象基类:                  节点流(文件流):                           缓冲流:

|---InputStream                 |---FileInputStream                           |---BufferedInputStream

|---OutputStream              |---FileOutputStream                         |---BufferedOutputStream

|---Reader                         |---FileReader                                   |---BufferedReader

|---Writer                           |---FileWriter                                      |---BufferedWriter


FileInputStream实例:

@Test
	public void TestFileInputStream2() {
		
		// 2.创建一个FileInputStream类的对象
		FileInputStream fis = null;
		try {
			// 1.创建一个File类的对象
			File file = new File("hello.txt");			
			fis = new FileInputStream(file);
			
			// 3.调用FileInputStream的方法实现file文件的读取。
			// read():读取文件的一个字节。当执行到文件结尾时,返回-1.	
			int b;
			while((b = fis.read() )!= -1){
				System.out.print((char)b);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			// 4.关闭相应的流
			try {
				if(fis != null){
					fis.close();					
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
FileOutputStream实例:
@Test
	public void testOutputStream(){
		//1.创建一个File对象,表明要写入的文件位置
		//输出的物理文件可以不存在,当执行过程中,若不存在会自动创建。若存在,则会覆盖原有文件。
		File file = new File("hello2.txt");
		//2.创建一个FileOutputStream的对象,将file的对象作为形参传递给FileOutputStream的构造器中
		FileOutputStream fos = null;
		try {
		//3.写入操作
			fos = new FileOutputStream(file);
			fos.write(new String("I Love You!").getBytes());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			//4.关闭流
			if(fos != null)
			{
				try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

文件复制:

@Test 
	public static void copyFile(String src , String dest)
	{
				//1.提供文件复制的方法
				File f1 = new File(src);
				File f2 = new File(dest);
				
				//2.提供相应的流
				FileInputStream fis = null;
				FileOutputStream fos = null;
				
				try {
					fis = new FileInputStream(f1);
					fos = new FileOutputStream(f2);
					
					//3.实现复制
					byte[] b = new byte[20];
					int len;
					while((len = fis.read(b)) != -1){
						fos.write(b, 0, len);
					}
					
				} catch (Exception e) {
					e.printStackTrace();
					
				}finally{
					//4.关闭流
					if(fos != null){
						try {
							fos.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					if(fis != null){
						try {
							fis.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
	}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章