Java文件读取(一)

1.Java文件读取

相对路径:不需要完整的路径名也可以访问到这个文件,通常用..\代表上级文件路径名称
绝对路径:从文件顶层位置到最底层位置的一连串地址

1.1Java的IO流

1.1.1.Java流的划分

输入、输出流:这里的输入输出都是相对内存而言的。输入:只写不读。输出:只读不写。
字节、字符流:数据单元不同来区分。字节:8字节,字符:16位字符
节点、处理流:不是处理流的都是节点流。处理流在节点流/已存在流的基础上进行封装和连接

1.1.2.各种流之间类的划分

字节流:InputStream\OutputStream作为基类。前缀单词区分输入和输出
(对文件的操作都需要借助File这个类,File类可以新建、删除、修改文件名和目录、获得文件路径(绝对路径/相对路径)。但是这个类不能对文件内容本身进行操作!!!)
输出示例:hello world
字节输出

public class FileTest_1 {

	public static void main(String[] args) {
		File file = new File("E:\\", "新建文本文档.txt");//创建File对象,此构造器包含两个参数:父地址名,孩子地址名
		InputStream input = null;//创建一个输入流引用
		try {//不使用try,catch可以使用throws IOException
			input = new FileInputStream(file);//这个类用来访问文件,构造器带一个File类对象(字节流)
			int ch = 0;
			while((ch = input.read()) != -1) {//循环读取字节,不存在字节时read()返回-1
				System.out.print((char)ch); 
			}
		} catch(IOException e) {
			e.printStackTrace();
		} finally {
			if(input != null) {
				try {
					input.close();
				} catch(IOException e) {
					System.out.println(e.getMessage());
					e.printStackTrace();
				}
			}
		}
	}
	
}

字节输入

public static void main(String[] args) throws IOException {
		File file = new File("E:\\", "新建文本文档.txt");
		OutputStream output = null;
		String str = "hello world";
		byte[] bt = new byte[100];
		bt = str.getBytes();
		try {
			output = new FileOutputStream(file);
			output.write(bt, 0, bt.length);
			
		} catch(IOException e) {
			e.printStackTrace();
		} finally {
			output.close();
		}
}

对应文档便会出现str的字符信息
1.1.3.字符流:Reader/Writer为其基类

字符流的写入

public static void main(String[] args) {
		try (
			FileWriter fw = new FileWriter("E:\\新建文本文档.txt")){//Java7以后新更新内容,可以在try后()内关闭必须显示关闭的内容
			fw.write("人民有信仰\r\n");//要用\r\n换行才有效
			fw.write("国家有希望\r\n");
		} catch(FileNotFoundException e) {
			e.printStackTrace();
		}
}

字符流读取

//以行读取文件
public static void main(String[] args) {
		try (
			FileReader fr = new FileReader("E:\\新建文本文档.txt");
			BufferedReader br = new BufferedReader(fr)){
			String line;
			while((line = br.readLine()) != null) {
				System.out.print(line + '\n');
			}
		} catch(IOException e) {
			e.printStackTrace();
		}
}

//单字符读取文件

public static void main(String[] args) {
		File file = new File("E:\\\\新建文本文档.txt");
		Reader reader = null;
		try {
			reader = new InputStreamReader(new FileInputStream(file));
			int temp;
			while((temp = reader.read()) != -1) {
				if((char)temp != 'r') {
					System.out.print((char)temp);
				}
			}
		} catch(IOException e) {
			e.printStackTrace();
		}
		//第二种读取方式
		try (
				FileReader fr = new FileReader("E:\\新建文本文档.txt")){
			int hasRead = 0;
			while((hasRead = fr.read()) != -1) {
				if((char)hasRead != 'r')
					System.out.print((char)hasRead);
			}
		} catch(IOException e) {
			e.printStackTrace();
		}
	}

1.1.4.随机读取文件

public class File_4 {
	public static void main(String[] args) throws IOException {
		RandomAccessFile random = null;
		try {
			random = new RandomAccessFile("E:/新建文本文档.txt", "r");
			long fileLength = random.length();
			int beginIndex = (fileLength > 4) ? 4 : 0;
			random.seek(beginIndex);
			byte[] bt = new byte[10];
			int read = 0;
			while((read = random.read(bt)) != -1) {
				System.out.write(bt, 0, read);
			}
		} catch(IOException e) {
			e.printStackTrace();
		}finally {
			if(random != null) {
				try {
					random.close();
				} catch(IOException e) {
					e.printStackTrace();
				}
			}
		}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值