JAVA I/O流 初步学习

目录

一.File类的一些常用方法

二.字节流+输出流

三.字节流+输入流

四.字符流+输出流

五.字符流+输入流

六.对象输出流ObjectInputStream

七.字节流+输入流解决乱码

八.装饰模式

九.configer


 

 

 

一.File类的一些常用方法

1.创建文件夹

2.删除文件夹

3.创建多级目录,多个文件夹

4.在文件夹里创建文本

代码汇总

package com.gongsi.cn;

import java.io.File;
import java.io.IOException;
//创建文件夹 删除文件夹
public class TestFile {
	public static void main(String[] args) throws Exception {
//		File file=new File("c:\\bjyxszd");
//		//boolean isYes=file.mkdir();//新建文件夹
//		boolean isYes=file.delete();//删除文件夹
//		System.out.println(isYes);
		
		//使用JAVA的File类的对象来描述硬盘中的某个文件夹或者文件
//		File file=new File("c:/bjyxszd/bjyxszd.txt");
//		boolean isDel=file.delete();
//		System.out.println(isDel);
		
		
		//创建多级目录
//		File file=new File("c:\\bjyxszd/xz/wyb");
//		boolean is=file.mkdirs();
//		System.out.println(is);
		
		//在多级目录下创建文件
		File file=new File("c:\\bjyxszd/xz/wyb/ttkx.txt");
		boolean is=file.createNewFile();
		System.out.println(is);
		
	}

}

二.字节流+输出流

package com.gongsi.cn;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class TestOuputStreame {

	public static void main(String[] args) throws Exception {
		File file=new File("c:\\a/b.txt");
		OutputStream ou=new FileOutputStream(file);
		String b="今天为什么那么困啊啊啊!我是不是真的有病!";
		ou.write(b.getBytes());
		ou.close();
	}
	
}

三.字节流+输入流

package com.gongsi.cn;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
//字节流+输入流
public class TestInputStreame {
	public static void main(String[] args) throws Exception {
		
		
//		//这种写法用的少
//		//1.使用File类对象表示C盘中的a.txt对象
//		File file=new File("c:\\a/a.txt");//水池
//		//2.创建一个字节输入流对象
//		InputStream in=new FileInputStream(file);
//		//3.使用流管道循环从硬盘中读入到内存条中
//		int data =in.read();//读取一个字符
//		while (data!=-1) {
//			System.out.println((char)data);
//			data=in.read();//继续读
//			
//		}
//		in.close();
		
		//这种写法用得多
		//1.使用File类对象表示C盘中的a.txt对象
				File file=new File("c:\\a/a.txt");//水池
				//2.创建一个字节输入流对象
				InputStream in=new FileInputStream(file);
				//3.使用流管道循环从硬盘中读入到内存条中
				int data;//读取一个字符
				while ((data=in.read())!=-1) {
					System.out.println((char)data);
					
				}
				in.close();
		
		
	}

}

四.字符流+输出流

package com.gongsi.cn.ch1;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

//字符流+输出流
public class TestWritter {
	public static void main(String[] args) throws Exception {
		File file=new File("c:\\a/a.txt");
		Writer out=new FileWriter(file);
		BufferedWriter bw=new BufferedWriter(out);
		String str="刚刚我的奶茶去哪里!!!冰冰凉凉的";
		bw.write(str);
		bw.close();
		out.close();
	}

}

五.字符流+输入流

package com.gongsi.cn.ch1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

//字符流+输入流
public class TestReader {
	public static void main(String[] args) throws Exception {
//		File file=new File("c:\\a/a.txt");
//		Reader reader=new FileReader(file);
//		int r=reader.read();
//		while ((r=reader.read())!=-1) {
//			System.out.println((char)r);
//			
//		}
//		reader.close();
		
		//缓冲流  解决编码格式错误的问题
		File file=new File("c:\\a/a.txt");
		Reader reader=new FileReader(file);
		BufferedReader bufferedReader=new BufferedReader(reader);
		String r;
		while ((r=bufferedReader.readLine())!=null) {
			System.out.println(r);
			
		}
		//关流 从大到小
		bufferedReader.close();
		reader.close();
		
		
		
	}

}

六.对象输出流ObjectInputStream

对象要Serializable 标记

package com.gongsi.cn.ch2;

import java.io.Serializable;
//Serializable 标记
public class Student implements Serializable{
	private String name;
	private String age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public Student(String name, String age) {
		super();
		this.name = name;
		this.age = age;
	}
	public Student() {
		super();
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	
	

}
package com.gongsi.cn.ch2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;


public class TestObjectWritter {

