输入输出流

public static void main(String[] args) {
		// TODO Auto-generated method stub
		File file=new File("a.txt");
		if (!file.exists()) {
			boolean flag;
			try {
				flag = file.createNewFile();//创建
				System.out.println(flag?"文件创建成功":"失败");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		String path=file.getAbsolutePath();//获取绝对路径
		System.out.println(path);
		String filewname=file.getName();//获取名字+后缀
		System.out.println(filewname);
		long len =file.length();
		System.out.println(len );//获取长度
		if (file.isFile()) {//盘对是否是文件夹
			System.out.println("文件");
		}else {
			System.out.println("文件夹");
		}
	}

字节流

public static void read1() {
		File file=new File("a.txt");
		FileInputStream in=null;
		
		try {
			in=new FileInputStream(file);
			int len=(int)file.length();//获取字节
			for (int i = 0; i <len; i++) {
				char result=(char)in.read();//每次读取一个字节
				System.out.println(result);//没次输出一个字节
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if (in!=null) {
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void read2() {
		File file=new File("a.txt");
		FileInputStream in=null;
		try {
			in=new FileInputStream(file);
			byte bts[]=new byte[1024];//建议使用1024,边缘会出现中文乱码
			int len=0;
			while((len=in.read(bts))!=-1){
				//每次读取byte【100】长度的内容放到bts数组中
				//每次读取返回当前读取的数量,如果结束返回-1
				String str=new String(bts,0,len);//将byte数组转string	
				System.out.println(str);
			}
			System.out.println("读取完毕");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}//finally
	}
	public static void write1() {
		File file=new File("a.txt");
		FileOutputStream out=null;
		Scanner input=new Scanner(System.in);
		System.out.println("请输入要插入的内容");
		String str=input.next();
		
		try {
			out=new FileOutputStream(file,false);//如果文件不存在自动创建
			char ch[]=str.toCharArray();
			for(char c:ch){
				out.write(c);
			}
			System.out.println("保存成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}//finally	
	}
	
	public static void  write2() {
		File file=new File("a.txt");
		FileOutputStream out=null;
		Scanner input=new Scanner(System.in);
		System.out.println("请输入要插入的内容");
		String str=input.next();
		try {
			out=new FileOutputStream(file,false);
			byte bts[]=str.getBytes();
			out.write(bts);
			System.out.println("保存成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

复制文件
复制文件
字节流转字符流
在这里插入图片描述
数据流
在这里插入图片描述
在这里插入图片描述
对象流
在这里插入图片描述
总结:

  1. 根据方向分为:输入流(记事本中编写数据是临时保存在内存中的,将内存中的数据写入硬盘)writer+out,输出流reader inputStream(从硬盘中读取出数据显示到记事本)
  2. 根据传输格式不同,分为字符流(以两个字节为单位)reader+writer和字节流(以字节为基本单位,一个英文是一个字节,一个中文是两个字节)inputStream+outputStream
  3. 字节流:每次默认读取或者写入一个字节,但是也可以设置一个byte数组,每次建议读取1024个字节的内容,而每次写入如果字符串不是太大就直接根据字符串的长度来定义byte数组,但注意仍然会出现个别乱码,同时要注意每次转化为字符串的长度要根据当前实际读取的长度进行转换。(如果是文档还是不建议使用字节流)
  4. 字符流:字符流默认每次写入或者读取的是一个字符串,效率底下,所以我们会搭配缓冲流,每次读取或者写入的是一行的数据,但是默认是不会换行的,记得手动插入换行。(中文肯定不会乱码,所以如果是文档那么建议都使用字符流)
  5. 字节流转字符流inputStreamReader(传输的时候用字节流无论是什么平台都看成一个整体,接收之后根据刚开始存储的编码格式进行解码转化为对应的字符流,那么读取的是中文就不会乱码了。)网上传输的数据都是字节流?因为系统平台的不同需要找到一个共同的格式进行传输,字节流是不看文件类型,如果是文本文件可以再接受之后将字节流转化为字符流在读取就可以保证不会乱码了。
  6. 数据流(datainputStream+dataoutputStream),不能单独使用,必须搭配字节流,重点是必须根据写入的顺序读取出来。
  7. 对象序列化(objectOutputStream)和反序列化(objectinputStream),不能单独使用,必须搭配字节流,重点是对象必须实现序列化接口

public static void main(String[] args) {
	String path="http://200019.ip138.com/";
	InputStream is=null;
	InputStreamReader read=null;
	BufferedReader br=null;
	URLConnection con=null;
	
	try {
		URL url=new URL(path);//统一资源定位符
		con=url.openConnection();//连接到该地址
		is=con.getInputStream();//获得链接到该地址的字节流
		read=new InputStreamReader(is);//转化为字符流
		br=new BufferedReader(read);//转化为缓冲流
		String str="";
		StringBuffer sb=new StringBuffer();
		while ((str=br.readLine())!=null) {
			sb.append(str);
		}
		
		System.out.println(sb);
	} catch (Exception e) {
		// TODO: handle exception
		e.printStackTrace();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值