Heim >Java >javaLernprogramm >Java-Grundlagen – iO (3)
一、管道流
演示:PipedInputStream , PipedOutputStream
注意:管道流本身就不建议在一个线程中使用,这是因为向输出流中写的数据,都会存到输入流内部的一个1024字节大小的数组中,如果写的内容超过这个数组的大小,而且没有被输入流读取的话,输出流所在的线程就会等待,如果这时是在同一个线程中,该线程就会死锁,不推荐在同一个线程中使用。(API)
<span style="color: #0000ff">import</span><span style="color: #000000"> java.io.IOException; </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.io.PipedInputStream; </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.io.PipedOutputStream; </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span><span style="color: #000000"> Test17 { </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) <span style="color: #0000ff">throws</span><span style="color: #000000"> IOException, InterruptedException { Sender sender </span>= <span style="color: #0000ff">new</span><span style="color: #000000"> Sender(); Receiver receiver </span>= <span style="color: #0000ff">new</span><span style="color: #000000"> Receiver(); PipedOutputStream out </span>=<span style="color: #000000"> sender.getOut(); PipedInputStream in </span>=<span style="color: #000000"> receiver.getIn(); in.connect(out); </span><span style="color: #008000">//</span><span style="color: #008000"> out.connect(in); </span><span style="color: #008000">//</span><span style="color: #008000">这样也可以</span> <span style="color: #000000"> sender.start(); Thread.sleep(</span>10<span style="color: #000000">); receiver.start(); } } </span><span style="color: #008000">//</span><span style="color: #008000"> 发送者</span> <span style="color: #0000ff">class</span> Sender <span style="color: #0000ff">extends</span><span style="color: #000000"> Thread { </span><span style="color: #0000ff">private</span> PipedOutputStream out = <span style="color: #0000ff">new</span><span style="color: #000000"> PipedOutputStream(); </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> run() { </span><span style="color: #0000ff">try</span><span style="color: #000000"> { </span><span style="color: #0000ff">int</span> i = 1<span style="color: #000000">; </span><span style="color: #0000ff">while</span> (<span style="color: #0000ff">true</span><span style="color: #000000">) { Thread.sleep(</span>2000<span style="color: #000000">); String msg </span>= "你好,这是发送端发的第" + i++ + "条信息"<span style="color: #000000">; out.write(msg.getBytes()); } } </span><span style="color: #0000ff">catch</span><span style="color: #000000"> (Exception ex) { ex.printStackTrace(); } } </span><span style="color: #0000ff">public</span><span style="color: #000000"> PipedOutputStream getOut() { </span><span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span><span style="color: #000000">.out; } } </span><span style="color: #008000">//</span><span style="color: #008000"> 接收者</span> <span style="color: #0000ff">class</span> Receiver <span style="color: #0000ff">extends</span><span style="color: #000000"> Thread { </span><span style="color: #0000ff">private</span> PipedInputStream in = <span style="color: #0000ff">new</span><span style="color: #000000"> PipedInputStream(); </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> run() { </span><span style="color: #0000ff">try</span><span style="color: #000000"> { </span><span style="color: #0000ff">while</span> (<span style="color: #0000ff">true</span><span style="color: #000000">) { </span><span style="color: #0000ff">byte</span>[] buff = <span style="color: #0000ff">new</span> <span style="color: #0000ff">byte</span>[1024<span style="color: #000000">]; </span><span style="color: #0000ff">int</span> len =<span style="color: #000000"> in.read(buff); String str </span>= <span style="color: #0000ff">new</span> String(buff, 0<span style="color: #000000">, len); System.out.println(str); } } </span><span style="color: #0000ff">catch</span><span style="color: #000000"> (Exception ex) { ex.printStackTrace(); } } </span><span style="color: #0000ff">public</span><span style="color: #000000"> PipedInputStream getIn() { </span><span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span><span style="color: #000000">.in; } } </span><span style="color: #008000">//</span><span style="color: #008000"> Pipe closed 发生在管道关了,发送者还在发的情况下</span>
二、文件切割和合并
<span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> merget2() <span style="color: #0000ff">throws</span><span style="color: #000000"> Exception { OutputStream out</span>=<span style="color: #0000ff">new</span> FileOutputStream("c:/src_new.zip"<span style="color: #000000">); BufferedOutputStream bos</span>=<span style="color: #0000ff">new</span><span style="color: #000000"> BufferedOutputStream(out); </span><span style="color: #0000ff">for</span>(<span style="color: #0000ff">int</span> i=1;i<=20;i++<span style="color: #000000">){ InputStream in</span>=<span style="color: #0000ff">new</span> FileInputStream("c:/src_"+<span style="color: #000000">i); </span><span style="color: #0000ff">byte</span> [] buff=<span style="color: #0000ff">new</span> <span style="color: #0000ff">byte</span><span style="color: #000000">[in.available()]; in.read(buff); bos.write(buff); in.close(); } bos.close(); System.out.println(</span>"合并成功"<span style="color: #000000">); } </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> merge()<span style="color: #0000ff">throws</span><span style="color: #000000"> Exception{ OutputStream out</span>=<span style="color: #0000ff">new</span> FileOutputStream("c:/src_new.zip"<span style="color: #000000">); </span><span style="color: #0000ff">for</span>(<span style="color: #0000ff">int</span> i=1;i<=20;i++<span style="color: #000000">){ InputStream in</span>=<span style="color: #0000ff">new</span> FileInputStream("c:/src_"+<span style="color: #000000">i); </span><span style="color: #0000ff">byte</span> [] buff=<span style="color: #0000ff">new</span> <span style="color: #0000ff">byte</span><span style="color: #000000">[in.available()]; in.read(buff); out.write(buff); in.close(); } out.close(); System.out.println(</span>"合并成功"<span style="color: #000000">); }</span>
四、File 类概述
流只能操作数据
File 类 是IO包中,唯一代表磁盘文件本身的类
File 定义了一些与平台无关的方法来操作文件
File: 文件和目录路径名的抽象表示形式。//路径也是一种文件
用来将文件或文件夹封装成对象
方便对文件及文件夹的属性信息进行操作
File对象.经常做为参数传给流的构造函数
File类不能访问文件内容,即不能从文件中读数据,也不能写入数据,它只能对文件本身的属性进行操作
五、File 类的常见操作
boolean createNewFile() //创建一个新文件,成功返回true ,否则返回false ,如果文件已经存在,则返回false,和流不一样,流是覆盖
mikdir() mikdirs //创建目录,后者可以创建多级目录
boolean delete () //删除 删除此抽象路径名表示的文件或目录。如果此路径名表示一个目录,则该目录必须为空才能删除。 否则返回false
void deleteOnExit() //在虚拟机终止时,删除文件或目录 注意它的返回类型是void 因为虚拟机都终止了,当然要返回值也没用了
exists() // 测试此抽象路径名表示的文件或目录是否存在。
isDirectory() // 判断是否是目录
isFile() //判断是否是文件
isHidden() //判断是否是隐藏文件
<span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> fileDemo() <span style="color: #0000ff">throws</span><span style="color: #000000"> IOException{ File f1</span>=<span style="color: #0000ff">new</span> File("1.txt"); <span style="color: #008000">//</span><span style="color: #008000">当前路径下</span> File f2=<span style="color: #0000ff">new</span> File("c:\\2.txt"<span style="color: #000000">); File f3</span>=<span style="color: #0000ff">new</span> File("c:\\","3.txt"<span style="color: #000000">); File f4</span>=<span style="color: #0000ff">new</span> File("c:\\"<span style="color: #000000">); File f5</span>=<span style="color: #0000ff">new</span> File(f4,"5.txt"); <span style="color: #008000">//</span><span style="color: #008000">File(File parent,String child)</span> <span style="color: #008000">/*</span><span style="color: #008000">f1.createNewFile(); f2.createNewFile(); f3.createNewFile(); System.out.println(f4.createNewFile()); //返回false f5.createNewFile(); </span><span style="color: #008000">*/</span> <span style="color: #008000">/*</span><span style="color: #008000"> System.out.println(f1.delete()); //true System.out.println(f2.delete()); //true System.out.println(f3.delete()); //true System.out.println(f4.delete()); //false System.out.println(f5.delete()); //true </span><span style="color: #008000">*/</span> <span style="color: #008000">//</span><span style="color: #008000">System.out.println( new File("c:/aaa").delete()); </span><span style="color: #008000">//</span><span style="color: #008000">(aaa是个文件夹)如果aaa下有内容,则返回false,否则返回true,并将文件夹删除</span> <span style="color: #008000">/*</span><span style="color: #008000"> File f=new File("c:\\BBBSSS\\1.txt\\cc\\dd"); //这样写,1.txt也会被做为文件夹的名称 f.mkdirs(); //创建多级的时候,用mkdirs(); new File("c:/forlder1").mkdir(); //只能创建一级目录 </span><span style="color: #008000">*/</span> <span style="color: #008000">/*</span><span style="color: #008000">File ff=new File("c:\\不存在的文件.txt"); System.out.println(ff.isDirectory()); //false System.out.println(ff.isFile()); //false System.out.println(ff.isHidden()); //false </span><span style="color: #008000">*/</span> <span style="color: #008000">/*</span><span style="color: #008000"> File ff=new File("c:\\不存在的文件.txt"); ff.createNewFile(); System.out.println(ff.isDirectory()); //false System.out.println(ff.isFile()); //true System.out.println(ff.isHidden()); //false </span><span style="color: #008000">*/</span><span style="color: #000000"> File ff</span>=<span style="color: #0000ff">new</span> File("c:\\不存在的文件.txt"<span style="color: #000000">); ff.mkdir(); System.out.println(ff.isDirectory()); </span><span style="color: #008000">//</span><span style="color: #008000">true</span> System.out.println(ff.isFile()); <span style="color: #008000">//</span><span style="color: #008000">false</span> System.out.println(ff.isHidden()); <span style="color: #008000">//</span><span style="color: #008000">false </span><span style="color: #008000">//</span><span style="color: #008000">注意,对于还没有创建的文件,或文件夹,isDirectory,isFile 都会返回false 要想得到正确结果,必须先用 exists判断 </span> <span style="color: #000000"> }</span>
六、File 类获取文件信息类操作
String getName(); //返回文件或目录的名称
String getParent(); //返回父目录的名字,没有则返回null
String getPath(); //返回路径字串 ??? 含文件名
String getAbsolutePath(); // 返回绝对路径名字符串
//File getAbsoluteFile(); //返回绝对路径名形式。等同于 new File(this.getAbsolutePath())
long length(); //返回文件长度,以字节为单位
static File[] listRoots() //列出可用的文件系统根 在windows下即列出C D E等盘符
String [] list() //返回目录中的文件和目录 (即连文件名一起返回)(隐藏文件也会被返回)
String [] list(FilenameFilter filter) //返回经过指定过滤器过滤的文件和目录。
File [] listFiles() //返回当前目录下所有的文件
File[] listFiles(FilenameFilter filter)
<span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span><span style="color: #000000"> fileInfoDemo(){ File f</span>=<span style="color: #0000ff">new</span> File("c:\\Test\\1.txt"<span style="color: #000000">); System.out.println(</span>"getName--"+f.getName()); <span style="color: #008000">//</span><span style="color: #008000">1.txt</span> System.out.println("getParent()--"+f.getParent()); <span style="color: #008000">//</span><span style="color: #008000">c:\Test</span> System.out.println("getPath()--"+f.getPath()); <span style="color: #008000">//</span><span style="color: #008000">c:\Test\1.txt</span> System.out.println("getAbsolutePath--"+f.getAbsolutePath()); <span style="color: #008000">//</span><span style="color: #008000">c:\Test\1.txt</span> System.out.println("length()--"+f.length()); <span style="color: #008000">//</span><span style="color: #008000">1234</span> <span style="color: #000000"> File[] fileList</span>=<span style="color: #000000">File.listRoots(); </span><span style="color: #0000ff">for</span><span style="color: #000000">(File file : fileList) { System.out.println (file); </span><span style="color: #008000">//</span><span style="color: #008000">打印出 C:\ D:\ 等</span> <span style="color: #000000"> } } </span>
如果把上面的文件路径换成 File f=<span style="color: #0000ff">new</span> File("1.txt"); <span style="color: #008000">//</span><span style="color: #008000">使用的是相对路径,则结果为:</span> getName--1<span style="color: #000000">.txt getParent()</span>--<span style="color: #0000ff">null</span><span style="color: #000000"> getPath()</span>--1<span style="color: #000000">.txt getAbsolutePath</span>--C:\workspace\Lession21\1<span style="color: #000000">.txt length()</span>--0<span style="color: #000000"> A:\ C:\ D:\ E:\</span>
<span style="color: #008000">//</span><span style="color: #008000">例 显示全部</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span><span style="color: #000000"> listDemo(){ File f</span>=<span style="color: #0000ff">new</span> File("C:/文件夹A/大学毕业论文收集"<span style="color: #000000">); String [] nameList</span>=<span style="color: #000000">f.list(); </span><span style="color: #0000ff">for</span><span style="color: #000000">(String str:nameList){ System.out.println(str); } }</span>
<span style="color: #008000">//</span><span style="color: #008000">例子,带过滤的</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span><span style="color: #000000"> filterDemo(){ File f</span>=<span style="color: #0000ff">new</span> File("C:/文件夹A/大学毕业论文收集/中文系"<span style="color: #000000">); File [] fileList</span>=f.listFiles(<span style="color: #0000ff">new</span><span style="color: #000000"> FilenameFilter() { </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span><span style="color: #000000"> accept(File dir, String name) { </span><span style="color: #0000ff">return</span> !name.endsWith(".exe"); <span style="color: #008000">//</span><span style="color: #008000">只看exe文件</span> <span style="color: #000000"> } }); </span><span style="color: #0000ff">for</span><span style="color: #000000"> (File item: fileList) { System.out.println(item.getName()); } }</span>
七、递归操作
递归的查看目录中的内容
<span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span><span style="color: #000000"> main(String[] args) { recuDir(</span><span style="color: #0000ff">new</span> File("C:\\作业\\8.13"<span style="color: #000000">)); } </span><span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span><span style="color: #000000"> recuDir(File dir){ System.out.println(</span>"------------------"<span style="color: #000000">); File [] fileList</span>=<span style="color: #000000"> dir.listFiles(); </span><span style="color: #0000ff">for</span><span style="color: #000000">(File f:fileList){ </span><span style="color: #0000ff">if</span><span style="color: #000000">(f.isDirectory()){ recuDir(f); </span><span style="color: #008000">//</span><span style="color: #008000">递归调用</span> }<span style="color: #0000ff">else</span><span style="color: #000000">{ System.out.println(f.getName()); } } }</span>
八、RandomAccessFile
java 语言中功能最丰富的文件访问类
支持 "随机访问" 方式
可以跳转到文件的任意位置读写数据
该类对象有个指示器,指向当前读写的位置,当读写n个字节后,文件指示器将指向这n个字节的下一个字节处
刚打开文件的时候,指示器指向指向文件开头,可以移动指示器到新的位置
在等长记录格式文件的随机读取时有极大优势,但它只限于操作文件,不能访问其他io设备,如网络,内存映像等
RandomAccessFile的构造函数
new RandomAccessFile(f,"rw"); //读写方式 (如果文件不存在,会创建)
new RandomAccessFile(f,"r"); //只读方式
<span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) <span style="color: #0000ff">throws</span><span style="color: #000000"> IOException { </span><span style="color: #0000ff">final</span> <span style="color: #0000ff">int</span> LEN=8<span style="color: #000000">; </span><span style="color: #008000">/*</span><span style="color: #008000">Student stu1=new Student(20,"cat"); Student stu2=new Student(21,"sheep"); Student stu3=new Student(15,"duck"); RandomAccessFile r=new RandomAccessFile("c:/stu.txt","rw"); r.write(stu1.name.getBytes()); r.writeInt(stu1.age); r.write(stu2.name.getBytes()); r.writeInt(stu2.age); r.write(stu3.name.getBytes()); r.writeInt(stu3.age); r.close();</span><span style="color: #008000">*/</span><span style="color: #000000"> RandomAccessFile read</span>=<span style="color: #0000ff">new</span> RandomAccessFile("c:/stu.txt","rw"<span style="color: #000000">); read.skipBytes(</span>12); <span style="color: #008000">//</span><span style="color: #008000">跳过第一个学生的信息,其中年龄是4个字节,姓名是8个字节</span> System.out.println("第二个学生的信息:"<span style="color: #000000">); String str</span>=""<span style="color: #000000">; </span><span style="color: #0000ff">for</span>(<span style="color: #0000ff">int</span> i=0;i<LEN;i++<span style="color: #000000">){ str</span>+=(<span style="color: #0000ff">char</span><span style="color: #000000">)read.readByte(); } System.out.println(</span>"name:"+<span style="color: #000000">str); System.out.println(</span>"age:"+<span style="color: #000000">read.readInt()); System.out.println(</span>"第一个学生的信息:"<span style="color: #000000">); read.seek(</span>0<span style="color: #000000">); str</span>=""<span style="color: #000000">; </span><span style="color: #0000ff">for</span>(<span style="color: #0000ff">int</span> i=0;i<LEN;i++<span style="color: #000000">){ str</span>+=(<span style="color: #0000ff">char</span><span style="color: #000000">)read.readByte(); } System.out.println(</span>"name:"+<span style="color: #000000">str); System.out.println(</span>"age:"+<span style="color: #000000">read.readInt()); System.out.println(</span>"第三个学生的信息:"<span style="color: #000000">); read.skipBytes(</span>12<span style="color: #000000">); str</span>=""<span style="color: #000000">; </span><span style="color: #0000ff">for</span>(<span style="color: #0000ff">int</span> i=0;i<LEN;i++<span style="color: #000000">){ str</span>+=(<span style="color: #0000ff">char</span><span style="color: #000000">)read.readByte(); } System.out.println(</span>"name:"+<span style="color: #000000">str); System.out.println(</span>"age:"+<span style="color: #000000">read.readInt()); read.close(); }</span>
九、Properties 类详解
Properties是 HashTable 的子类,它增加了将键和值保存到流中,或从流中读取的功能。如果要用 properties.store()方法存储其对象内容,则关键字和值必须是String型。可以从流中载入键值对信息 void load(InputStream inStream) 从输入流中读取属性列表(键和元素对)。
Set
注意:尽量不要用中文
<span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) <span style="color: #0000ff">throws</span><span style="color: #000000"> IOException {
Properties settings </span>=<span style="color: #0000ff">new</span><span style="color: #000000"> Properties();
settings.load(</span><span style="color: #0000ff">new</span> FileInputStream("c:/config.ini"<span style="color: #000000">));
System.out.println(settings.getProperty(</span>"port")); <span style="color: #008000">//</span><span style="color: #008000">8080
</span><span style="color: #008000">//</span><span style="color: #008000">从流中取</span>
Set<String> set=settings.stringPropertyNames(); <span style="color: #008000">//</span><span style="color: #008000">取所有属性</span>
<span style="color: #0000ff">for</span><span style="color: #000000">(String key:set){
System.out.println(key</span>+":"+<span style="color: #000000">settings.getProperty(key));
}
</span><span style="color: #008000">//</span><span style="color: #008000">输入</span>
settings.setProperty("niceCat", "this is a niceCat"<span style="color: #000000">);
settings.store(</span><span style="color: #0000ff">new</span> FileOutputStream("c:/config.ini"),"this is note"<span style="color: #000000">);
System.out.println(</span>"ok"<span style="color: #000000">);
}</span>
Das obige ist der detaillierte Inhalt vonJava-Grundlagen – iO (3). Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!