java之IO流
程序员文章站
2024-03-05 15:17:55
...
IO流
1、了解二进制文件的读写
2、了解重定向标准I\O
3、了解对象的序列化
4、理解file类及其使用
5、理解java流的改建及其分类
6、理解文本文件的读写
java流概念:
流的分类:
1、按照流向划分:输入流,输出流
2、按照流的基类划分:字节流,字符流
1、字节流
a、字节输入流inputstream基类
b、字节输出流outputstream基类
2、字符流
a、字符输入流reader基类
b、字符输出流writer基类
InputStream:
int read():从输入流中读取单个字节,返回所读取的字节数据
int read(byte[] b):从输入流中读取最多b.length长度的字节,并存储在字节数组b中,返回实际读取的字节数
int read(byte[] b,int off,int len):从输入流中读取最多len长度的字节,保存到字节数组b中,保存的位置从off开始
void close():关闭输入流
int available():返回可以从输入流中读取的字节数目
skip(long n):从输入流中跳过参数n指定数目的字节
mark(int readlimit):标记输入流中的当前位置,以便可以使用reset()方法复位到该标记的位置
void reset():将当前位置复位为上次调用mark()方法标记的位置
public class Inputstream {
public static void main(String[] args) {
//InputStream stream=new FileInputStream("E:\\demo\\hello.txt");
//创建了一个文件字节输入流对象
InputStream stream=null;
try {
stream=new FileInputStream("E:/demo/hello.txt");
byte b[]=new byte[1024];
int i=0;
int count=0;
while((i=stream.read())!=-1){
b[count]=(byte)i;
count++;
}
System.out.println(new String(b));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Inputstream2 {
public static void main(String[] args) {
InputStream stream=null;
try {
stream=new FileInputStream("E:/demo/hello.txt");
//int i=stream.read();
byte b[]=new byte[2];
int i=0;
StringBuffer sBuffer=new StringBuffer();
//读取以b.length为单位的字节数据到数组中
while((i=stream.read(b))!=-1){
//使用sBuffer对象存储每次读取出来的数据
//使用string对象的三个参数构造器(字节数组,数组的起始位置,长度)存储
//把数据添加sBuffer对象
sBuffer.append(new String(b,0,i));
}
System.out.println(sBuffer);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Inputstream3 {
public static void main(String[] args) {
InputStream stream=null;
try {
stream=new FileInputStream("E:/demo/hello.txt");
byte b[]=new byte[1024];
int i=0;
StringBuffer sBuffer=new StringBuffer();
int off=0;
//输入流一次读取长度为5个字节的单位的数据,放入到数组的以off小标开始的位置
while((i=stream.read(b, off, 5))!=-1){
//循环每次把数组当中的从off开始的数据xianghou读取i个单位的数据放到字符串里
//把字符串添加到sb对象
sBuffer.append(new String(b,off,i));
off+=5;
}
System.out.println(sBuffer);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Inputstream4 {
public static void main(String[] args) {
InputStream stream=null;
try {
stream=new FileInputStream("E:/demo/hello.txt");
byte b[]=new byte[1024];
int i=0;
StringBuffer sBuffer=new StringBuffer();
int off=0;
int length=0;
while((i=stream.read(b, off, 5))!=-1){
//记录上一次的小标
off+=5;
length+=i;
}
sBuffer.append(new String(b,0,length));
System.out.println(sBuffer);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Inputstream5 {
public static void main(String[] args) {
InputStream stream=null;
try {
stream=new FileInputStream("E:/demo/hello.txt");
//System.out.println(stream.markSupported());
//读取的时候一次性将文件读取到数组中
byte b[]=new byte[stream.available()];//返回文件大小
StringBuffer sBuffer=new StringBuffer();
stream.read(b);
sBuffer.append(new String(b));
System.out.println(sBuffer);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
stream.close();
} /*catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(NullPointerException e){
e.printStackTrace();
}*/
//使用多态实现错误的输出
catch(Exception e){
e.printStackTrace();
}
}
}
}
OutputStream:
void write(int c):将指定的字节输出到输出流中
void write(byte[] buf):将字节数组的值输入到输出流中
void write(byte[] b,int off,int len):将字节数组中off位置开始,长度为len的字节数据输出到输出流中
void close():关闭输出流
void flush():强制把任何被缓冲的已写的输出数据输出到输出流
public class Outputstream {
public static void main(String[] args) {
FileOutputStream fos=null;
try {
String string="锄禾日当午,汗滴禾下土";
byte[]b=string.getBytes();
//int index=0;
fos=new FileOutputStream("E:/demo/out.txt");
fos.write(b);
/*while(index<b.length){
//一次只写一个字节
fos.write(b[index++]);
}*/
System.out.println("写入成功");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
//flush是把写入到输出流的数据强行输出到输出流
fos.flush();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
BufferedInputStream和BufferedOutputStream:用于缓冲读取,较常用public class Bufferd {
public static void main(String[] args) {
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try {
bis=new BufferedInputStream(new FileInputStream("E:/demo/p.jpg"));
byte []bs=new byte[bis.available()];
bis.read(bs);
System.out.println("读取成功");
bos=new BufferedOutputStream(new FileOutputStream("E:/demo1/p.jpg"));
bos.write(bs);
bos.flush();
System.out.println("写入成功");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bis.close();
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
PrintStream:public class PrintStreamtest {
public static void main(String[] args) {
PrintStream ps=null;
try {
// ps=new PrintStream(new FileOutputStream("E:/demo/write.txt"));
ps=new PrintStream("E:\\demo\\print.txt");
//ps=new PrintStream(System.out);
ps.print("飞流直下三千尺");
ps.println("疑是银河落九天");
ps.println("举头望明月");
ps.write(new String("哈哈哈哈").getBytes());
System.out.println("写入成功");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(ps!=null){
ps.flush();
ps.close();
}
}
}
}
BufferedReader:public class BRtest2 {
//读取文件的行数
public static void main(String[] args) {
BufferedReader br=null;
try {
br=new BufferedReader(new FileReader("E:/demo/br2.txt"));
char[]c=new char[1024];
int len=0;
StringBuffer sb=new StringBuffer();
String s=null;
int count=0;
while((s=br.readLine())!=null){
sb.append(s);
count++;
}
System.out.println(sb.toString());
System.out.println(count);
br.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
BufferedWriter:public class BWtest {
public static void main(String[] args) {
BufferedWriter bw=null;
try {
bw=new BufferedWriter(new FileWriter("E:/demo/file.txt"));
//windows里换行
bw.write("test\r\ntest");
//bw.newline()可以实现换行
bw.newLine();
bw.write("haha");
bw.flush();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
字节流转字符流:public class ISRtest {
public static void main(String[] args) {
FileInputStream fis=null;
InputStreamReader isr=null;
BufferedReader br=null;
try {
fis=new FileInputStream("E:/demo/file.txt");
int len=fis.available();
isr=new InputStreamReader(fis);
br=new BufferedReader(isr);
String string=null;
StringBuffer sb=new StringBuffer();
while((string=br.readLine())!=null){
sb.append(string);
}
br.close();
System.out.println(sb.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class BWtest {
public static void main(String[] args) {
BufferedWriter bw=null;
try {
bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("E:/demo/file.txt")));
bw.write("12345");
bw.newLine();
bw.write("test");
bw.write("中华");
bw.flush();
bw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
序列化:
将对象的状态存储到特定存储介质中
1、实现serializable接口
2、创建一个对象输出流objectoutstream
3、writeobject()方法输出序列化对象
反序列化:
1、创建一个对象输入流objectinputstream
2、readobject()方法读取序列化对象
3、将数据强转存入一个新的对象
public class Goods implements Serializable{
private String name;
private double price;
private int num;
public Goods(String name, double price, int num) {
super();
this.name = name;
this.price = price;
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
public class Dingdan {
public static void main(String[] args) {
File file=new File("E:/demo/save.txt");
ArrayList<Goods> list=new ArrayList<>();
if(!file.exists()){
Scanner scanner=new Scanner(System.in);
while(true){
System.out.print("请输入商品名称:");
String name=scanner.next();
System.out.print("请输入商品价格:");
double price=scanner.nextDouble();
System.out.print("请输入数量:");
int num=scanner.nextInt();
Goods goods=new Goods(name, price,num);
list.add(goods);
System.out.println("是否结束");
if(scanner.next().equals("y")){
break;
}
}
ObjectOutputStream oos=null;
try {
oos=new ObjectOutputStream(new FileOutputStream("E:/demo/save.txt"));
oos.writeObject(list);
System.out.println("录入成功");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
oos.flush();
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else{
ObjectInputStream ois=null;
try {
ois=new ObjectInputStream(new FileInputStream("E:/demo/save.txt"));
list=(ArrayList<Goods>)ois.readObject();
double s=0;
for(Goods goods:list){
s+=goods.getPrice()*goods.getNum();
}
System.out.println("\t\t\t\t\t\t总价为:"+s);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}