package string.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 字符流
* FileReader
* FileWriter
*
* BufferedReader
* BufferedWriter
* 字节流
* InputStream OutputStream
*
*/
public class InputStreamDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// writer();
// reader1();
//reader2();
reader3();
}
public static void writer() throws IOException {
FileOutputStream fos = new FileOutputStream("demo.txt");
fos.write("guwenrenzaichangsha".getBytes());
fos.close();
}
/**
* 单字节读取
*
* @throws IOException
*/
public static void reader1() throws IOException {
FileInputStream fis = new FileInputStream("demo.txt");
int len = 0;
while ((len = fis.read()) != -1) {
System.out.println((char) len);
}
}
/**
* 字节数组读取
*
* @throws IOException
*/
public static void reader2() throws IOException {
FileInputStream fis = new FileInputStream("demo.txt");
byte[] bytes = new byte[1024];
int len = 0;
while ((len = fis.read(bytes)) != -1) {
System.out.println(new String(bytes, 0, len));
}
}
/**
* available 方式读取(文件较大不建议使用)
*
* @throws IOException
*/
public static void reader3() throws IOException {
FileInputStream fis = new FileInputStream("demo.txt");
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
System.out.println(new String(bytes));
}
}