Day10 JAVA复习

1.6 I/O流

1.6.1 文件夹(文件路径)与文件:File类

创建时文件和路径不同,删除时一起删除
creatNewFile:创建文件,需要路径存在
mkdirs:创建文件夹
delete:只能删除空文件夹

public class Demo {
	//创建文件,只是对文件的描述,不能把文件夹和文件真的创建出来
	public static void test1(){
		File file=new File("d:\\a\\a.txt");//在绝对路径下查找 
		System.out.println(file);
		file =new File("a.txt");//默认在当前工程文件夹下查找
		
		file=new File("d:\\a","b.txt");
		System.out.println(file);
		
		File path=new File("d:\\a");
		file=new File(path,"c.txt");
		System.out.println(file);
	}
	//创建文件夹,删除文件夹
	public static void test2(){
		File file =new File("d:\\a");
		//判断文件夹(文件)是否存在,返回true存在
		if(!file.exists()){
			//mkdir创建文件夹
			boolean b=file.mkdir();
			System.out.println(b);
		}else{
			System.out.println("文件夹已存在");
			System.out.println(file.delete());
		}
	}
	public static void test21(){
		File file =new File("d:\\b\\c");
		//判断文件夹(文件)是否存在,返回true存在
		if(!file.exists()){
			//mkdir创建文件夹
			boolean b=file.mkdirs();
			System.out.println(b);
		}else{
			System.out.println("文件夹已存在");
			file.delete();
			System.out.println(file.delete());
		}
	}
	public static void test22() throws IOException{
		File file =new File("d:\\b\\c\\d.txt");
		//判断文件夹(文件)是否存在,返回true存在
		if(!file.exists()){
			//mkdir创建文件夹
			
			boolean b=file.createNewFile();
			System.out.println(b);
		}else{
			System.out.println("文件夹已存在");
			file.delete();
			System.out.println(file.delete());
		}
	}
	public static void test3() throws IOException{
		File file=new File("a.txt");
		if(!file.exists()){
			boolean b=file.createNewFile();
			System.out.println(b);
		}else{
			System.out.println("文件已创建");
		}
	}
	
	public static void test4(){
		File file=new File("d:\\a\\b");
		System.out.println(file.isDirectory());//判断是否文件夹
		System.out.println(file.isFile());//判断是否文件
		System.out.println(file.getAbsolutePath());//获取文件路径
		File files[]=file.listFiles();//获取文件路径(文件夹)和文件
		for(File f:files){
			System.out.println(f);
		}
	}
	//打印a文件夹下的所有文件
	//递归调用
	public static void getFile(String path){
		File f=new File(path);
		if(f.exists()){
			File files[]=f.listFiles();
			//如果文件夹没有子文件夹和文件,跳出循环
			if(files.length==0){
				return;
			}
			for(File file:files){
				if(file.isDirectory()){
					System.out.println(file.getAbsolutePath());
					getFile(file.getAbsolutePath());
				}if(file.isFile()){
					System.out.println(file.getAbsolutePath());
				}
			}
		}
	}
	public static void main(String[] args) throws IOException {
		getFile("d:\\a");
	}
}

1.6.2 输入输出流与字节流

以程序为核心判断输入输出

//按照方向分:输入流和输出流
//读取方式分:字节流和字符流
/*InputStream  OutputStream
 * 文件字节流的输入输出
 * FileInputStream  FileOutputStream
 * 
 */
public class Demo2 {
	//把数据写到数据文件
	public static void test1() throws IOException{
		//如果文件不存在,会自动创建文件
		File file=new File("a.txt");
		//1.选择输出流
		FileOutputStream out=new FileOutputStream(file);
		String str="大家辛苦了";
		//2.选择写的方法
		out.write(str.getBytes());
		out.close();
	}
	//把数据从数据文件读出来
		public static void test2() throws IOException{
			File file=new File("a.txt");
			//1.选择输入流
			FileInputStream in=new FileInputStream(file);
			//2.选择方法
			byte[] buffer =new byte[(int) file.length()];
			//把数据读到数组中
			in.read(buffer);
			String str=new String(buffer);
			System.out.println(str);
			in.close();
	    }
		//一个字节一个字节的读
		public static void test3() throws IOException{
			//1.选择输入流
			FileInputStream in=new FileInputStream("a.txt");
			//识别英文字符
			/*int n=0;
			while((n=in.read())!=-1){
				System.out.print((char)n);
			}*/
			
			//识别汉字(GBK格式)
			//GBK一个汉字两个字节,utf-8一个汉字三个字节
			//如果用utf-8编码,应该3个字节显示
			//byte[] b=new byte[3];
			int n=0;
			byte[] b=new byte[2];
			while((in.read(b))!=-1){
				String str=new String(b);
				System.out.print(str);
			}
			in.close();
		}
		//实现数据的追加
		public static void test4() throws IOException{
			File file=new File("a.txt");
			FileOutputStream out=new FileOutputStream(file,true);
			String str="hello\n";//手动换行
			out.write(str.getBytes());
			out.close();
		}
	
