Java I/O
1.Java的核心库java.io提供了全面的IO接口。包括:文件读写、标准设备输出等。Java中IO是以流为基础进行输入输出的,所有数据被串行化写入输出流,或者从输入流读入。
2.分类
字符流:输入流(read)输出流(writer) 传输的是一个字节
字节流:输入流(InputStream)输出流(OutputStream) 传输的是一个字符
(1).字节输出流
超类:java.io.OutputStream
需要注意的几个子类:java.io.FileOutputStream
(2).字节输入流
超类:java.io.InputStream
需要注意的几个子类:java.io.FileInputStream
(3).字符输出流
超类:java.io.Writer(抽象类)
需要注意的几个子类:
(4).字符输入流:java.io.BufferedWriter,java.io.OutputStreamWriter
超类:java.io.Reader (抽象类)
需要注意的几个子类:java.io.BufferedReader,java.io.InputStreamReader
package io;
import java.io.*;
import java.util.stream.Stream;
public class Dome01 {
public static void main(String[] arg){
String s = "C:\\Users\\MES\\Desktop\\gggg.txt";
// creaTxt(s);
// read(s);
// input(s);
// readTst(s);
readTest(s);
}
//创建新文件
public static void creaTxt(String file){
File file1 = new File(file);
if(!file1.exists()){
try {
boolean newFile = file1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void read(String filePath){
try{
File file = new File(filePath);
if(file.isFile() && file.exists()){
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(filePath),"UTF-8");
BufferedReader br = new BufferedReader(inputStreamReader);
String lineTXT = null;
int read = inputStreamReader.read();
System.out.println(read);
//按行读取
while((lineTXT = br.readLine()) != null){
System.out.println(lineTXT);
System.out.println("kllll");
}
br.close();
}else{
System.out.println("文件不存在");
}
}catch (Exception e){
System.out.println("文件读取失败!");
}
}
public static void input(String file){
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
//显示的是ASCII表中的十进制的值
int read = fileInputStream.read();
int read1 = fileInputStream.read();
int read2 = fileInputStream.read();
System.out.println(read);
System.out.println(read1);
System.out.println(read2);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void readTst(String file){
FileReader fileReader = null;
try {
fileReader = new FileReader(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int read1 = 0;
try {
read1 = fileReader.read();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader bufferedReader = new BufferedReader(fileReader);
int read = 0;
try {
read = bufferedReader.read();
} catch (IOException e) {
e.printStackTrace();
}
String s = null;
try {
s = bufferedReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
//读取之后 再读这个文本之前 已经读过的字符就不会再读了
System.out.println(s);
System.out.println(read);
System.out.println(read1);
}
public static void readTest(String file){
FileReader fileReader = null;
try {
fileReader = new FileReader(file);
int read = fileReader.read();
System.out.println(read);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}