Java学习--day9-parttwo

Java IO
9、BufferedReader类
用于从缓冲区中读取数据,所有的输入字节数据都将放在缓冲区中。
BufferedReader只能接收字符输入流的实例。所以必须将字节输入流System.in通过InputStreamReader转换为字符流。
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
异常..
String str = buf.readLine(); //很方便,没有了接收数组的长度限制,也能正确的接收中文。
System.out.println(str);

10、Scanner类
专门的输入数据类,此类不只可以完成输入数据操作,还可以方便地对输入数据进行验证。Scanner类提供了一个可以接收 InputStream类型的构造方法,只要是字节输入流的子类都可以为 Scanner类实例化进行读取。
①字符串的输入
Scanner scan = new Scanner(Syetem.in);
scan.useDelimiter("\n"); //将分割符设置为换行符,默认是空格
String str = scan.next();
System.out.print(str);
②int,float型的数据输入
if(scan.hasNextInt()){
int i=scan.nextInt();
}
if(scan.hasNextFloat()){
int i=scan.nextFloat();
}
③日期格式数据输入
Date date = null;
if(scan.hasNext("^\\d{4}-\\d{2}-\\d{2}$")){
String str = scan.next("^\\d{4}-\\d{2}-\\d{2}$");
}
try{
date=new SimpleDateFormat("yyyy-MM-dd").parse(str)
}catch(ParseException e){
e.printStackTrace();
}
System.out.print(date);
④读取文件信息
File f = new File("d:"+File.pathSeparator()+"text.txt");
Scanner scan =null;
try{
scan = new Scanner(f);
}catch(...){...}
StringBuffer str = new StringBuffer();
while(scan.hasNext()){
str.append(scan.next().append("\n"))
}
System.out.print(str);

11、数据操作流
IO包中提供了两个与平台无关的数据操作流:DataOutputStream和DataInputStream。通常数据输出流会按照一定格式将数据输出,再通过数据输入流按照一定格式读入。
写入数据:
DataOutputStream dos = new DataOutputStream(new FileOutputStream(new File("d:"+File.pathSeparator()+"text.txt")));
String names[]={"衬衣","手套","围巾"};
float prices[]={98.3f,30.3f,50.5f};
int nums[]={3,2,1};
for(int i =0;i<names.length;i++){
dos.writeChars(names[i]);
dos.writeChar('\t');
dos.writeFloat(prices[i]);
dos.writeChar('\t');
dos.writeInt(nums[i]);
dos.writeChar('\n');
}
读取数据:
DataInputStream dos = new DataInputStream(new FileInputStream(new File("d:"+File.pathSeparator()+"text.txt")));
String name = null;
float price =0.0f;
int nums = 0;
char c =0;
char temp[] =null;
try{
while(ture){
temp = new char[200];
len = 0;
while((c=dis.readChar())!='\t'){ //读取name
temp[len]=c;
len++;
}
name = new String(temp,0,len);
price = dis.readFloat();
dis.readChar();
num = dis.readInt();
dis.readChar();
System.out.println("名称: %s; 价格: %5.2f; 数量: %d\n", name,price,num);
}catch(Exception e){
}
}dis.close();

12、合并流
将两个文件的内容合并成一个文件:SequenceInputStream类
InputStream is1 = new FIleInputStream(new File("d:"+File.separator+"a.txt"));
InputStream is2 = new FIleInputStream(new File("d:"+File.separator+"b.txt"));
OutputStream os = new FileOutputStream(new File("d:"+File.separator+"ab.txt"));
SequenceInputStream sis = new SequenceInputStream(is1,is2);
int temp = 0;
while((temp=sis.read())!=-1){
os.write(temp);
}
is1.close();
is2.close();
sis.close();
os.close();

13、压缩流
为了减少传输时的数据量,提供压缩流将文件或文件夹压缩成ZIP、JAR、GZIP等形式。
每一个压缩文件中都有多个文件,每一个子文件都使用ZipEntry表示。
①ZipOutputStream类
压缩文件————————
File file = new File("d:"+File.pathSeparator()+"text.txt"); //定义要压缩的文件
File Zipfile = new File("d:"+File.pathSeparator()+"text.zip"); //定义压缩后文件的名称
InputStream input = new FileInputStream (file); //定义输入文件流
ZipOutputStream zipout = new ZipOutputStream(new FileOutputStream(zipfile));
zipout.putNextEntry(new ZipEntry(file.getName())); //设置每一个ZipEntry对象,并设置名称
zipout.setComment("www"); //设置注释
int temp = 0;
while((temp=input.read())!=-1){
zipout.write(temp);
}
input.close();
zipout.close();

