读入字节流:FileInputStream
FileInputStreamDemo.Java
- package com.tianjf;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- public class FileInputStreamDemo {
- public static void main(String[] args) throws IOException {
- /** 知道文件大小的读取 */
- /* 整个读取 */
- String fileName = "D:" + File.separator + "hello01.txt";
- File file = new File(fileName);
- InputStream in = new FileInputStream(file);
- byte[] bytes = new byte[(int) file.length()];
- in.read(bytes);
- in.close();
- System.out.println(new String(bytes));
- /* 一个字节一个字节读取 */
- in = new FileInputStream(file);
- bytes = new byte[(int) file.length()];
- for (int i = 0; i < bytes.length; i++) {
- bytes[i] = (byte) in.read();
- }
- in.close();
- System.out.println(new String(bytes));
- /** 不知道文件大小的读取,需要判断是否读到了结尾 */
- /* 每次读取1KB */
- in = new FileInputStream(file);
- bytes = new byte[1024];
- while (true) {
- // 读取1KB的内容到数组中
- int num = in.read(bytes);
- // 返回-1则表示读到了结尾
- if (num == -1) {
- break;
- }
- // 将字节数组转换为字符串(index = 0, length = num)
- String str = new String(bytes, 0, num);
- System.out.println(str);
- }
- in.close();
- /* 一个字节一个字节的读取 */
- in = new FileInputStream(file);
- bytes = new byte[1024];
- int count = 0;
- int tempByte = 0;
- while ((tempByte = in.read()) != -1) {
- bytes[count++] = (byte) tempByte;
- }
- in.close();
- System.out.println(new String(bytes));
- }
- }
写入字符流:FileWriter
FileWriterDemo.java
- package com.tianjf;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.Writer;
- public class FileWriterDemo {
- public static void main(String[] args) throws IOException {
- String fileName = "D:" + File.separator + "hello.txt";
- File file = new File(fileName);
- String str = "hello";
- Writer out = new FileWriter(file);
- out.write(str);
- out.flush();
- out.close();
- /* 追加新内容到文件中 */
- out = new FileWriter(file, true);
- out.write("\r\n" + str);
- out.flush();
- out.close();
- }
- }
读入字符流:FileReader
FileReaderDemo.java
- package com.tianjf;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.Reader;
- public class FileReaderDemo {
- public static void main(String[] args) throws IOException {
- String fileName = "D:" + File.separator + "hello.txt";
- File file = new File(fileName);
- char[] chars = new char[100];
- Reader in = new FileReader(file);
- int count = in.read(chars);
- in.close();
- System.out.println("读入的长度为:" + count);
- System.out.println("内容为:" + new String(chars, 0, count));
- /* 依次读取,需要判断是否读到了结尾 */
- chars = new char[100];
- in = new FileReader(file);
- int tempChar = 0;
- count = 0;
- while ((tempChar = in.read()) != -1) {
- chars[count++] = (char) tempChar;
- }
- in.close();
- System.out.println("内容为:" + new String(chars, 0, count));
- }
- }
使用字节流好还是字符流好?
答案是字节流。首先因为硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容。但是字符只是在内存中才会形成的,所以在开发中,字节流使用广泛。缓冲区:BufferdReader
BufferdReaderDemo.java
- package com.tianjf;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- public class BufferdReaderDemo {
- public static void main(String[] args) throws IOException {
- BufferedReader br = null;
- BufferedWriter bw = null;
- bw = new BufferedWriter(new FileWriter("D:" + File.separator + "hello.txt"));
- br = new BufferedReader(new FileReader("D:" + File.separator + "hello.txt"));
- String s = null;
- for (int i = 1; i <= 100; i++) {
- s = String.valueOf(Math.random());
- bw.write(s);
- // 写入一个行分隔符
- bw.newLine();
- }
- bw.flush();
- while ((s = br.readLine()) != null) {// BufferdReader方便的方法readLine()读一行
- System.out.println(s);
- }
- bw.close();
- br.close();
- }
- }
使用Scanner类从文件中读出内容
ScannerDemo.java
- package com.tianjf;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class ScannerDemo {
- public static void main(String[] args) throws FileNotFoundException {
- File file = new File("d:" + File.separator + "hello.txt");
- Scanner scanner = new Scanner(file);
- while (scanner.hasNext())
- {
- System.out.println(scanner.next());
- }
- }
- }