	public static void main(String[] args) throws Exception {
		//对象输出流
//		Student student=new Student("张三","男");
//		File file=new File("c:\\a/student.txt");
//		OutputStream out=new FileOutputStream(file);
//		ObjectOutputStream oos=new ObjectOutputStream(out);
//		oos.writeObject(student);
//		oos.close();
//		out.close();
//		System.out.println("写出完毕");
		
		
		//对象输入流
		File file=new File("c:\\a/student.txt");
		InputStream in=new FileInputStream(file);
		ObjectInputStream ois=new ObjectInputStream(in);
		Student stu=(Student) ois.readObject();
		System.out.println(stu.toString());
		ois.close();
		in.close();
		
		
		
		
		
		
		
		
	}
}

七.字节流+输入流解决乱码

在字节流+输入流的基础上,修改代码解决出现乱码的情况

package com.gongsi.cn.ch3;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.swing.text.AbstractDocument.BranchElement;

public class Test {

	public static void main(String[] args) throws Exception {
		
		//乱码
//		File file=new File("c:\\a/a.txt");
//		InputStream in=new FileInputStream(file);
//		int str;
//		while ((str=in.read())!=-1) {
//			
//			System.out.println((char)str);
//		}
//		in.close();
		
		
		//解决乱码
		File file=new File("c:\\a/a.txt");
		InputStream in=new FileInputStream(file);
		InputStreamReader isr=new InputStreamReader(in);
		BufferedReader br=new BufferedReader(isr);
		String str;
		while ((str=br.readLine())!=null) {
			
			System.out.println(str);
		}
		in.close();
		
		
		
		
		
	}
}

八.装饰模式

1.例子

package com.gongsi.cn.ch4;

public class Person {
	public void eat() {
		System.out.println("正常吃饭");
	}

}
package com.gongsi.cn.ch4;

public class Supperperson {
	Person person;
	//构造方法
	public Supperperson(Person person) {
		this.person=person;
	}
	
	public void eat() {
		System.out.println("喝贡茶");
		person.eat();
		System.out.println("做按摩");
		System.out.println("去睡觉");
		System.out.println("去唱K");
	}

}

 

package com.gongsi.cn.ch4;

public class Test {

	public static void main(String[] args) {
		Person person =new Person();//被装饰者
		person.eat();
		System.out.println("---------");
		Supperperson supperperson=new Supperperson(person);//装饰者
		supperperson.eat();
	}
}

 2.例子

步骤:

1)构造方法传参

2)方法

3)关流

package com.gongsi.cn.ch5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;

import org.omg.CORBA.PUBLIC_MEMBER;

/**
 * 
 * @author 超级棒呀
 * 读取全部
 * MyBufferReader装饰着类
 *
 */
public class MyBufferReader {
	
	private Reader reader;//被装饰者类
	public MyBufferReader(Reader reader) {
		this.reader=reader;
	}
	public String readAll() throws Exception {
		StringBuffer sb=new StringBuffer();
		BufferedReader br=new BufferedReader(reader);
		String strLine;
		while ((strLine=br.readLine())!=null) {
			sb.append(strLine);
			
		}
		return sb.toString();
	}
	

	public void close() throws Exception {
		reader.close();
	}
	
	
	

}

package com.gongsi.cn.ch5;

import java.io.File;
import java.io.FileReader;
import java.io.Reader;

public class TestMyBufferReader {
	private void mian() throws Exception {
		File file=new File("C:\\a/a.txt");
		Reader reader=new FileReader(file);
		MyBufferReader myBufferReader=new MyBufferReader(reader);
		String str=myBufferReader.readAll();
		myBufferReader.close();
		reader.close();

	}

}

九.configer

 

package com.gongsi.cn.ch6;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Properties;

public class Configer {
	Properties properties=new Properties();
	public Configer(String filename) {
		File file=new File(filename);
		InputStream in;
		try {
			in=new FileInputStream(file);
			properties.load(in);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String get(String key) {
		String value=(String) properties.get(key);
		return value;
	}
	
	

}

 

package com.gongsi.cn.ch6;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Properties;

public class TestProperties {
	public static void main(String[] args) throws Exception {
//		Properties properties=new Properties();
//		File file=new File(".\\config\\jdbc.properties");
//		InputStream in=new FileInputStream(file);
//		properties.load(in);//输入流去加载硬盘中的文件到内存条中,存放在properties的对象中
//		String driver=(String) properties.get("jdbc.driver");
//		System.out.println(driver);
//		
//		String userName=(String) properties.get("jdbc.userName");
//		System.out.println(userName);
//		
//		String password=(String) properties.get("jdbc.password");
//		System.out.println(password);
		
		Configer configer=new Configer(".\\config\\jdbc.properties");
		String driver=configer.get("jdbc.driver");
		System.out.println(driver);
		String userName=configer.get("jdbc.userName");
		System.out.println(userName);
		String password=configer.get("jdbc.password");
		System.out.println(password);
		
		
	}

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值