Java之I/O流

本文介绍了如何使用Java进行基本的文件操作,包括文件的创建与删除、获取文件信息、文件的输入输出流操作以及带缓存的输入输出流的使用。通过实例展示了不同场景下文件操作的具体实现。

1,文件的创建与删除

package Ioliu;

import java.io.File;
import java.io.IOException;

public class FileTest {
	public static void main(String[] args){
		File file = new File("word.txt");
		if(file.exists()){
			file.delete();
			System.out.println("文件已删除");
		}else{
			try {
				file.createNewFile();
				System.out.println("文件已创建");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}

}
文件已创建

2.获取文件信息

package Ioliu;

import java.io.File;
import java.io.IOException;

public class FileTest {
	public static void main(String[] args){
		File file = new File("word.txt");
		if(file.exists()){
			String name = file.getName();
			long length = file.length();
			boolean hidden = file.isHidden();
			String path = file.getAbsolutePath();
			System.out.println("文件名称:" + name);
			System.out.println("文件长度是: " + length);
			System.out.println("该文件是隐藏文件吗?" + hidden);
			System.out.println("文件的绝对路径:" + path);
		}else{
			System.out.println("该文件不存在");
		}
	}
}

文件名称:word.txt
文件长度是: 0
该文件是隐藏文件吗?false

文件的绝对路径:C:\Users\lenovo\workspace\Learn\word.txt

2:文件输入/输出流

FileInputStream类 , FileOutputStream 类 ,//字节输出,写入流

FileReader 类,FileWriter 类//字符输出,写入流

package Ioliu;

import java.io.*;

public class FileStreamTest {
	public static void main(String[] args){
		File file = new File("word.txt");
		try {
			FileOutputStream out = new FileOutputStream(file);
			byte buy[] = "你好!".getBytes();
			out.write(buy);//将数组中的信息写入到文件中
			out.close();//将流关闭
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			FileInputStream in = new FileInputStream(file);
			byte byt[] = new byte[1024];
		    int len = in.read(byt);//从文件中读取信息
		    System.out.println("文件中的信息是:" + new String(byt,0,len));
		    in.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			FileWriter out = new FileWriter(file);
			String s = "你好啊!";
			out.write(s);
			out.close();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try{
			FileReader in = new FileReader(file);
			char byt[] = new char[1024];
			int len = in.read(byt);
			System.out.println("文件中的信息:" + new String(byt,0,len));
			in.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}


3.带缓存的输入输出流

BufferedInputStream类 与 BufferedOutputStream类,它们与OutputStream InputStream 的区别是flush方法。

文件 ----> InputStream---->BufferedInputStream----->目的地

BufferedReader 类 与 BuffersdWriter类继承Reader类与writer类,可以以行为单位进行输入输出

字符数据----->BufferedWriter------>OutputStreamWriter------>OutputStream---->文件

package Ioliu;

import java.io.*;

public class Student {
	public static void main(String args[]){
		String content[] = {"好久不见","最近好吗","常联系"};
		File file = new File("word.txt");
		try {
			FileWriter fw = new FileWriter(file);
			BufferedWriter bufw = new BufferedWriter(fw);
			for(int k = 0; k < content.length; k++){
				bufw.write(content[k]);//将字符串数组中的元素写入到磁盘文件中
				bufw.newLine();//将数组中的单个元素以单行的形式写入文件
			}
			bufw.close();
			fw.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			FileReader fr = new FileReader(file);
			BufferedReader bufr = new BufferedReader(fr);
			String s= null;
			int i = 0;
			while((s = bufr.readLine())!=null){
				i++;
				System .out.println("第" + i + "行:" + s);
			}
			bufr.close();
			fr.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值