【黑马程序员】java I/O 学习总结
来源:互联网 发布:linux 卸载amazon 编辑:程序博客网 时间:2024/04/25 00:20
---------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! --------
/**
* (1)路径分隔符,一般在windows版本的java中使用正斜杠"/",如果使用反斜杠要转义"\\"*
* (2)File的renameTo(File newName)方法不仅是重命名,还会根据newName中的路径移动
* 文件位置,无法跨文件系统,目标目录下有同名文件的话可能失败.重命名后原来的File不会
* 自动更改指向的路径,要手动改 file=newName
*
* (3)获取目录中的内容 file.list() file.list(FileFielter )
* 创建目录 file.makdir() file.makdirs()
*
* (4)Jdk7之后所有打开流的I/O类都实现了AutoCloseable接口,如果用来带资源的try语句就
* 不需要再显式调用close()。
* try(FileInputStream fis=new FileInputStram(file)){
* 。。。}catrch(Exception ){. . .}
* fis的作用域仅仅在try块中。
*
*(5)文件输入流:FileInputStream fis=new FileInputStream(File或Str_path)
*读取文件:Byte b=fis.read()
* Byte[] b=new Byte; fis.read(byte)或fis.read(byte[],int offset,int numBytes)
* (offset:读取后存放入数组的起始点,numBytes:读取长度)
*
*(6)文件输出流:FileOutputStream fos=new FileOutputStream(File或Str_path)
*写入文件: fos.write(byte) fos.write(byte[]) fos.write(byte[],int offset,int numBytes)
*
*(7)ByteArrayInputStream和ByteArrayOutputStream:实现了mark(int) 和 reset(),
*可以读取同一个输入多次.close()方法无效,也不会报异常
*
*(8)缓存I/O:BufferInputStream类:支持mark和reset。可以封装任何InputStream
*构造函数BufferInputStream(InputStream) ;BufferInpurStream(InputStream,int size)
*
*(9)DataInputStream,DataOutputStream:可以向流中写入基本数据类型。
*对同一文件的读写顺序要相同
*
*(10)RandomAccessFile,构造函数参数为(File,String access)或(String filename,String access)
*access是文件访问类型,“r”只读;“rw”可读写;“rws”,‘rwd’可读写,且每次修改都会被立即
*写入到物理设备;
*
*(11)字符流
*抽象类Reader:mark(),reset(),read(),read(char[]),read(char[],int offset,int numberChars)
*抽象类Writer: write(),write(char[]),write(char[],int offset,int numberChars)
* append(char)末尾追加一个字符,flush()完成输出状态,清空缓存,刷新输出缓存
*实现:FileWriter,CharArrayWriter,ButfferReader等
*基本上Byte型的I/O类 都有对应的Char型。
*/
package CollectionDemo.src;import java.io.ByteArrayInputStream;import java.io.DataInput;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileFilter;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.util.Scanner;public class JavaIO { public static void main(String[] args) {// 用路径创建File对象并读取文件相关信息File f1=new File("F:/android/Test/tnt.txt");p("File name : "+f1.getName());p("File path :"+f1.getPath());p("Abs path :"+f1.getAbsolutePath());p("file exist ? "+f1.exists());p("file is write?is read"+f1.canWrite()+f1.canRead());p("file excute :"+f1.canExecute());p("file last modified"+f1.lastModified());p("file size : "+f1.length());p("get free space :"+f1.getFreeSpace());//操作文件File newName=new File("F:/android/Test/hehe.txt");p("can reNameTo?"+f1.renameTo(newName));f1=newName;p("f1 name :"+f1.getName());String dirname="F:/android";f1=new File(dirname);if(f1.isDirectory()){ p("Directory of "+f1); String s[]=f1.list(); for(String ss:s){File f2=new File(dirname+"/"+ss);if(f2.isDirectory()){ p(ss+" is a direct");}else{ p(ss+" is a file");} }}else{ p(dirname + " is not direct");}String dir1="F:/android/Test/T_T";f1=new File(dir1);f1.mkdir();File file1=new File("F:/android/Test/T_T/abc.txt");p("file exist :"+file1.exists());if(file1.exists()){ try(FileInputStream fis=new FileInputStream(file1)){ int num=fis.available(); p("can read Byte : "+num); byte[] input=new byte[num]; char[] ch=new char[num]; String[] str2=new String[num]; int r1=26; //读取文件 fis.read(input,0,num); for(int i=0;i<input.length;i++){ str2[i]=Byte.toString( input[i]); System.out.print(str2[i]+" "); } p("");// fis.skip(5);// p("mark surpose: "+fis.markSupported());// for(int i=0;i<input.length;i++){ // System.out.print((char)fis.read()+" ");// } //尝试一年SCanner读取输入流失败 // Scanner scanner=new Scanner(fis);// int i=0; // while(scanner.hasNextByte()){// ch[i]=(char) scanner.nextByte();// System.out.print(ch[i]+",");// i++;// } String file1path="F:/android/Test/file1.txt"; String file2path="F:/android/Test/file2"; String file3path="F:/android/Test/file3"; FileOutputStream fos1=new FileOutputStream(file1path); FileOutputStream fos2=new FileOutputStream(file2path); FileOutputStream fos3=new FileOutputStream(file3path); String source="Now is the time for all good men\n"+ "to come to the aid of their county"+ "and pau their due taxes."; byte buf[]=source.getBytes(); //3种方式写入文件 for(int i=0;i<source.length();i++){ fos1.write(buf[i]); } fos2.write(buf); fos3.write(buf,buf.length/4,buf.length-buf.length/4); }catch (IOException e) { e.printStackTrace();// TODO: handle exception } String temp="abc"; byte b[]=temp.getBytes(); ByteArrayInputStream bais=new ByteArrayInputStream(b); for(int i=0;i<2;i++){ int c; while((c=bais.read())!=-1){ switch (i) {case 0: System.out.print((char)c); break;case 1: System.out.print((char)(Character.toUpperCase(c)));default: break;} } p(""); bais.reset(); } try(DataOutputStream dout=new DataOutputStream(new FileOutputStream( "F:/android/Test/dataOutput.txt"))){ dout.writeDouble(98.6); dout.writeInt(1000); dout.writeBoolean(true); dout.writeChar('x'); }catch (IOException e) { e.printStackTrace();// TODO: handle exception} try(DataInputStream din=new DataInputStream(new FileInputStream( "F:/android/Test/dataOutput.txt"))){ p(""+din.readDouble()); p(""+din.readInt()); p(""+din.readBoolean()); p(""+din.readChar()); }catch(IOException e){ e.printStackTrace(); } try(FileReader fr=new FileReader("F:/android/Test/file1.txt")){ char c; while((c=(char) fr.read())!=-1){ System.out.print(c); } p(""); }catch (IOException e) { e.printStackTrace();// TODO: handle exception} } } public static void p(String str){System.out.println(str); }}class Onlytxt implements FileFilter{ private String txt; public Onlytxt(String txt) {// TODO 自动生成的构造函数存根this.txt="."+txt; } @Override public boolean accept(File pathname) {// TODO 自动生成的方法存根return true; } }
//对象串行化与反串行化
<pre name="code" class="java">package CollectionDemo.src;import java.io.*;public class JavaIO2 { public static void p(String str){System.out.println(str); } public static void main(String[] args) {// TODO 自动生成的方法存根try(ObjectOutputStream objos=new ObjectOutputStream(new FileOutputStream("F:/android/Test/serial"))){ MyClass object1=new MyClass("hello",-7,2.7e10); p("Object1 : "+object1); objos.writeObject(object1); }catch (IOException e) { e.printStackTrace();// TODO: handle exception}try(ObjectInputStream objis=new ObjectInputStream(new FileInputStream("F:/android/Test/serial"))){ MyClass object2=(MyClass)objis.readObject(); p("Object2 : "+object2.s+" " +object2.i+" " +object2.d);}catch (IOException e) { e.printStackTrace();// TODO: handle exception}catch(ClassNotFoundException e){ e.printStackTrace();} }}class MyClass implements Serializable{ private static final long serialVersionUID = 7637931522066060949L; public String s; public int i; public double d; public MyClass(String s,int i,double d){this.s=s;this.i=i;this.d=d; }}
--------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a><ahref="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ---------
0 0
- 【黑马程序员】java I/O 学习总结
- 黑马程序员,Java I/O流学习
- 黑马程序员-java i/o
- 黑马程序员-java-I/O
- 黑马程序员java基础篇----I/O总结(上)
- 黑马程序员java基础篇----I/O总结(下)
- java i/o黑马程序员学习笔记(2)
- java i/o黑马程序员学习笔记(1)
- 黑马程序员-I/O流的学习
- 黑马程序员-java I/O篇
- 黑马程序员java I/O系统1
- 黑马程序员Java I/O系统2
- 【黑马程序员】java I/O流浅谈
- 黑马程序员:java I/O流
- 黑马程序员 Java基础 I/O技术
- 黑马程序员-----JAVA I/O流
- 黑马程序员-Java I/O体系
- 黑马程序员--Java I/O (数据操作)
- C++命名规则
- 嵌套的异步回调
- IE浏览器版本区别
- Hadoop报错:NoRouteToHostException: No route to host
- 张晔:面向统计研究的R编程
- 【黑马程序员】java I/O 学习总结
- 在MAC上安装homebrew
- Java关键字static、final使用小结
- 哼一首歌,等日落
- SELECT …INTO
- ActionBar详解
- recv返回值----任意长度的可读的数据
- CSS A link hover active visited伪类超链接锚文本样式应用用法案例
- 文档模式(doctype)