I/O的一些小练习(见注释)

学习了两周的I/O知识,感觉有点迷糊,在这里做一个笔记吧!方便以后自己查看。

学习IO学了一大堆的函数,里面有各个常用流。

OutputStreamWriter/InputStreamReader可以指定编码方式,输出流默认会先清空文件的内容
FileInputStream/FileOutputStream读入字节信息/写出字节信息
BufferedReader/BufferedWriter /BufferedInputStream/BufferedoutputStream/PrintWriter缓存流,先写输出流再输入流
DataInputStream/DataOutputStream数据流
ObjectInputStream/ObejctOutputStream对象流
package io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
/*
 *@author birdA
 *@date:20200923
 */
public class Suibi {
	
	//创建一个UTF-8带BOM格式的空文件
	public static void getBOM(String filepath) {
		File file=new File(filepath);
		try(FileInputStream fis=new FileInputStream(file)){
			byte[] bytes=new byte[(int)file.length()];
			fis.read(bytes);
			System.out.println(bytes[0]);
			System.out.println("BOM的编码: ");
			for(byte b:bytes) {
				int i=b&0xff;
				System.out.print(Integer.toHexString(i));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	//去掉BOM编码
	public static void getRemove(String filepath) {
		File file=new File(filepath);
		byte[] bytes=new byte[(int) file.length()];
		try(FileInputStream fis=new FileInputStream(file)){			
			fis.read(bytes);
			System.out.println(file.length());
			if(bytes[0]==(byte)0xef&&bytes[1]==(byte)0xbb&&bytes[2]==(byte)0xbf) {
				byte[] bytes1=new byte[(int) file.length()-3];
				for(int i=0;i<file.length()-3;i++) {
					bytes1[i]=bytes[i+3];					
				}
				bytes=bytes1;			
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		try(FileOutputStream fos=new FileOutputStream(file)){
			fos.write(bytes);
			System.out.println(file.length());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	//移除注释
	public static void removeComments(String filepath) {
		File file=new File(filepath);
		char[] chars=new char[(int)filepath.length()]; 
		StringBuffer sb=new StringBuffer();
		try(FileReader fr=new FileReader(file);
				BufferedReader br=new BufferedReader(fr);
			){

			while (true) {
				// 一次读一行
				String s = br.readLine();				
				if (s != null) {					
					//去掉空行
					s=s.trim();
					System.out.println(s+"  字符串长度 "+s.length());
					if(s.indexOf("//")==0) {
						s="";
					} else {
						System.out.println(s);
						sb.append(s+"\n");
					}
				} else
					break;
			}									
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
	
	//练习向文件中写入两个数字,然后把这两个数字分别读取出来
	//缓存流写入
	public static void bufferedInt(String filepath) {
		File file=new File(filepath);
		try(FileWriter fw=new FileWriter(file);
				PrintWriter pw=new PrintWriter(fw);){
			pw.print("21@25");		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//缓存流读入和识别
	public static void bufferedReaderInt(String filepath) {
		File file=new File(filepath);
		char[] charint=new char[(int)file.length()];
		
		try(FileReader fr=new FileReader(file);
				BufferedReader br=new BufferedReader(fr)){
			br.read(charint);
			String s="0123456789";			
			for(int i=0;i<file.length();i++) {
				if(s.indexOf(Character.toString(charint[i]))!=-1) {
					System.out.print(charint[i]);
				}else
					System.out.println(" ");
			}		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	//对象流的输入序列化
	public static void main(String[] args) throws FileNotFoundException, IOException {	
		//文件写入空字符数组是占用字节的
//		char chars[]=new char[2];
//		System.out.println((int)chars[0]);
//		FileWriter fw=new FileWriter(new File ("F:/Wangchuang/io/diao"));
//		fw.write(chars);
//		fw.close();			
		objectSerializable("F:/Wangchuang/io/kil.txt");
		//bufferedReaderInt("F:/Wangchuang/io/diao");		
	}
	
	
	//将字符串按照指定编码写入文件
	public static void opwriter(String filepath) throws IOException {
		File file =new File(filepath);		
		FileOutputStream fos=new FileOutputStream(file);
		OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
		osw.write("1a12中");
		osw.close();
		fos.close();
		getBytes(file);
		
	}
	
	//输出字节内容(具体编码信息)
	public static void getBytes(File file) {
		try(FileInputStream fis=new FileInputStream(file)){
			byte[] bytes=new byte[(int)file.length()];
			fis.read(bytes);
			System.out.println("文件的字节信息为: ");
			for(byte b:bytes) {
				int i=b&0xff;
				System.out.print(Integer.toHexString(i)+" ");
			}			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
		
	//对象序列化与反序列化
	public static void objectSerializable(String filepath) {
		File file=new File(filepath);
		Hero[] heros=new Hero[10];		
		for(int i=0;i<10;i++) {
			heros[i]=new Hero("hero "+i);
		}
		for(Hero h:heros) {
			System.out.println(h);
		}
		
		try (
			// 输出
			FileOutputStream fos = new FileOutputStream(file);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			// 输入
			FileInputStream fis = new FileInputStream(file);
			ObjectInputStream ois = new ObjectInputStream(fis);
				) {
			oos.writeObject(heros);
			
			try {
				
				Hero[] heroa=(Hero[]) ois.readObject();
				
				for(Hero a:heroa) {
					System.out.println(a);
				}
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
				
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}	
}
public class TestFIleA {	
	//BufferedInputStream,BufferedOutputStream
	public static void copybyteStream(String copyingfilepath,String copyedfilepath) {
		File copyingfile=new File(copyingfilepath);
		File copyedfile=new File(copyedfilepath);
		try(BufferedInputStream bis=new BufferedInputStream(new FileInputStream(copyingfile));
				BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(copyedfile));
			)
		{				
			byte[] bytes=new byte[(int)copyingfile.length()];
			//将文件信息从缓冲区读入字节数组中去
			int len=0;
			while((len=bis.read(bytes))!=-1) {
				//把字节信息读入缓冲区
				bos.write(bytes);
				//把字节信息从缓冲区导入到文件中去
				bos.flush();			
			}				
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
		
//BufferedReader,BufferedWriter
	public static void copycharStream(String copyingfilepath,String copyedfilepath) {
		File copyingfile=new File(copyingfilepath);
		File copyedfile=new File(copyedfilepath);
		try(BufferedReader br=new BufferedReader(new FileReader(copyingfile));
			BufferedWriter bw=new BufferedWriter(new FileWriter(copyedfile));	
			)
		{	
			//从缓存流中一行一行读入数据
			while (true) {
				String line = br.readLine();
				if(line==null) {
					break;
				}
				System.out.println(line);
			}					
			//写入字符到缓存流中
			bw.write("小可爱\n");		
			//将缓存流中的信息写到文件中去
			bw.flush();			
			//另外一种写入方式
			try(PrintWriter pw=new PrintWriter(bw)){
				pw.println("我来了");
				pw.flush();
				pw.println("等等我");
				pw.flush();
			}								
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
	public static void main(String[] args) throws IOException  {
	}
}

9.24更新

/* @IO综合练习
 * @author bird-a
 * @DATE 20200924
 */
 
public class TestFIleA {	
	//复制单个文件
	public static void copyFile(String srcFile, String destFile){
		File sfile=new File(srcFile);
		File dfile=new File(destFile);
		if(sfile.exists()){
			if(sfile.isFile()) {
				try(FileInputStream fis=new FileInputStream(sfile);
						FileOutputStream fos=new FileOutputStream(dfile);)
				{
					byte[] bytes=new byte[(int)sfile.length()];
					fis.read(bytes);
					fos.write(bytes);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
		}	
	}
	
	//复制文件夹(文件夹里面可能也有文件夹)
	public static void copyDirection(String srcFile, String destFile) {
		File sfile=new File(srcFile);
		File dfile=new File(destFile);
		dfile.mkdirs();//除非已经存在,否则就会创建
		if(sfile.isDirectory()) {
			File[] files=sfile.listFiles();
			for(File file:files) {
				//复制前的准备工作
				File dsfile=new File(dfile,file.getName());//复制文件名过去
				//文件复制
				if(file.isFile()) {
					//dsfile.createNewFile();可加可不加
					copyFile(file.getAbsolutePath(),dsfile.getAbsolutePath());					
				}
				//文件夹复制
				if(file.isDirectory()) {
					copyDirection(file.getAbsolutePath(),dsfile.getAbsolutePath());					
				}				
			}						
		}		
	}
	
	//查找文件内容
	public static void search(File folder, String search) {	
		if(folder.exists()) {
			if(folder.isDirectory()) {
				File[] files=folder.listFiles();
				for(File file:files) {
					if(file.isDirectory()) {
						search(file,search);
					}
					else judgeinornot(file,search);
				}
			}
			if(folder.isFile()) {
				judgeinornot(folder,search);
			}				
		}else {
			System.out.println("文件不存在");
		}			
	}
	
	//判断文件里面有没有这个内容
	public static boolean judgeinornot(File file,String search) {
		try(FileReader fr=new FileReader(file)){
			char[] chars=new char[(int)file.length()];
			fr.read(chars);
			String s=new String(chars);	
			if(s.indexOf(search)!=-1) {
				System.out.println("找到目标字符串"+search+",位置在"+file.getAbsolutePath());
			}
			return s.indexOf(search)!=-1;		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
		return false;
	}
			
	public static void main(String[] args) throws IOException  {		
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值