6.Java高级编程 输入和输出处理一

Java高级编程 输入和输出处理一

一、File文件操作

File类的常用方法
方法名称说明
boolean exists( )判断文件或目录是否存在
boolean isFile( )判断是否是文件
boolean isDirectory( )判断是否是目录
String getPath( )返回此对象表示的文件的相对路径名
String getAbsolutePath( )返回此对象表示的文件的绝对路径名
String getName( )返回此对象表示的文件或目录的名称
boolean delete( )删除此对象指定的文件或目录
boolean createNewFile( )创建名称的空文件,不创建文件夹
long length()返回文件的长度,单位为字节**,** 如果文件不存在,则返回 0L
//绝对路径   相对路径
		File file  = new File("1.text");
		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());
		System.out.println("删除文件或目录"+file.delete());

		/*
		 * 1B字节 = 8bit(位)
		 * 1KB千字节 = 1024B
		 */
		String path = "D:\\4021/001";
		String name = "1.txt";
		
		try {
			File file1 = new File(path);
			if(!file1.exists()&&!file1.isDirectory()) {
				//进来创建
				System.out.println("创建目录");
				file1.mkdirs();//创建多级目录   file1.mkdir();创建文件夹||或目录
		
			}
			File file2 = new File(path,name);
			if(!file2.exists()&&file2.isFile()) {
				//进来创建
				boolean b = file2.createNewFile();//创建文件
			}
				
		} catch (Exception e) {
			e.printStackTrace();
		}

二、流的分类与使用

Java流的分类

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

**字节流:**byte

​ **输入流:**input(读取)

InputStream->(子类为)FileInputStream(路径)

​ .available():获得文件字节数量

​ .read(字节数组)||.read() 读取文件

​ is.close;//关闭流 释放资源

​ **输出流:**Output(写入)

​ OutputStream->FilOutputStream(“路径”,boolean 是否追加)

​ os.write(字符串.get.Bytes(“utf-8”));写入内容 将字符串转换为字节数组 并释放资源

​ os.close();//释放资源

​ os.flush(); 强制把缓冲区的数据写到输出流中

**字符流:**char(String)

​ 输入流:Reader

Reader->InputStreamReader(字节流InputStream,编码)->FileReader(路径)

​ 解决乱码问题 Reader a = new InputStreamReader(new FileInputStream(路径),“utf-8”);

​ 缓冲区 BufferedReader(Reader)

​ br.readLine();

​ 输出流:Writer

Writer->OutputStreamWriter(字节流OutputStream,编码)->FileWriter(路径||file对象)

​ 方法:

​ write(字符串)写入内容

​ flush()将缓冲区中的数据 刷新到文件中

​ close()释放资源

​ 缓冲区BufferedWriter(Writer)

​ append(字符)写入内容

​ write(字符串)写入内容

​ flush() 将缓冲区中的数据 刷新到文件中

​ close()释放资源

​ bw.newLine();另起一行

三、代码演示

/**
 * 输入流    input   读取文件内容  文件->代码    字节流
 * @author 26255
 *
 */
public class Text04 {

