File类

package thinking.in.java.chapter10;/*** File 类有一个欺骗性的名字——通常会认为它对付的是一个文件,但实情并非如此。它既代表一个特定文件* 的名字,也代表目录内一系列文件的名字。若代表一个文件集,便可用list()方法查询这个集,返回的是一* 个字串数组。之所以要返回一个数组,而非某个灵活的集合类,是因为元素的数量是固定的。而且若想得到* 一个不同的目录列表,只需创建一个不同的File 对象即可。事实上,“FilePath”(文件路径)似乎是一个* 更好的名字。本节将向大家完整地例示如何使用这个类,其中包括相关的 FilenameFilter(文件名过滤器)* 接口。*//*** 10.4.1 目录列表器* 现在假设我们想观看一个目录列表。可用两种方式列出File 对象。若在不含自变量(参数)的情况下调用* list(),会获得 File 对象包含的一个完整列表。然而,若想对这个列表进行某些限制,就需要使用一个“目* 录过滤器”,该类的作用是指出应如何选择File 对象来完成显示。*/
//: DirList.java
// Displays directory listing
import java.io.*;
public class DirList {public static void main(String[] args) {try {File path = new File(".");String[] list;if(args.length == 0)list = path.list();elselist = path.list(new DirFilter(args[0]));for(int i = 0; i < list.length; i++)System.out.println(list[i]);} catch(Exception e) {e.printStackTrace();}}
}
class DirFilter implements FilenameFilter {String afn;DirFilter(String afn) { this.afn = afn; }public boolean accept(File dir, String name) {// Strip path information:String f = new File(name).getName();return f.indexOf(afn) != -1;}
} ///:~


output result:

.git
.gitignore
.idea
.mvn
doc
mvnw
mvnw.cmd
out
pom.xml
readinglist.iml
README.en.md
README.md
src
targetProcess finished with exit code 0
package thinking.in.java.chapter10;/*** 下例用一个匿名内部类(已在第7 章讲述)来重写显得非常理想。首先创建了一个filter()方法,它返回指* 向FilenameFilter 的一个句柄:*/
//: DirList2.java
// Uses Java 1.1 anonymous inner classes
import java.io.*;
public class DirList2 {public static FilenameFilterfilter(final String afn) {// Creation of anonymous inner class:return new FilenameFilter() {String fn = afn;public boolean accept(File dir, String n) {// Strip path information:String f = new File(n).getName();return f.indexOf(fn) != -1;}}; // End of anonymous inner class}public static void main(String[] args) {try {File path = new File(".");String[] list;if(args.length == 0)list = path.list();elselist = path.list(filter(args[0]));for(int i = 0; i < list.length; i++)System.out.println(list[i]);} catch(Exception e) {e.printStackTrace();}}
} ///:~
package thinking.in.java.chapter10;//: DirList3.java
// Building the anonymous inner class "in-place"
import java.io.*;
public class DirList3 {public static void main(final String[] args) {try {File path = new File(".");String[] list;if(args.length == 0)list = path.list();elselist = path.list(new FilenameFilter() {public booleanaccept(File dir, String n) {String f = new File(n).getName();return f.indexOf(args[0]) != -1;}});for(int i = 0; i < list.length; i++)System.out.println(list[i]);} catch(Exception e) {e.printStackTrace();}}
} ///:~
package thinking.in.java.chapter10;//: SortedDirList.java
// Displays sorted directory listing
import thinking.in.java.chapter8.StrSortVector;import java.io.*;
public class SortedDirList {private File path;private String[] list;public SortedDirList(final String afn) {path = new File(".");if(afn == null)list = path.list();elselist = path.list(new FilenameFilter() {public booleanaccept(File dir, String n) {String f = new File(n).getName();return f.indexOf(afn) != -1;}});sort();}void print() {for(int i = 0; i < list.length; i++)System.out.println(list[i]);}private void sort() {StrSortVector sv = new StrSortVector();for(int i = 0; i < list.length; i++)sv.addElement(list[i]);// The first time an element is pulled from// the StrSortVector the list is sorted:for(int i = 0; i < list.length; i++)list[i] = sv.elementAt(i);}// Test it:public static void main(String[] args) {SortedDirList sd;if(args.length == 0)sd = new SortedDirList(null);elsesd = new SortedDirList(args[0]);sd.print();}
} ///:~
//这里进行了另外少许改进。不再是将path(路径)和 list(列表)创建为main()的本地变量,它们变成了
//类的成员,使它们的值能在对象“生存”期间方便地访问。事实上,main()现在只是对类进行测试的一种方
//式。大家可以看到,一旦列表创建完毕,类的构建器就会自动开始对列表进行排序。
//这种排序不要求区分大小写,所以最终不会得到一组全部单词都以大写字母开头的列表,跟着是全部以小写
//字母开头的列表。然而,我们注意到在以相同字母开头的一组文件名中,大写字母是排在前面的——这对标
//准的排序来说仍是一种不合格的行为。Java 1.2 已成功解决了这个问题。
Usage:MakeDirectories path1 ...
Creates each path
Usage:MakeDirectories -d path1 ...
Deletes each path
Usage:MakeDirectories -r path1 path2
Renames from path1 to path2Process finished with exit code 1

IO 流的典型应用

package thinking.in.java.chapter10;//: IOStreamDemo.java
// Typical IO Stream Configurations
import java.io.*;
//import com.bruceeckel.tools.*;
public class IOStreamDemo {public static void main(String[] args) {try {// 1. Buffered input fileDataInputStream in =new DataInputStream(new BufferedInputStream(new FileInputStream(args[0])));String s, s2 = new String();while((s = in.readLine())!= null)s2 += s + "\n";in.close();// 2. Input from memoryStringBufferInputStream in2 =new StringBufferInputStream(s2);int c;while((c = in2.read()) != -1)System.out.print((char)c);// 3. Formatted memory inputtry {DataInputStream in3 =new DataInputStream(new StringBufferInputStream(s2));while(true)System.out.print((char)in3.readByte());} catch(EOFException e) {System.out.println("End of stream encountered");}// 4. Line numbering & file outputtry {LineNumberInputStream li =new LineNumberInputStream(new StringBufferInputStream(s2));DataInputStream in4 =new DataInputStream(li);PrintStream out1 =new PrintStream(new BufferedOutputStream(new FileOutputStream("IODemo.out")));while((s = in4.readLine()) != null )out1.println("Line " + li.getLineNumber() + s);out1.close(); // finalize() not reliable!} catch(EOFException e) {System.out.println("End of stream encountered");}// 5. Storing & recovering datatry {DataOutputStream out2 =new DataOutputStream(new BufferedOutputStream(new FileOutputStream("Data.txt")));out2.writeBytes("Here's the value of pi: \n");out2.writeDouble(3.14159);out2.close();DataInputStream in5 =new DataInputStream(new BufferedInputStream(new FileInputStream("Data.txt")));System.out.println(in5.readLine());System.out.println(in5.readDouble());} catch(EOFException e) {System.out.println("End of stream encountered");}// 6. Reading/writing random access filesRandomAccessFile rf =new RandomAccessFile("rtest.dat", "rw");for(int i = 0; i < 10; i++)rf.writeDouble(i*1.414);rf.close();rf =new RandomAccessFile("rtest.dat", "rw");rf.seek(5*8);rf.writeDouble(47.0001);rf.close();rf =new RandomAccessFile("rtest.dat", "r");for(int i = 0; i < 10; i++)System.out.println("Value " + i + ": " +rf.readDouble());rf.close();// 7. File input shorthandInFile in6 = new InFile(args[0]);String s3 = new String();System.out.println("First line in file: " +in6.readLine());in6.close();// 8. Formatted file output shorthandPrintFile out3 = new PrintFile("Data2.txt");out3.print("Test of PrintFile");out3.close();// 9. Data file output shorthandOutFile out4 = new OutFile("Data3.txt");out4.writeBytes("Test of outDataFile\n\r");out4.writeChars("Test of outDataFile\n\r");out4.close();} catch(FileNotFoundException e) {System.out.println("File Not Found:" + args[0]);} catch(IOException e) {System.out.println("IO Exception");}}
} ///:~
package thinking.in.java.chapter10;/*** 3. 格式化内存输入* StringBufferInputStream 的接口是有限的,所以通常需要将其封装到一个DataInputStream 内,从而增强* 它的能力。然而,若选择用readByte()每次读出一个字符,那么所有值都是有效的,所以不可再用返回值来* 侦测何时结束输入。相反,可用available()方法判断有多少字符可用。下面这个例子展示了如何从文件中* 一次读出一个字符*/
//: TestEOF.java
// Testing for the end of file while reading
// a byte at a time.
import java.io.*;
public class TestEOF {public static void main(String[] args) {try {DataInputStream in =new DataInputStream(new BufferedInputStream(new FileInputStream("src/main/java/thinking/in/java/chapter10/TestEOF.java")));while(in.available() != 0)System.out.print((char)in.readByte());} catch (IOException e) {System.err.println("IOException");}}
} ///:~
package thinking.in.java.chapter10;/*** 3. ₩ᅠᄐ¥ᄐマ¥フヨ¥ニナ¥ᆳリ│ᄒモ¥ナᆬ* StringBufferInputStream ￧レト₩ホᆬ¥マᆪ₩リᆵ₩ワノ←ルミ￧レト￯ᄐフ₩ノタ¦ᄏᆬ←タレ¥ᄌᄌ←ワタ│ᆭチ¥ᄚニ¥ナᄊ¥ᄚチ│ᆪナ¥ネᄚ¦ᄌタ¦ᄌᆰDataInputStream ¥ニナ￯ᄐフ¦ᄏホ│タフ¥ᄁ゙¥ᄐᄎ* ¥ᆴテ￧レト│テᄑ¥ハロ ̄タツ￧トᄊ│タフ￯ᄐフ│ヒᆬ←タノ₩ヒᄅ￧ヤᄄreadByte()₩ᆵマ₩ᆲᄀ│ᆵᄏ¥ヌᄎ¦ᄌタ¦ᄌᆰ¥ᆳラ￧ᆲᆭ￯ᄐフ←ツᆪ¦ᄍネ₩ノタ₩ワノ¥タᄐ←テᄑ₩リᆵ₩ワノ₩ユネ￧レト￯ᄐフ₩ノタ¦ᄏᆬ¦ᄌヘ¥マᆵ¥ニヘ￧ヤᄄ│﾿ヤ¥ロ゙¥タᄐ₩ンᆬ* ¦ᄒᆭ₩ᄉヒ¦ᄑユ₩ラᄊ￧ᄏモ₩ン゚│ᄒモ¥ナᆬ ̄タツ￧ロᄌ¥マヘ￯ᄐフ¥マᆵ￧ヤᄄavailable()₩ヨᄍ₩ᄈユ¥ネᄂ₩ヨᆳ₩ワノ¥ᄂレ¥ᄚム¥ᆳラ￧ᆲᆭ¥マᆵ￧ヤᄄ ̄タツ¦ᄌヒ←ンᄁ│﾿ル¦ᄌᆰ¦ᄒヒ¥ᆳミ¥ᄆユ￧ᄂᄎ¦ᄎニ¥ᆭツ¦ᄑユ¦ᄏホ₩ヨヌ¦ᄏᄊ¦ᄌᆳ* ¦ᄌタ₩ᆲᄀ│ᆵᄏ¥ヌᄎ¦ᄌタ¦ᄌᆰ¥ᆳラ￧ᆲᆭ*/
//: TestEOF.java
// Testing for the end of file while reading
// a byte at a time.
import java.io.*;
public class TestEOF {public static void main(String[] args) {try {DataInputStream in =new DataInputStream(new BufferedInputStream(new FileInputStream("src/main/java/thinking/in/java/chapter10/TestEOF.java")));while(in.available() != 0)System.out.print((char)in.readByte());} catch (IOException e) {System.err.println("IOException");}}
} ///:~
Process finished with exit code 0
/*** 7. 快速文件输入* 若想创建一个对象,用它从一个缓冲的DataInputStream 中读取一个文件,可将这个过程封装到一个名为* InFile 的类内。如下所示:*/import java.io.*;
public class InFile extends DataInputStream {public InFile(String filename)throws FileNotFoundException {super(new BufferedInputStream(new FileInputStream(filename)));}public InFile(File file)throws FileNotFoundException {this(file.getPath());}
} ///:~
package thinking.in.java.chapter10;import java.io.*;/*** 8. 快速输出格式化文件* 亦可用同类型的方法创建一个 PrintStream,令其写入一个缓冲文件。下面是对com.bruceeckel.tools 的扩* 展:*/
public class PrintFile extends PrintStream {public PrintFile(String filename)throws IOException {super(new BufferedOutputStream(new FileOutputStream(filename)));}public PrintFile(File file)throws IOException {this(file.getPath());}
} ///:~
package thinking.in.java.chapter10;import java.io.*;/*** 9. 快速输出数据文件* 最后,利用类似的快捷方式可创建一个缓冲输出文件,用它保存数据(与由人观看的数据格式相反):*/
public class OutFile extends DataOutputStream {public OutFile(String filename)throws IOException {super(new BufferedOutputStream(new FileOutputStream(filename)));}public OutFile(File file)throws IOException {this(file.getPath());}
} ///:~
package thinking.in.java.chapter10;//: Echo.java
// How to read from standard input
import java.io.*;/*** 10.5.4 从标准输入中读取数据* 以Unix 首先倡导的“标准输入”、“标准输出”以及“标准错误输出”概念为基础,Java 提供了相应的* System.in,System.out 以及System.err。贯这一整本书,大家都会接触到如何用 System.out 进行标准输* 出,它已预封装成一个 PrintStream 对象。System.err 同样是一个 PrintStream,但System.in 是一个原始* 的InputStream,未进行任何封装处理。这意味着尽管能直接使用 System.out 和System.err,但必须事先封* 装System.in,否则不能从中读取数据。* 典型情况下,我们希望用readLine()每次读取一行输入信息,所以需要将System.in 封装到一个* DataInputStream 中。这是 Java 1.0 进行行输入时采取的“老”办法。在本章稍后,大家还会看到 Java 1.1* 的解决方案。下面是个简单的例子,作用是回应我们键入的每一行内容:*/
public class Echo {public static void main(String[] args) {DataInputStream in =new DataInputStream(new BufferedInputStream(System.in));String s;/*** 之所以要使用try 块,是由于 readLine()可能“掷”出一个 IOException。注意同其他大多数流一样,也应* 对System.in 进行缓冲。* 由于在每个程序中都要将System.in 封装到一个 DataInputStream 内,所以显得有点不方便。但采用这种设* 计方案,可以获得最大的灵活性。*/try {while((s = in.readLine()).length() != 0)System.out.println(s);// An empty line terminates the program} catch(IOException e) {e.printStackTrace();}}
} ///:~
package thinking.in.java.chapter10;//: SortedWordCount.java
// Counts words in a file, outputs
// results in sorted form.
import thinking.in.java.chapter8.StrSortVector;import java.io.*;
import java.util.*;/*** 下面是一个简单的程序,用于计算各个单词在文本文件中重复出现的次数:*/
class Counter {private int i = 1;int read() { return i; }void increment() { i++; }
}
public class SortedWordCount {private FileInputStream file;private StreamTokenizer st;private Hashtable counts = new Hashtable();SortedWordCount(String filename)throws FileNotFoundException {try {file = new FileInputStream(filename);st = new StreamTokenizer(file);st.ordinaryChar('.');st.ordinaryChar('-');} catch(FileNotFoundException e) {System.out.println("Could not open " + filename);throw e;}}void cleanup() {try {file.close();} catch(IOException e) {System.out.println("file.close() unsuccessful");}}void countWords() {try {while(st.nextToken() !=StreamTokenizer.TT_EOF) {String s;switch(st.ttype) {case StreamTokenizer.TT_EOL:s = new String("EOL");break;case StreamTokenizer.TT_NUMBER:s = Double.toString(st.nval);break;case StreamTokenizer.TT_WORD:s = st.sval; // Already a Stringbreak;default: // single character in ttypes = String.valueOf((char)st.ttype);}if(counts.containsKey(s))((Counter)counts.get(s)).increment();elsecounts.put(s, new Counter());}} catch(IOException e) {System.out.println("st.nextToken() unsuccessful");}}Enumeration values() {return counts.elements();}Enumeration keys() { return counts.keys(); }Counter getCounter(String s) {return (Counter)counts.get(s);}Enumeration sortedKeys() {Enumeration e = counts.keys();StrSortVector sv = new StrSortVector();while(e.hasMoreElements())sv.addElement((String)e.nextElement());// This call forces a sort:return sv.elements();}public static void main(String[] args) {try {SortedWordCount wc =new SortedWordCount(args[0]);wc.countWords();Enumeration keys = wc.sortedKeys();while(keys.hasMoreElements()) {String key = (String)keys.nextElement();System.out.println(key + ": "+ wc.getCounter(key).read());}wc.cleanup();} catch(Exception e) {e.printStackTrace();}}
} ///:~
package thinking.in.java.chapter8;//: StrSortVector.java
// Automatically sorted Vector that
// accepts and produces only Strings
import java.util.*;
public class StrSortVector {private SortVector v = new SortVector(// Anonymous inner class:new Compare() {public booleanlessThan(Object l, Object r) {return((String)l).toLowerCase().compareTo(((String)r).toLowerCase()) < 0;}public booleanlessThanOrEqual(Object l, Object r) {return((String)l).toLowerCase().compareTo(((String)r).toLowerCase()) <= 0;}});private boolean sorted = false;public void addElement(String s) {v.addElement(s);sorted = false;}public String elementAt(int index) {if(!sorted) {v.sort();sorted = true;}return (String)v.elementAt(index);}public Enumeration elements() {if(!sorted) {v.sort();sorted = true;}return v.elements();}// Test it:public static void main(String[] args) {StrSortVector sv = new StrSortVector();sv.addElement("d");sv.addElement("A");sv.addElement("C");sv.addElement("c");sv.addElement("b");sv.addElement("B");sv.addElement("D");sv.addElement("a");Enumeration e = sv.elements();while(e.hasMoreElements())System.out.println(e.nextElement());}
} ///:~
package thinking.in.java.chapter10;//: AnalyzeSentence.java
// Look for particular sequences
// within sentences.
import java.util.*;
public class AnalyzeSentence {public static void main(String[] args) {analyze("I am happy about this");analyze("I am not happy about this");analyze("I am not! I am happy");analyze("I am sad about this");analyze("I am not sad about this");analyze("I am not! I am sad");analyze("Are you happy about this?");analyze("Are you sad about this?");analyze("It's you! I am happy");analyze("It's you! I am sad");}static StringTokenizer st;static void analyze(String s) {prt("\nnew sentence >> " + s);boolean sad = false;st = new StringTokenizer(s);while (st.hasMoreTokens()) {String token = next();// Look until you find one of the// two starting tokens:if(!token.equals("I") &&!token.equals("Are"))continue; // Top of while loopif(token.equals("I")) {String tk2 = next();if(!tk2.equals("am")) // Must be after Ibreak; // Out of while loopelse {String tk3 = next();if(tk3.equals("sad")) {sad = true;break; // Out of while loop}if (tk3.equals("not")) {String tk4 = next();if(tk4.equals("sad"))break; // Leave sad falseif(tk4.equals("happy")) {sad = true;break;}}}}if(token.equals("Are")) {String tk2 = next();if(!tk2.equals("you"))break; // Must be after AreString tk3 = next();if(tk3.equals("sad"))sad = true;break; // Out of while loop}}if(sad) prt("Sad detected");}static String next() {if(st.hasMoreTokens()) {String s = st.nextToken();prt(s);return s;}elsereturn "";}static void prt(String s) {System.out.println(s);}
} ///:~

Java IO 系统(一)相关推荐