压缩文件夹————————
...
if(file.isDirectory()){
File lists[] = file.listFiles(); //列出全部文件
for(int i =0; i< lists.length; i++){
input = new FileInputStream(lists[i]);
zipout.putNextEntry(new ZipEntry(file.getName()+File.separator+lists[i].getName()));
int temp = 0;
while((temp=input.read())!=-1){
zipout.write(temp);
}
input.close();
}
}
zip.close();
②ZipFile类
每一个压缩文件都可以使用ZipFile表示。此中解压缩方式只适用压缩文件中只有一个文件的情况。
File file = new File("d:"+File.pathSeparator()+"text.zip"); //找到压缩文件
File outpuFile= new File("d:"+File.Separator()+"unzip.txt"); //定义解压缩后文件的名称
ZipFile zipFile = new ZipFile(file); //实例化ZipFile对象
ZipEntry entry = ZipFile.getEntry("a.txt"); //得到一个压缩实体
InputStream input = zipFile.getInputStream (entry); //将压缩实体传入输入流
OutputStream out = new FileOutputStream(outputFile); //实例化输出流对象
int temp =0;
while((temp=input.read())!=-1){
zipout.write(temp);
}
input.close();
out.close();
③ZipInputStream类 ZipInputStream
可以方便地解压缩,不管压缩文件中有多少文件。
File file = new File("d:"+File.Separator()+"textdir.zip"); //找到压缩文件
File outFile = null; //定义输出的文件对象
ZipFile zipFile = new ZipFile(file); //实例化ZipFile对象
ZipInputStream zipInput = new ZipInputStream (new FileInputStream(file));
ZipEntry entry = null; //定义一个ZipEntry对象,用来接收压缩文件中的每一个压缩实体
InputStream input = null; //定义输入流,用来读取每一个ZipEntry
OutputStream out =null; //定义输出流,用来输出每一个ZipEntry
while((entry = zipInput.getNextEntry())!=null){
outFile = new File("d:"+File.Separator()+entry.getName());
if(!outFile.getParentFile().exists()){
outFile.getParentFile().mkdir();
}
if(!outFile..exists()){
outFile.createNewFile();
}
input = zipFile.getInputStream(entry); //得到压缩实体的输入流
out = new FileOutputStream(outFile);
int temp =0;
while((temp=input.read())!=-1){
out.write(temp);
}
input.close();
out.close();
}

14、回退流
Java提供一种回退输入流(PushbackInputstream、PushbackReader),可以把读取进来的数据重新退回到输入流的缓冲区中。
ByteArrayInputStream bai = new ByteArrayInputStream ("www.baidu.com".getBytes()); //实例化内存输入流
PushbackInputstream push = new PushbackInputstream(bai);
int temp =0;
while((temp=push.read())!=-1){
if(temp=='.'){
push.unread(temp);
temp = push.read();
System.out.print(" (退回" + (char)temp+")")
}else{
System.out.print((char)temp);
}
}

15、字符编码
①ISO8859-1:属于单字节编码,0~255,主要在英文上应用。
②GBK/GB2312:中文的国标编码,专门用来表示汉字,双字节编码。GBK可以表示简繁体,GB2312只能表示简体,GBK兼容GB2312。
③unicode:Java中使用此编码,是最标准的一种编码,使用十六进制表示编码,不兼容ISO8859-1。
④UTF:UTF编码兼容ISO8859-1,同时可以用来表示所有的语言字符。不过UTF编码是不定长编码,每一个字符长度为1~6字节不等。一般在中文网页中使用此编码,可以节省空间。
如果程序的编码和本地的默认编码保持不一致就会产生乱码。
System.getProperty("file.encoding"); //用来获取本地的编码方式

16、对象序列化
对象序列化就是把一个对象变为二进制的数据的一种方法。可以方便地实现对象的传输或者存储。但这个类必须实现Serializable接口。(对象序列化只是属性被序列化,因为每个对象方法都是相同的。)
①对象输出流 ObjectOutputStream
File file = new File("d:"+File.Separator()+"test.txt");
OutputStream out = new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(new Person("张三",30));
oos.close();
②对象输入流 ObjectInputStream
File file = new File("d:"+File.Separator()+"test.txt");
InputStream input = new FileInputStream(f);
ObjectInputStream ois = new ObjectInputStream(input);
Object obj = ois.readObject();
ois.close();
System.out.print(obj);
//姓名:张三;年龄:30
③Externalizable接口
如果用户希望自己制定序列化的内容,那这个类就需要实现Externalizable接口。Externalizable是Serializable的子接口,所以类需要覆写其中定义的两个方法:writeExternal()指定要保存的属性信息,对象序列化的时候用。readExternal()读取被保存的信息,反序列话的时候调用。
Externalizable相较于Serializable而言,实现复杂由开发人员自己完成,但可以自己决定哪个对象保存可以呢会造成速度的提升。由于是部分存储,空间也可能会减少。
④transient关键字
在实现Serializable进行对象序列化时,如果某个属性不希望被序列化就可以使用transient关键字申明。
⑤序列化一组对象
如果想要序列化多个对象,可以使用对象数组进行操作。使用Object数组接收。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值