	public static void main(String[] args) {
		InputStream is = null;
		try {
			//创建流
			is = new FileInputStream("2.txt");
			//创建字节数组  用于储存文件信息
			byte b[] = new byte[is.available()];
			is.read(b);   //读取文件内容
			
			//将字节数组 转换为字符串
			String str  = new String(b,"utf-8");
			System.out.println(str);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if(is!=null)is.close();//关闭流 释放资源
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
				

/**
 * 输出流  output 写入       代码->文件   字节流
 * @author 26255
 *
 */
public class Text05 {

	public static void main(String[] args) {
		try {
			//创建流
			OutputStream os = new FileOutputStream("2.txt",true);
			os.write("zhisha".getBytes("utf-8"));
			os.close();//释放资源
		}catch(Exception e){
			e.printStackTrace();
		}

/**
 * 读取文件内容   输入流   字符流
 * @author 26255
 *
 */
public class Text06 {

	public static void main(String[] args) {
		try {
			//Reader reader = new FileReader("2.txt");
			//解决中文乱码问题
			Reader reader  =new InputStreamReader
					(new FileInputStream("2.txt"),"uft-8");
			//字符数组
			char c[] = new char[100];
			reader.read(c);
			//将数组转换为字符串
			String msg = new String(c);
			System.out.println(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
}





/**
 * 读取文件内容    输入流    缓冲区
 * @author 26255
 *
 */
public class Text07 {

	public static void main(String[] args) {
		try {
			//Reader reader = new FileReader("2.txt");
			//解决中文乱码问题
			Reader reader = new InputStreamReader
					(new FileInputStream("2.txt"),"utf-8");
			//创建缓冲区
			BufferedReader rb = new BufferedReader(reader);
			String line = null;
			while((line=rb.readLine())!=null) {
				System.out.println(line);
			}
			rb.close();
			reader.close();//结束
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

/**
 * 输出流   写入     字符流
 * @author 26255
 *
 */
public class Text08 {
	public static void main(String[] args) {
		try {
			//Writer writer = new FileWriter("2.txt");
			//解决乱码
			Writer writer = new OutputStreamWriter
					(new FileOutputStream("2.txt",true),"uft-8");
			writer.write("赔过钱");
			//writer.append("");
			System.out.println("写入成功!!!");
			writer.close();
		} catch (Exception e) {
		e.printStackTrace();
		}

	}

}





/**
 * 输出流    写入    字符流      缓冲区   
 * @author 26255
 *
 */
public class Text09 {

	public static void main(String[] args) {
		try {
			//Writer writer = new FileWriter("2.txt");
			//解决乱码
			Writer writer = new OutputStreamWriter
					(new FileOutputStream("2.txt",true),"uft-8");
			//创建缓冲区
			BufferedWriter bw = new BufferedWriter(writer);
			bw.newLine();//另起一行
			bw.append("田楷波");
			
			
			//将缓冲区中的数据  刷新到文件中
			bw.flush();
			System.out.println("写入成功!!");
			//释放资源
			bw.close();
			writer.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

四、序列化与反序列化

序列化:内存对象->文件 写入文件

​ ObjectOutputStream(OutStream字节流)

​ WriteObject(对象)

反序列化:文件->内存对象 读取文件

​ ObjectInputStream(InputStream字节流)

​ readObject()返回对象

注意:序列化对象要实现接口java.io.Serializable

/**
 * 序列化  写入
 * 反序列化  读取
 * @param args
 */
	public static void main(String[] args) {
		OutputStream os = null;
		ObjectOutputStream oos = null;
		InputStream is = null;
		ObjectInputStream ois = null;
		try {
			//创建字节流对象
			os = new FileOutputStream("D:\\xiangmueclipse\\expertch07\\src\\com\\hz\\ch04\\wwa.txt");
			//创建序列化对象
			oos = new ObjectOutputStream(os);
			List<Dog> list = new ArrayList<Dog>();
			
			//让对象 可序列化
			list.add(new Dog("张三","飒飒的萨摩亚"));
			
			//让对象 可序列化
			list.add(new Dog("张x","xxxxx"));
			oos.writeObject(list);
			
			//创建字节流对象
			is = new FileInputStream("3.txt");
			
			//创建反序列化对象
			ois = new ObjectInputStream(is);//读取
			
			 //Dog d = (Dog)ois.readObject();//读取
			//System.out.println(d.toString());
			List<Dog> dogs = (List<Dog>) ois.readObject();
			for(Dog d:dogs) {
				System.out.println(d.toString());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				os.close();
				ois.close();
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}

}

ystem.out.println(d.toString());
List dogs = (List) ois.readObject();
for(Dog d:dogs) {
System.out.println(d.toString());
}

	} catch (Exception e) {
		e.printStackTrace();
	}finally {
		try {
			os.close();
			ois.close();
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

懒洋洋大魔王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值