public class StreamTest2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//File file=new File("/Users/hanzhao/Desktop/zhaohanhan.txt");
//System.out.println(file.exists());
//题外话
String s="aa我";
System.out.println(s.length());//返回3个字符char
byte[]bytes1=s.getBytes();
System.out.println(bytes1.length);//返回5个字节,一个字母一个字节,一个汉字两个字节
//FileOutputStream fos = null;//局部变量要赋值
OutputStream fos = null;//用FileOutputStream 的父类来调用,OutputStream是abstract抽象类,不能new
try {
// fos = new FileOutputStream("/Users/hanzhao/Desktop/zhaohanhan.txt");
fos=new FileOutputStream("/Users/hanzhao/Desktop/daleilei.txt");
byte[] bytes="aaa找找找adfsdfsdfads的房间都是法律框架上的六块腹肌都是".getBytes();//String类里面的方法,返回byte【】
fos.write(bytes);
//fos.close();//如果前面的代码出现问题,那么fos.close();就不会被执行,所以,把这个方法写在finally,保障一定被执行
//不执行的话,会占用资源,执行一次占用一次
}catch(FileNotFoundException e) {
System.out.println("找不到文件");
}catch(IOException ex) {
System.out.println("找不到文件流");
}finally {
if(fos!=null) {
try {
fos.close();
}catch(IOException e) {
//里面可以不写,耍流氓啊啊啊啊啊啊
}
}
} //结果就是daleilei.txt被写入内容
}
}
二、InputStream read
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class StreamTest1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStream fis = null;
try {
fis = new FileInputStream("/Users/hanzhao/Desktop/xiaoshuo.txt");
//xiaoshuo.txt里面已经放好了内容,把这些内容read到console控制台里面
byte[] bytes = new byte[50];
/*int b=fis.read(bytes);//返回值int,代表每次读的字节数
System.out.println(b);//每次读50个字节
System.out.println(bytes);//[B@79698539
System.out.println(new String(bytes));
//要把read的bytes字节转换为String数据,但是这样只能read到50个字节的内容,所有我们要用while循环以下
*/
while(fis.read(bytes)>0) {//如果写成fis.read()>0,显示为一片空白
String s=new String(bytes);
System.out.print(s);
System.out.println(s);
}//read成功,但是有乱码,这是因为读取字节流,按每次50个字节读,有可能会read到半个汉字的原因。
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("文件地址不对");
// e.printStackTrace();
} catch (IOException e) {
System.out.println("read?");
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}
}
}
}
}
三、字节流,read and write
// 没有调用IOUtils中的copy方法,老老实实自己写
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class StreamTest3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
long startMS = System.currentTimeMillis();
InputStream fis = null;
OutputStream fos = null;
try {
fis = new FileInputStream("/Users/hanzhao/Desktop/zhaizhai.txt");
fos = new FileOutputStream("/Users/hanzhao/Desktop/xiaofengfeng.txt");
byte[] bytes = new byte[1204];
int len;
while ((len = fis.read(bytes)) > 0) {
fos.write(bytes, 0, len);
}
long endMS = System.currentTimeMillis();
System.out.println("copytime is" + (endMS - startMS));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println("file is not exist" + e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println();
} finally {
IOUtils.closeQuietly(fis);
IOUtils.closeQuietly(fos);
}
}
}
四、调用了IOUtils中的方法了
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class StreamTest5 {
public static void main(String[] args) {
//调用了IOUtils中的方法了
long startMS = System.currentTimeMillis();
InputStream fis = null;
OutputStream fos = null;
try {
fis = new FileInputStream("/Users/hanzhao/Desktop/zhaizhai.txt");
fos = new FileOutputStream("/Users/hanzhao/Desktop/xiaofengfeng.txt");
/*byte[] bytes = new byte[1204];
int len;
while ((len = fis.read(bytes)) > 0) {
fos.write(bytes, 0, len);
}*/
IOUtils.copy(fis, fos);
long endMS = System.currentTimeMillis();
System.out.println("copytime is" + (endMS - startMS));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println("file is not exist" + e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println();
} finally {
IOUtils.closeQuietly(fis);
IOUtils.closeQuietly(fos);
}
}
//自己写的copy方法,好搞笑写的,太×××烂啦,怎么想的,int bufferSize明明指的是1024等等数值,我怎么把它想成返回值了
//还有,公式不是万能的啊,明明只能代替下面这些,我怎么全写上了,服了,服了。。。。。。
// byte[] bytes = new byte[1204];
// int buffSize1;
// while ((buffSize1 = inStream1.read(bytes)) > 0) {
// outStream1.write(bytes, 0, buffSize1);
// }
//copy方法放在IUOtils类里面了,方便一些存放和调用
public static void copy(InputStream inStream, OutputStream outStream, int bufferSize) {
InputStream inStream1 = null;
OutputStream outStream1 = null;
try {
inStream1 = new FileInputStream("/Users/hanzhao/Desktop/zhaizhai.txt");
outStream1 = new FileOutputStream("/Users/hanzhao/Desktop/xiaofengfeng.txt");
byte[] bytes = new byte[1204];
int buffSize1;
while ((buffSize1 = inStream1.read(bytes)) > 0) {
outStream1.write(bytes, 0, buffSize1);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println("file is not exist" + e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println();
} finally {
IOUtils.closeQuietly(inStream1);
IOUtils.closeQuietly(outStream1);
}
}
}
五、IOUtils类,Utils意思为工具类,里面存放的全部是static静态方法
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
public class IOUtils {
// IOUtils utils代表工具类,里面是static类
// InputStream OutputStream Reader Writer都implements Closeable
// 所以都可以调用close();方法,体现了不同对象的多态
public static void closeQuietly(InputStream inStream) {
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void closeQuietly(OutputStream outStream) {
if (outStream != null) {
try {
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void closeQuietly(Reader reader) {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void closeQuietly(Writer writer) {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void closeQuietly(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void copy(InputStream inStream, OutputStream outStream, int bufferSize) throws IOException {
// 程序要严谨,要判断inStream,outStream如果==null,bufferSize如果小于等于0,都要抛出异常
if (inStream == null) {
throw new IllegalArgumentException("inStream不能为空");
// IllegalArgumentException extends RuntimeException 把异常抛出去 由调用着处理
}
if (outStream == null) {
throw new IllegalArgumentException("outStream不能为空");
}
if (bufferSize <= 0) {
throw new IllegalArgumentException("bufferSize不能为负数");
}
byte[] buffer = new byte[bufferSize];
int len;
while ((len = inStream.read(buffer)) > 0) {// 把异常抛出去 由调用着处理
outStream.write(buffer, 0, len);
}
}
public static void copy(InputStream inStream, OutputStream outStream) throws IOException{
copy(inStream, outStream, 1024*1024);//把异常抛出去,由调用着处理
}
}
六、自己写着实验用的IOUtils
import java.io.Closeable;
import java.io.IOException;
public class IOUtils2 implements Closeable {
public IOUtils2() {
super();
// TODO Auto-generated constructor stub
}
@Override
public void close() throws IOException {
// TODO Auto-generated method stub
}
}
七、自己写着实验用的IOUtils
import java.io.Closeable;
import java.io.IOException;
public interface IOUtils3 extends Closeable{
@Override
default void close() throws IOException {
// TODO Auto-generated method stub
}
}
总结;要敢于犯错,在错误中总结经验。
原文:https://blog.51cto.com/14394144/2410705