	public static void main(String[] args) throws IOException {
		test4();
	}
	

}

1.6.3 对象流

//实现Serializable接口
//是一个标识性的接口,没有方法
//只要实现了这个接口,标识类被序列化了,创建的对象就叫序列化对象
public class Student implements Serializable{
	
	private String name;
	private int age;
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
}
/*
 * 对象流:ObjectInputStream  ObjectOutputStream
 * 序列化:把对象转换成字符序列,把字符序列写到数据文件(网络)
 * 反序列化:把字符序列还原成对象
 * 对象的序列化:标识类实现了Serializable接口(标识性的接口,没有方法),
 *            标识类被序列化了,创建的对象就叫序列化对象
 * 能够直接连接到文件的流叫节点流:例如FileInputStream
 * 不能直接连接到文件的流叫处理流:流的套接
 */
public class Demo4 {
	public static void test1() throws Exception {
		Student stu=new Student("张三",18);
		Student stu2=new Student("李四",18);
		//定义对象的输出流
		ObjectOutputStream oos=new ObjectOutputStream(
				new FileOutputStream ("oos.txt"));
		//写对象到数据文件
		oos.writeObject(stu);
		oos.writeObject(stu2);
		//关闭流
		oos.close();
	}
	//对象的输入流
	public static void test2() throws Exception{
		ObjectInputStream ois=new ObjectInputStream(
				new FileInputStream ("oos.txt"));
		//读对象(反序列化过程)
		Student stu= (Student) ois.readObject();
		Student stu2=(Student) ois.readObject();
		System.out.println(stu+","+stu2);
	}
	public static void main(String[] args) throws Exception   {
		// TODO Auto-generated method stub
		test2();
	}
}

1.6.4 字符流

/*
 * 字符流: Reader   Writer
 */
public class Demo5 {
	//FileWriter
	public static void test1() throws IOException{
		FileWriter fw=new FileWriter("fw.txt");
		fw.write("hello");
		fw.close();
	}
	//FileReader
	public static void test2() throws IOException{
		FileReader fr=new FileReader("fw.txt");
		char[] cfr=new char["fw.txt".length()];
		fr.read(cfr);
		String str=new String(cfr);
		System.out.println(str);
		fr.close();
	}
	//练习:实现两个数据文件之间的拷贝 fw.txt-》fw1.txt
	public static void test3() throws Exception{
		FileReader fr=new FileReader("fw.txt");
		char[] cfr=new char["fw.txt".length()];
		fr.read(cfr);
		String str=new String(cfr);
		FileWriter fw=new FileWriter("fw1.txt");
		fw.write(str);
		fw.close();
		fr.close();
	}
	public static void main(String[] args) throws Exception {
		test3();
	}
}

1.6.5 缓冲流

/*
 * 缓冲流(高级流)效率高
 * 
 */
public class Demo6 {
	//使用PrintWriter实现换行写数据
	public static void test1() throws Exception{
		PrintWriter pw=new PrintWriter(
				new FileOutputStream("fw.txt",true));//true表示可以追加内容,执行一次追加一行
		pw.println("yw 123456 adasd 354135");
		pw.close();//close方法执行之前,刷新缓冲区
	}
	//使用BufferedReader成行读取数据
	public static void test2() throws Exception{
		BufferedReader br=new BufferedReader(
				new FileReader("fw.txt"));
		String str="";
		//String str=br.readLine();
		//while((str)!=null)
		while((str=br.readLine())!=null){
			System.out.println(str);
		}
		br.close();
	}
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		test2();
	}
}

1.6.6 转换流

/*
 * 转换流 InputStreamReader   OutputStreamWriter
 */
 public static void test1() throws Exception{
		FileInputStream fis=new FileInputStream("fi.txt");
		//字节流转换成字符流
		BufferedReader br=new BufferedReader(
				new InputStreamReader(fis));
	}

1.6.7 控制台的I/O

System.out();
System.err();//输出红色字体

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值