文章会有【原创】或者【转载】标示,部分来自Google Baidu的学习结果 {Java/PHP/Python/Ruby/Go}

Java 在学习三十八【原创】  


文件的读写操作

1):字节流》读写以字节为单位 二进制

2):字符流》读写以字符为单位 只适合文本类


1):输入流》读操作

2):输出流》写操作


字节:

InputStream(读)抽象类 —> FileInputStream

OutputStream(写)抽象类 —> FileOutputStream


所有的IO操作都有IOException

FileInputStream in = new FileInputStream("/Users/houhuiyang/Web/Java/test.txt");

in.read()

【注意】从此输入流中读取一个字节,如果没有输入可用,则此方法阻塞

【注意】对于单字节编码没有问题,对于多字节的有乱码

【注意】对于大的文件读取效率比较低


通过字节数组来读取,称之为批量读取

byte[] b = new byte[1024*5];

int bytes = in.read(b, 0, b.length);

String s = new String(b, 0, bytes);

System.out.println(s);


package ioJava;


import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;


public class Test5 {


public static void main(String[] args) {

test1();

System.out.println("###############################");

test2();

System.out.println("###############################");

test3();

}

public static void test1(){

FileInputStream in;

try {

in = new FileInputStream(

"/Users/houhuiyang/Web/Java/test.txt");

int c;

//从此输入流中读取一个字节,如果没有输入可用,则此方法阻塞

//对于单字节编码没有问题,对于多字节的有乱码

//对于大的文件读取效率比较低

while ((c = in.read()) != -1) {

System.out.print((char) c);

}

in.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

public static void test2() {

FileInputStream in;

FileInputStream in2;

try {

long start = System.currentTimeMillis();

in = new FileInputStream("/Users/houhuiyang/Web/Java/test.txt");

System.out.println("*********************************");

// 通过字节数组来读取

byte[] b = new byte[1024 * 5];

int bytes = in.read(b, 0, b.length);

String s = new String(b, 0, bytes);

System.out.println(s);

System.out.println("*********************************");

long end = System.currentTimeMillis();

System.out.println(end - start);

long start1 = System.currentTimeMillis();

in2 = new FileInputStream("/Users/houhuiyang/Web/Java/test.txt");

byte[] bb = new byte[1024 * 5];

int bytes2;

while ((bytes2 = in2.read(bb, 0, bb.length)) != -1) {

String s2 = new String(bb, 0, bytes2);

System.out.println(s2);

}

System.out.println("*********************************");

long end1 = System.currentTimeMillis();

System.out.println(end1 - start1);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

public static void test3() {

try {

FileInputStream in = new FileInputStream("/Users/houhuiyang/Web/Java/test.txt");

// 返回可读的字节数

int length = in.available();

byte[] b = new byte[length];

in.read(b);

String s = new String(b);

System.out.println(s);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}


}


字符串转字节:

String test = "Hello\n北京\n成都\n太原";

byte[] testbyte = test.getBytes();


做缓冲输入:

BufferedInputStream —-> 先读入缓冲区

BufferedOutputStream —-> 先从缓冲区读取写入


package ioJava;


import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;


public class Test7 {


public static void main(String[] args) {

try {

// FileInputStream

FileInputStream in = new FileInputStream(

"/Users/houhuiyang/Web/Java/DSC03762.JPG");

// BufferedInputStream

BufferedInputStream br = new BufferedInputStream(in);

BufferedOutputStream bout = new BufferedOutputStream(

new FileOutputStream("/Users/houhuiyang/Web/Java/DSC03762_2.JPG"));

// 字节数组,放入内存

byte[] b = new byte[in.available()];

int bytes = br.read(b, 0, b.length);

// 先写入缓冲区,刷新缓冲区才会写入磁盘

bout.write(b, 0, bytes);

bout.flush();

bout.close();

br.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}


}


PipedInputStream —-> 管道流写入

PipedOutputStream —-> 管道流读取


【管道流】PipedOutputStream(PipedInputStream()) PipedInputStream(PipedOutputStream())

package ioJava;


import java.io.InputStream;

import java.io.OutputStream;

import java.io.PipedInputStream;

import java.io.PipedOutputStream;


public class Test8 {


public static void main(String[] args) {

try {

PipedInputStream pin = new PipedInputStream();

PipedOutputStream pout = new PipedOutputStream(pin);

Send send = new Send(pout);

Recdiver recdiver = new Recdiver(pin);


send.send();

recdiver.recdiver();


pin.close();

pout.close();

} catch (Exception e) {

e.printStackTrace();

}

}


}


class Send {

OutputStream out;


public Send(OutputStream out) {

super();

this.out = out;

}

public void send() {

try {

byte value = (byte) (Math.random()*1000);

System.out.println("send" + value);

out.write(value);

} catch(Exception e) {

e.printStackTrace();

}

}

}


class Recdiver {

InputStream in;


public Recdiver(InputStream in) {

super();

this.in = in;

}

public void recdiver() {

try {

byte value = (byte) in.read();

System.out.println("recdiver" + value);

} catch(Exception e) {

e.printStackTrace();

}

}

}