I/O流

1、什么是流?

  流可以理解为一种传输数据的通道,I/O流(即Input/Output,输入/输出),java中所有输入流都是抽象类 InputStream(字节输入流) 或抽象类 Reader(字符输入流) 的子类;而所有输出流都是抽象类OutputStream(字节输出流) 或抽象类Writer(字符输出流) 的子类。

2、输入流

  InputStream类是字节输入流的抽象类,是所有字节输入流的父类。java中的字符是Unicode编码,是双字节的。InputStream是用来处理字节的,不适合处理字符文本。所以java有单独的类Reader来处理字符,但Reader类并没有替换InputStream类。在这里插入图片描述
在这里插入图片描述

3、输出流

  同理,OutputStream类是字节输入流的抽象类,是所有字节输入流的父类。java中的字符是Unicode编码,是双字节的。OutputStream是用来处理字节的,不适合处理字符文本。所以java有单独的类Writer来处理字符,但Writer类并没有替换OutputStream类。
OutputStream在这里插入图片描述

4、File类

File类是java.io包中唯一代表磁盘文件本身的对象。File类定义了一些方法来操作文件,可以通过调用File类中的方法,实现创建、删除、重命名文件等操作。数据流可以将数据写入到文件中,文件也是数据流最常用的数据媒体。

1、文件删除和创建

创建的三种方式:

  1. File(String pathname)
    new File(String pathname)
    其中pathname指路径名称
    File file = new File(“d:/1.txt”);
  2. File(String parent,String child)
    根据父路径和子路径字符串(包含文件名)创建一个新的File对象。
  3. File(File f,String child)
    new File(File f,String child)

2、获取文件信息

方法返回值说明
getNameString获取文件名称
canRead()boolean判断文件是否可读
canWrite()boolean判断文件是否可被写入
exists()boolean判断文件是否存在
length()long获取文件的长度
getAbsolutePath()String获取文件的绝对路径
getParent()String获取文件的父路径
isFile()boolean判断文件是否存在
isDirectory()boolean判断文件是否为一个目录
isHidden()boolean判断文件是否为隐藏文件
lastModified()long获取文件最后修改时间
//读取文件中的内容,最好的方式是设置缓冲区。
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class Lian03zuoye {
	public static void main(String[] args) throws IOException {
		try {
			Reader fr =new FileReader("C:\\myFile\\my Prime.txt");
			char[] c= new char[3]; //设置缓冲区
			StringBuilder sBuilder=new StringBuilder();
			int i =0;
			while((i=fr.read(c))!=-1) {
				System.out.println(123);//每3个字符(缓冲区的长度)循环一次
				String we =new String(c,0,i);
				//一定要加i来限定字符串长度,则分割后的字符串最后一个字符串中的空字符就不会被自动补充。
				sBuilder.append(we);
			}
			System.out.println(sBuilder);
			fr.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.nio.file.Files;


//文件读取和文件复制
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class Lian02zuoye12 {
	@SuppressWarnings("null")
	public static void main(String[] args) throws IOException {
		
		//练习1
//		File file = new File("D:\\wen.java");
//		file.createNewFile();
//		System.out.println("文件是否存在:"+file.exists());
//		System.out.println("文件是否是文件:"+file.isFile());
//		System.out.println("文件是否是目录:"+file.isDirectory());
//		System.out.println("文件相对路径名:"+file.getPath());
//		System.out.println("文件绝对路径名:"+file.getAbsolutePath());
//		System.out.println("文件名字:"+file.getName());
//		System.out.println("文件大小:"+(file.length()/1000+file.length()%1000*0.001)+"KB");
//		file.delete();
		
		//方法不好舍去
//		FileInputStream fis =new FileInputStream("D:\\JAVA\\课堂资料\\java高级\\day02\\CollectionsTest02\\src\\suffle\\Student.java");
//		System.out.println(fis.available());
//		try {
//			for (int i = 0; i < fis.available()*10000; i++) {
//				System.out.print((char)fis.read());	
//			
//			}
//		} catch (Exception e) {
//			// TODO: handle exception
//		}finally {
//			fis.close();
//		}
		
		
//		FileOutputStream fos = new FileOutputStream("D:\\\\JAVA\\\\课堂资料\\\\java高级\\\\day02\\\\CollectionsTest02\\\\src\\\\suffle\\\\Student.java");
//		String a = "好好学习,天天向上";
//		byte[] words = a.getBytes();
//		//fos.write(102);//输入一个int类型的编码
//		fos.write(words, 0, words.length);
		
		
		
		//练习2
		//写入文件内容
		File file = new File("D:\\我的青春谁做主.txt");
		file.createNewFile();
		String k = "我的青春当然是我做主啦!!";
		byte[] word = k.getBytes();
		//String kk = new String(word,"utf-16");
		FileOutputStream o =new FileOutputStream(file);
		o.write(word, 0, word.length);
		o.flush();
		o.close();
		
		
		//拿出文件里的内容
		FileInputStream a =new FileInputStream("D:\\我的青春谁做主.txt");
		byte tt[] = new byte[1024];
		int len = 0;
		int rrad =0;
		while ((rrad=a.read())!=-1) {
			tt[len]=(byte)rrad;
			len++;
		}
		String we = new String(tt,0,len);
		System.out.println(we);
		o.flush();
		a.close();
		
		
		//再将拿出的内容放入要复制的文件中
		String s=we.toString();
		File outFile = new File("C:\\myFile");
		outFile.mkdirs();
		FileOutputStream d = new FileOutputStream("C:\\myFile\\my Prime.txt");
		byte[] words = s.getBytes();
		d.write(words, 0, words.length);
		o.flush();
		d.close();
		
		
		//输入两个文件File source, File dest
//		InputStream input = null;     
//	    OutputStream output = null;     
//	    try { 
//	           input = new FileInputStream(source); 
//	           output = new FileOutputStream(dest);         
//	           byte[] buf = new byte[1024];         
//	           int bytesRead;         
//	           while ((bytesRead = input.read(buf)) > 0) { 
//	               output.write(buf, 0, bytesRead); 
//	           } 
//	    } finally { 
//	        input.close(); 
//	        output.close(); 
//	    } 
	}
	
	//Files类 复制两个文件(java7)
	public void name(File a,File b) throws IOException {
		Files.copy(a.toPath(), b.toPath());
	}
	
	//使用Commons IO复制 
//	private static void copyFileUsingApacheCommonsIO(File source, File dest) 
//	        throws IOException { 
//	    FileUtils.copyFile(source, dest); 
//	} 
	
	
	//使用FileChannel复制 
	private static void copyFileUsingFileChannels(File source, File dest) throws IOException {     
        FileChannel inputChannel = null;     
        FileChannel outputChannel = null;     
	    try { 
	        inputChannel = new FileInputStream(source).getChannel(); 
	        outputChannel = new FileOutputStream(dest).getChannel(); 
	        outputChannel.transferFrom(inputChannel, 0, inputChannel.size()); 
	    } finally { 
	        inputChannel.close(); 
	        outputChannel.close(); 
	    } 
	}
	
	//使用FileStreams复制 
	private static void copyFileUsingFileStreams(File source, File dest) 
	        throws IOException {     
	    InputStream input = null;     
	    OutputStream output = null;     
	    try { 
	           input = new FileInputStream(source); 
	           output = new FileOutputStream(dest);         
	           byte[] buf = new byte[1024];         
	           int bytesRead;         
	           while ((bytesRead = input.read(buf)) > 0) { 
	               output.write(buf, 0, bytesRead); 
	           } 
	    } finally { 
	        input.close(); 
	        output.close(); 
	    } 
	} 
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值