  1. Java IO 系统

    Java IO系统 File类 用来处理文件目录,既可以代表一个特定文件的名称,也可以代表一组文件的名称,如果代表的是一个文件组,可以调用File.list()方法返回一个字符数组. list()不传 ...

  2. java io系统_java中的io系统详解

    Java 流在处理上分为字符流和字节流.字符流处理的单元为 2 个字节的 Unicode 字符,分别操作字符.字符数组或字符串,而字节流处理单元为 1 个字节,操作字节和字节数组. Java 内用 U ...

  3. I/O流(包括操作系统与内核,用户空间),I/O工作原理,Java I/O流的设计及Java IO系统

    文章目录 一.操作系统与内核 1.1操作系统 1.2内核 1.3 关系图 二.内核空间和用户空间 2.1:目的: 2.2.内核空间(Kernel-space): 2.3.用户空间(User-space ...

  4. java牛奶订购系统,Java IO系统

    IO流: 1: 字节流    java.io.InputStream  /  java.io.OutputStream 2:字符流     java.io.Reader   /  java.io.Wr ...

  5. think in java i o_5.[Think in Java笔记]Java IO系统

    1.输入输出 InputStream和OutputStream是面向字节的,Reader和Write则面向字符的且兼容Unicode. InputStream类型 ByteArrayInputStre ...

  6. 五.Java IO、NIO、文件、通讯

    2019独角兽企业重金招聘Python工程师标准>>> Java 的 I/O 大概可以分成四组: 基于字节操作的 I/O :InputStream 和 OutputStream 基于 ...

  7. java byte char io流_一文带你看懂JAVA IO流,史上最全面的IO教学

    原标题:一文带你看懂JAVA IO流,史上最全面的IO教学 一.IO流是什么 惯例引用百科的回答 流是一种抽象概念,它代表了数据的无结构化传递.按照流的方式进行输入输出,数据被当成无结构的字节序或字符 ...

  8. java io流详解_一文带你看懂JAVA IO流,史上最全面的IO教学啦

    一.IO流是什么 惯例引用百科的回答流是一种抽象概念,它代表了数据的无结构化传递.按照流的方式进行输入输出,数据被当成无结构的字节序或字符序列.从流中取得数据的操作称为提取操作,而向流中添加数据的操作 ...

  9. javaio流_一文带你看懂JAVA IO流,史上最全面的IO教学啦

    一.IO流是什么 惯例引用百科的回答 流是一种抽象概念,它代表了数据的无结构化传递.按照流的方式进行输入输出,数据被当成无结构的字节序或字符序列.从流中取得数据的操作称为提取操作,而向流中添加数据的操 ...

  10. 一文带你看懂JAVA IO流(一),史上最全面的IO教学啦(附送JAVA IO脑图)

    一.IO流是什么 惯例引用百科的回答 流是一种抽象概念,它代表了数据的无结构化传递.按照流的方式进行输入输出,数据被当成无结构的字节序或字符序列.从流中取得数据的操作称为提取操作,而向流中添加数据的操 ...

最新文章

  1. Caffe 中关于 LetNet-5 网络的定义文件 lenet.prototxt 解析
  2. 组合使用Laravel和vfsStream测试文件上传
  3. [数据库] Navicat for MySQL事件Event实现数据每日定期操作
  4. 如何查找历史线程阻塞原因_学习 Web Worker(js中的“多线程”)
  5. “”和“” java
  6. 50個AJAX Tools and Frameworks
  7. python学了真的很有用吗-学习Python真的有必要参加培训吗?老男孩Python学习机构...
  8. “互联网+”时代,漫谈影响用户体验的X因素
  9. 数据库访问优化法则详解之返回更少的数据
  10. 外螺纹对照表_螺纹对照表
  11. 微信小程序插件安装Vant有赞组件库
  12. ValueError:only one element tensors can be converted to Python scalars解决办法
  13. 微信发红包的测试用例点
  14. Linux软件包管理工具-yum
  15. 什么第一台多媒体电子计算机诞生,第一台多媒体电计算机是哪一年诞生的
  16. PLC远程监控与数据采集方案
  17. 关于wlw(windows live writer):“无法连接到您的日志服务:服务器响应无效”的解决方案
  18. Nginx rewrite路由重写
  19. EBS 报表开发:标准报表Text输出改为XML输出
  20. mmdetection 环境配置与简单测试(mmrotate同理)

热门文章

  1. 【TDA4系列】向 PSDKRA 添加新的图像传感器
  2. 【车道线检测与寻迹】2月13日 CV导论+数字图像处理与opencv实践+canny边缘检测
  3. Interpreting and Disentangling Feature Components of Various Complexity from DNNS论文解读
  4. Linux基础(day74)
  5. Hibernate_02
  6. 用函数调用的方法输出乘法口诀表
  7. 初识设计模式之--简单工厂
  8. python 教程 第十章、 输入/输出
  9. Linux命令之passwd
  10. 热点:安全问题是否能将DNS推入云服务