import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* int available() 返回没有读取的字节个数
* long skip(long n) 跳过n个字节,或者说是从第n个字节(下标)开始读取
*/
public class FileInputStreamTest04 {
public static void main(String[] args) {
FileInputStream it = null;
try {
it = new FileInputStream("temp.txt");
System.out.println("没有读取的字节个数:" + it.available());
byte[] bytes = new byte[it.available()];
System.out.println(" byte[] bytes = new byte[it.available()];");
System.out.println("这种方式不太适合太大的文件,因为byte[]数组不能太大,大概六千多万吧,60M左右");
int i ;
it.skip(2);
while((i = it.read() )!= -1){
System.out.print(i + " ");
/*
97 98 99 100 101 102 13 10 117 98 108 105 99
abcdef
ublic
一共13个元素,其中10是换行,13是回车
*/
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (it != null) {
try {
it.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* BufferedReader :
* 带有缓冲区的字符输入流
* 使用这个流的时候不需要自定义Char数组,或者说不需要自定义byte数组,自带缓冲
* String readLine() 读一行文本。
*/
public class BufferedReaderTest01 {
public static void main(String[] args) {
FileReader reader = null;
BufferedReader br = null;
try {
reader = new FileReader("myfile02");
String str = null;
br = new BufferedReader(reader);
while((str = br.readLine()) != null){
System.out.print(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 当一个流的构造方法中需要一个流的时候,这个被传进来的流就叫做:节点流(这里指FileReader)
* 外部负责包装的这个流,叫做:包装流,处理流(这里指BufferedReader)
*/
//br.readLine()方法读取一个文本行,但不带换行符
//关闭流,对包装流来说,只需要关闭最外层的流就行,节点流会自动关闭的(源码里的close方法里调用的)
}
}
import java.io.*;
/**
* 字节流转换为字符流:InputStreamReader
*/
public class BufferedReaderTest02 {
public static void main(String[] args) {
//字节流
FileInputStream fis = null;
BufferedReader br = null;
try {
//fis = new FileInputStream("myfile");
//转换,这里fis是节点流,reader是包装流
//InputStreamReader reader = new InputStreamReader(fis);
//字符流,这里reader是节点流,br是包装流(相对而言的,看在什么地方)
//br = new BufferedReader(reader);
//也可合并着写,套娃
br = new BufferedReader(new InputStreamReader(new FileInputStream("myfile")));
String s = null;
while((s = br.readLine()) != null){
System.out.println(s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.*;
/**
* BufferedWriter
*/
public class BufferedWriterTest01 {
public static void main(String[] args) {
BufferedWriter bw = null;
try {
//两个节点流,两个包装流(都是相对而言的)
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("myfile03",true)));
bw.write("hewllo world");
bw.write("\n");
bw.write("hahahaha");
bw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
拷贝文件(万能的)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
/**
* 使用FileInputStream + FileOutputStream完成文件的拷贝
* 拷贝的过程是一边读,一边写
* 使用以上的字节流拷贝文件时,文件类型随意,万能的,什么文件都能拷贝(注意:是文件,不是文件夹)
*/
public class CopyTest01 {
public static void main(String[] args) {
FileOutputStream opt = null;
FileInputStream ipt = null;
try {
//创建一个输入流对象(H盘-->内存)
//文件夹是不行的,必须要指定的文件
ipt = new FileInputStream("H:\\jdk1.8中文版.CHM");
//创建一个输出流对象(内存-->I盘)
opt = new FileOutputStream("I:\\jdk1.8中文版.CHM");
//核心,一边读,一边写
byte[] bytes = new byte[1024 * 1024]; //就是1000kb,1MB
int i = 0;
long time01 = System.currentTimeMillis();//获取拷贝前的时间
while((i = ipt.read(bytes)) != -1){
opt.write(bytes,0,i);
//假如很多字节,则i代表byte【】长度这么多字节,假如没有byte[]长度这么多,
// i就是读了所有的,有5个,i就是5
}
long time02 = System.currentTimeMillis();
System.out.println("拷贝花费了: " + (time02 - time01) + "毫秒");
opt.flush();//不要忘记刷新
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (ipt != null) {
try {
ipt.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(opt != null){
try {
opt.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//分开来try,因为如果在一起,第一个抛异常的话,就不再向下执行了
}
}
}
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* 使用FileReader FileWriter进行拷贝的话,只能拷贝“普通文本”文件
*/
public class CopyTest02 {
public static void main(String[] args) {
FileReader in = null;
FileWriter out = null;
try {
//读
in = new FileReader("myfile02");
//写
out = new FileWriter("Test\\src\\myfile03");
char[] chars = new char[1024 * 1024];
int readCount = 0;
while((readCount = in.read(chars)) != -1){
out.write(chars,0,readCount);
}
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}