Java IO
字节(Byte,8位)流Stream,中文占用两个字节
字符流(char) reader,writer,涉及到字符传输的时候就可能需要编码,否则可能出现乱码
字符集(Charset):编码
一 字节流
public classReadByteStream {
public static voidmain(String[] args) {
try {
FileInputStream fis = newFileInputStream("hello.txt");
byte[] input = new byte[22];
fis.read(input);
String inputStr = new String(input);
System.out.println(inputStr);
fis.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
public classWriterByteStream {
public static voidmain(String[] args) {
try {
FileOutputStream fos = newFileOutputStream("hello1.txt");
String outStr = "write123 写入数据";
byte[] output = outStr.getBytes("UTF-8");
fos.write(output);
fos.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
二 通过字节流实现文件的拷贝
public classCopyByByteStream {
public static voidmain(String[] args) {
try {
FileInputStream fis = newFileInputStream("231.png");
FileOutputStream fos = newFileOutputStream("223.png");
byte[] input = new byte[50];
while(fis.read(input) != -1) {
fos.write(input);
}
fos.close();
fis.close();
System.out.println("done");
} catch(Exception e) {
e.printStackTrace();
}
}
}
实现视频拷贝
public class ReadByBufferStream {
public static void main(String[] args) {
try {
long time = System.currentTimeMillis();
FileInputStream fis = new FileInputStream("kero.rmvb");
BufferedInputStream bis = new BufferedInputStream(fis);//(fis,50000000)
FileOutputStream fos = new FileOutputStream("new.rmvb");
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] input = new byte[10000000];
while(bis.read(input) != -1) {
bos.write(input);
}
bis.close();
fis.close();
bos.close();
fos.close();
System.out.println((System.currentTimeMillis() - time)/1000 + "s");
} catch (Exception e) {
e.printStackTrace();
}
}
}
字符流
public class RWByCharStream {
public static void main(String[] args) {
try {
File file = new File("hello.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
FileOutputStream fos = new FileOutputStream("newHello.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
char[] input = new char[100];
while(isr.read(input) != -1) {
osw.write(input);
}
osw.close();
fos.close();
isr.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//字符流
public class RWByBufferedCharStream {
public static void main(String[] args) {
try {
File file = new File("hello.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
FileOutputStream fos = new FileOutputStream("newHello.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
BufferedReader br = new BufferedReader(isr);
PrintWriter pw = new PrintWriter(osw);
//BufferedWriter bw = new BufferedWriter(osw);
String input = null;
while((input = br.readLine()) != null) {
//bw.write(input);
pw.println(input);
}
//bw.flush();
pw.flush();//把缓冲写入
pw.close();
//bw.close();
osw.close();
fos.close();
br.close();
isr.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
文件读取与写入
public class FileReaderWriter {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("hello.txt");//字符文本
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("new2hello.txt");
BufferedWriter bw = new BufferedWriter(fw);
String line = null;
while((line = br.readLine()) != null) {
bw.write(line + "\n");
}
bw.flush();
bw.close();
fw.close();
br.close();
fr.close();
System.out.println("do");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
RandomAccessFile 读取文件中的特定内容
public class WriteFile extends Thread {
File file;
int block;
public WriteFile(File f, int b) {
this.file = f;
this.block = b;
}
@Override
public void run() {
try {
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.close();
//raf.s
} catch (Exception e) {
}
}
}
使用Apache commons.io库
import org.apache.commons.io.FileUtils;
public class TestFileUtils {
public static void main(String[] args) {
File file = new File("hello.txt");
File file2 = new File("new3hello.txt");
try {
FileUtils.copyFile(file, file2);
} catch (Exception e) {
// TODO: handle exception
}
}
}