Java中IO流操作用处很多,也有好多类,不过常用的也就应该6,7种。
当然还是要根据需求来选择需要的类进行编写代码。特别是要弄清楚是读文件还是要写文
件,这是是很重要的。
1.读文件操作示例:
input.txt源文件内容截图:
skip(int length):跳过指定长度
read(byte[] b):将字节写入b字节数组
package JavaStream;
import java.io.FileInputStream;
import java.io.InputStream;
public class ReadFile {
public static void main(String[] args)
throws Exception{
InputStream input = new FileInputStream("input.txt");
byte[] b = new byte[70];
//input.skip(1);当时汉字的时候则结果则会出错,因为一个汉字占2个字节
input.skip(10);
input.read(b);
String s = new String(b);
System.out.println(s);
input.close();
}
}
结果截图:
2.写文件操作示例:
这边注意new FileOutputStream()里面的参数,当有true的时候表示内容不会覆盖
只会增加(这边就不截图了)
package JavaStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.print.attribute.standard.OutputDeviceAssigned;
public class WriteFile {
public static void main(String[] args)
throws Exception{
//增加的内容会覆盖掉原有内容
//OutputStream out = new FileOutputStream("input.txt");
//加了一个参数--true,则可以在其基础上增加内容,而不会覆盖
OutputStream out = new FileOutputStream("input.txt",true);
String s = "总有一天我会打败你的,大魔王";
out.write(s.getBytes());
out.close();
}
}
3.复制图片示例:
代码一:
package JavaStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class CopyImage1 {
public static void main(String[] args)
throws Exception{
//建立输入流
InputStream input = new FileInputStream("D:/bz.png");
//建立输出流
OutputStream out = new FileOutputStream("D:/Copybz.png");
//读取图片
//input.available()获取文件大小的
byte[] b = new byte[input.available()];
input.read(b);
//输出读取的内容
out.write(b);
//关闭资源
input.close();
out.close();
}
}
代码二:
当我们想用同样的方法复制一个电影怎么办呢?如果用以上方法,一次性复制过去会给
内存带来的极大的负担,而且十有八九会出现内存泄露的警告。
所以我们要循环复制,每次复制一点就可以了。
package JavaStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class CopyImage2 {
public static void main(String[] args)
throws Exception{
//分批次复制文件
//如果一次性去读文件,内存不能承受,一次性去复制一部电影,内存会溢出
//新建输入流
InputStream input = new FileInputStream("D:/bz.png");
//新建输出流
OutputStream out = new FileOutputStream("D:/Copybz3.png");
//循环读取文件,设置一次读1k
byte[] b = new byte[1024];
int count = 0;
//用while循环
while((count = input.read(b))>=0)
{
out.write(b,0,count);
}
//关闭资源
input.close();
out.close();
}
}
这边注意while循环的写法,特别是while((count = input.read(b))>=0),这句话结合
out.write(b,0,count)确保把图片所有的字节都复制过去,而且不会少更加不会多!
如果我们单纯的用while(input.read(b)>=0)的话,复制也十有八九可以成功,但是你
会发现复制后的图片比原图片大1k
4.从网站的链接上下载图片示例:
package JavaStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import org.omg.CosNaming.NamingContextExtPackage.URLStringHelper;
public class DownLoad {
public static void main(String[] args) throws Exception {
URL url = new URL(
"http://i1.sinaimg.cn/home/2013/1008/U8455P30DT20131008135420.png");
URLConnection conn = url.openConnection();
InputStream input = conn.getInputStream();
// 新建输出流
OutputStream out = new FileOutputStream("U8455P30DT20131008135420.png");
// 循环读取文件,设置一次读1k
byte[] b = new byte[1024];
int count = 0;
// 用while循环
while ((count = input.read(b)) >= 0) {
out.write(b, 0, count);
}
// 关闭资源
input.close();
out.close();
}
}